Android仿照Launcher的Workspace实现左右滑动切换

Android仿照Launcher的Workspace实现左右滑动切换
Android仿照Launcher的Workspace实现左右滑动切换

Android功能总结:仿照Launcher的Workspace实现左右滑动切换

作者:吴镇风发表于2011-8-9 22:22:55 评论(0) 阅读(74)

对于Launcher的桌面滑动大家应该都比较熟悉了,最好的体验应该是可以随着手指的滑动而显示不同位置的桌面,

比一般用ViewFlinger+动画所实现的手势切换页面感觉良好多了~~~~

分析了一下Launcher中的WorkSpace,里面有太多的代码我们用不上了(拖拽,长按,,,),把里面的冗余代码去掉得到实现滑动切换屏幕所必需的。。。。

新建一个ScrollLayout类,继承自ViewGroup。

重写onMeasure和onLayout两个方法:

其中onMeasure方法中,得到ScrollLayout的布局方式(一般使用FILL_PAR ENT),然后再枚举其中所有的子view,设置它们的布局(FILL_PARENT),这样在ScrollLayout之中的每一个子view即为充满屏幕可以滑动显示的其中一页。

在onLayout方法中,横向画出每一个子view,这样所得到的view的高与屏幕高一致,宽度为getChildCount()-1个屏幕宽度的view。

添加一个Scroller来平滑过渡各个页面之间的切换,

重写onInterceptTouchEvent和onTouchEvent来响应手指按下划动时所需要捕获的消息,例如划动的速度,划动的距离等。再配合使用scrollBy (int x, i nt y)方法得到慢速滑动小距离的时候,所需要显示的内容。最后当手指起来时,

根据划动的速度与跨度来判断是向左滑动一页还是向右滑动一页,确保每次用户操作结束之后显示的都是整体的一个子view.

ScrollLayout源码:

view plaincopy to clipboardprint?

package com.yao_guet.test;

import Android.content.Context;

import android.graphics.Canvas;

import android.util.AttributeSet;

import android.util.Log;

import android.view.MotionEvent;

import android.view.VelocityTracker;

import android.view.View;

import android.view.ViewConfiguration;

import android.view.ViewGroup;

import android.widget.Scroller;

/**

* 仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类

* @author Yao.GUET

* blog: https://www.360docs.net/doc/373857316.html,/Yao_GUET

* date: 2011-05-04

*/

public class ScrollLayout extends ViewGroup {

private static final String TAG = "ScrollLayout";

private Scroller mScroller;

private VelocityTracker mVelocityTracker;

private int mCurScreen;

private int mDefaultScreen = 0;

private static final int TOUCH_STATE_REST = 0;

private static final int TOUCH_STATE_SCROLLING = 1;

private static final int SNAP_VELOCITY = 600;

private int mTouchState = TOUCH_STATE_REST;

private int mTouchSlop;

private float mLastMotionX;

private float mLastMotionY;

public ScrollLayout(Context context, AttributeSet attrs) { this(context, attrs, 0);

// TODO Auto-generated constructor stub

}

public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

// TODO Auto-generated constructor stub

mScroller = new Scroller(context);

mCurScreen = mDefaultScreen;

mTouchSlop =

ViewConfiguration.get(getContext()).getScaledTouchSlop();

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

// TODO Auto-generated method stub

if (changed) {

int childLeft = 0;

final int childCount = getChildCount();

for (int i=0; i

final View childView = getChildAt(i);

if (childView.getVisibility() != View.GONE) {

final int childWidth = childView.getMeasuredWidth();

https://www.360docs.net/doc/373857316.html,yout(childLeft, 0,

childLeft+childWidth,

childView.getMeasuredHeight());

childLeft += childWidth;

}

}

}

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

L og.e(TAG, "onMeasure");

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

final int width = MeasureSpec.getSize(widthMeasureSpec);

final int widthMode = MeasureSpec.getMode(widthMeasureSpec);

if (widthMode != MeasureSpec.EXACTLY) {

throw new IllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!");

}

final int heightMode = MeasureSpec.getMode(heightMeasureSpec);

if (heightMode != MeasureSpec.EXACTLY) {

throw new IllegalStateException("ScrollLayout only can run at EXACTLY mode!");

}

// The children are given the same width and height as the scrollLayout

final int count = getChildCount();

for (int i = 0; i < count; i++) {

getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); }

// Log.e(TAG, "moving to screen "+mCurScreen);

scrollTo(mCurScreen * width, 0);

}

/**

* According to the position of current layout

* scroll to the destination page.

*/

public void snapToDestination() {

f inal int screenWidth = getWidth();

f inal int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;

s napToScreen(destScreen);

}

public void snapToScreen(int whichScreen) {

// get the valid layout page

w hichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));

i f (getScrollX() != (whichScreen*getWidth())) {

final int delta = whichScreen*getWidth()-getScrollX();

mScroller.startScroll(getScrollX(), 0,

delta, 0, Math.abs(delta)*2);

mCurScreen = whichScreen;

invalidate(); // Redraw the layout

}

}

public void setToScreen(int whichScreen) {

w hichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));

m CurScreen = whichScreen;

s crollTo(whichScreen*getWidth(), 0);

}

public int getCurScreen() {

r eturn mCurScreen;

}

@Override

public void computeScroll() {

// TODO Auto-generated method stub

if (https://www.360docs.net/doc/373857316.html,puteScrollOffset()) {

scrollTo(mScroller.getCurrX(), mScroller.getCurrY());

postInvalidate();

}

}

@Override

public boolean onTouchEvent(MotionEvent event) {

// TODO Auto-generated method stub

if (mVelocityTracker == null) {

mVelocityTracker = VelocityTracker.obtain();

}

mVelocityTracker.addMovement(event);

final int action = event.getAction();

final float x = event.getX();

final float y = event.getY();

switch (action) {

Log.e(TAG, "event down!");

if (!mScroller.isFinished()){

mScroller.abortAnimation();

}

mLastMotionX = x;

break;

case MotionEvent.ACTION_MOVE:

int deltaX = (int)(mLastMotionX - x);

mLastMotionX = x;

scrollBy(deltaX, 0);

break;

case MotionEvent.ACTION_UP:

Log.e(TAG, "event : up");

// if (mTouchState == TOUCH_STATE_SCROLLING) {

final VelocityTracker velocityTracker = mVelocityTracker; https://www.360docs.net/doc/373857316.html,puteCurrentVelocity(1000);

int velocityX = (int) velocityTracker.getXVelocity();

Log.e(TAG, "velocityX:"+velocityX);

if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {

// Fling enough to move left

Log.e(TAG, "snap left");

snapToScreen(mCurScreen - 1);

} else if (velocityX < -SNAP_VELOCITY

&& mCurScreen < getChildCount() - 1) {

// Fling enough to move right

Log.e(TAG, "snap right");

snapToScreen(mCurScreen + 1);

} else {

snapToDestination();

}

if (mVelocityTracker != null) {

mVelocityTracker.recycle();

mVelocityTracker = null;

}

// }

mTouchState = TOUCH_STATE_REST;

break;

mTouchState = TOUCH_STATE_REST;

break;

}

return true;

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub

Log.e(TAG, "onInterceptTouchEvent-slop:"+mTouchSlop);

final int action = ev.getAction();

if ((action == MotionEvent.ACTION_MOVE) &&

(mTouchState != TOUCH_STATE_REST)) {

return true;

}

final float x = ev.getX();

final float y = ev.getY();

switch (action) {

case MotionEvent.ACTION_MOVE:

final int xDiff = (int)Math.abs(mLastMotionX-x);

if (xDiff>mTouchSlop) {

mTouchState = TOUCH_STATE_SCROLLING;

}

break;

case MotionEvent.ACTION_DOWN:

mLastMotionX = x;

mLastMotionY = y;

mTouchState = mScroller.isFinished()? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;

break;

case MotionEvent.ACTION_CANCEL:

case MotionEvent.ACTION_UP:

mTouchState = TOUCH_STATE_REST;

break;

}

return mTouchState != TOUCH_STATE_REST;

}

}

ScrollLayoutTest.Java

package com.yao_guet.test;

import android.app.Activity;

import android.os.Bundle;

public class ScrollLayoutTest extends Activity {

/** Called when the activity is first created. */ @Override

public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(https://www.360docs.net/doc/373857316.html,yout.main);

}

}

Android简单的登陆界面的设计开发

通信实训报告 -Android移动平台开发 学院:信息工程学院 班级: 学号: 姓名:

实训内容: 一.1.Andriod的简介 Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统、中间件、用户界面和应用软件组成,号称是首个为移动终端打造的真正开放和完整的移动软件。目前,最新版本为Android 2.4 Gingerbread 和Android 3.0 Honeycomb。 Android是基于Linux开放性内核的操作系统,是Google公司在2007年11月5日公布的手机操作系统。 Android早期由原名为"Android"的公司开发,谷歌在2005年收购"Android.Inc"后,继续对Android系统开发运营,它采用了软件堆层(software stack,又名软件叠层)的架构,主要分为三部分。底层Linux内核只提供基本功能,其他的应用软件则由各公司自行开发,部分程序以Java编写。2011年初数据显示,仅正式上市两年的操作系统Android已经超越称霸十年的塞班系统,使之跃居全球最受欢迎的智能手机平台。现在,Android系统不但应用于智能手机,也在平板电脑市场急速扩张,在智能MP4方面也有较大发展。采用Android系统主要厂商包括台湾的HTC,(第一台谷歌的手机G1由HTC生产代工)美国摩托罗拉,SE等,中国大陆厂商如:魅族(M9),华为、中兴、联想、蓝魔等。 2.Android构架图 二.1软件下载 Android SDK,网址是https://www.360docs.net/doc/373857316.html,. JDK的下载地址https://www.360docs.net/doc/373857316.html,/javase/downloads/widget/jdk6.jsp。Eclipse的下载网址是https://www.360docs.net/doc/373857316.html,/downloads/ 2.Android开发环境搭建

第4章 Android用户界面设计

视图组件的使用模式 常用组件 高级组件 提示框与警告对话框

就是Android应用程序的开发过程。一般过程是先通过XML布局文件或Java代码创建界面布局,设定组件显示样式,随后获取UI组件对象,并处理组件事件响应。 视图组件的定义 资源的访问 生成视图组件资源标识 视图组件的引用 视图组件的事件响应 组件的常用属性

1.1视图组件的定义 使用XML布局文件定义视图组件 使用Java代码定义视图组件(不推荐)

1.1视图组件的定义 使用XML布局文件定义视图组件 Android平台为大多数视图组件以及其子类提供了XML标记,可通过XML布局文件中的标记来定义视图组件。XML中的每个元素代表了一个组件,即元素名称对应相应的Java类。

1.1视图组件的定义

相关文档
最新文档