详细讲解自定义ViewGroup+Scroller+VelocityTracker做出Launcher滑动
来源:互联网 发布:windows的最新版本 编辑:程序博客网 时间:2024/06/03 14:28
转自:http://blog.csdn.net/u014649337/article/details/38302535
滑动在Android UI界面中用的也是比较多的,比如垂直滑动,水平滑动,侧边滑动等等,而且android 也不乏像viewpager这样的滑动控件, 但是有时在做项目的时候,ViewPager
往往不能 满足我们的需求,所以我们需要按照自己的要求自定义滑动组件,比如像自定义ViewGroup 就可以帮助我们实现这个需求,下面我们直接上代码 看看自定ViewGroup
是怎样实现滑动的:
实例代码如下:
自定义ViewGroup , MainGroup.java
- package com.example.myflaydemo;
-
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.VelocityTracker;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Scroller;
-
- public class MainGroup extends ViewGroup {
-
- float mLastionMotionX = 0 ;
- Scroller scroller;
- VelocityTracker velocityTracker;
- int SNAP_VELOCITX = 600;
- int currentScreen = 0;
-
- public MainGroup(Context context, AttributeSet attrs) {
- super(context, attrs);
- scroller = new Scroller(context);
-
- }
-
-
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
-
-
- int totalWidth = 0;
- int childCount = getChildCount();
- for (int i = 0; i < childCount; i++) {
- View childView = getChildAt(i);
- if(childView.getVisibility() != View.GONE){
- int childWidth = childView.getMeasuredWidth();
- int childHeight = childView.getMeasuredHeight();
- childView.layout(totalWidth, 0, totalWidth+childWidth, childHeight);
- totalWidth += childWidth;
- }
- }
-
- }
-
-
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
- int width = measureWidth(widthMeasureSpec);
- int height = measureHeight(heightMeasureSpec);
-
- measureChildren(widthMeasureSpec, heightMeasureSpec);
-
- setMeasuredDimension(width, height);
- }
-
-
- public int measureWidth(int widthMeasureSpec){
- int result = 0;
- int measureMode = MeasureSpec.getMode(widthMeasureSpec);
- int width = MeasureSpec.getSize(widthMeasureSpec);
- switch (measureMode) {
- case MeasureSpec.AT_MOST:
- case MeasureSpec.EXACTLY:
- result = width;
- break;
- default:
- break;
- }
- return result;
- }
-
-
- public int measureHeight(int heightMeasureSpec){
- int result = 0;
- int measureMode = MeasureSpec.getMode(heightMeasureSpec);
- int height = MeasureSpec.getSize(heightMeasureSpec);
- switch (measureMode) {
- case MeasureSpec.AT_MOST:
- case MeasureSpec.EXACTLY:
- result = height;
- break;
- default:
- break;
- }
- return result;
- }
-
-
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
-
- float x = event.getX();
- if(velocityTracker == null){
- velocityTracker = VelocityTracker.obtain();
- }
-
- velocityTracker.addMovement(event);
-
- int action = event.getAction();
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- mLastionMotionX = x;
- break;
- case MotionEvent.ACTION_MOVE:
- int detalX = (int) (mLastionMotionX-x);
-
- int scrollX = getScrollX();
-
- if (detalX < 0 && scrollX + detalX < 0) {
- detalX = 0 - scrollX;
- } else if (detalX > 0 && scrollX + detalX > (getChildCount()-1)*getWidth()) {
- detalX = (getChildCount()-1)*getWidth() - scrollX;
- }
-
- scrollBy(detalX, 0);
- mLastionMotionX = x;
- break;
- case MotionEvent.ACTION_UP:
- int velocityX = (int) velocityTracker.getXVelocity();
- if(velocityX>SNAP_VELOCITX && currentScreen>0){
- snapToScreen(currentScreen-1);
- }
- else if(velocityX<-SNAP_VELOCITX && currentScreen<(getChildCount()-1)){
- snapToScreen(currentScreen+1);
- }else{
- snapToDestination();
- }
- break;
-
- default:
- break;
- }
-
- return true;
- }
-
- @Override
- public void computeScroll() {
-
- super.computeScroll();
-
- if(scroller.computeScrollOffset()){
- scrollTo(scroller.getCurrX(), scroller.getCurrY());
- postInvalidate();
- }
-
- }
-
- private void snapToDestination() {
-
-
- int whichScreen = ( getScrollX() + getWidth()/2 ) / getWidth();
- snapToScreen(whichScreen);
- }
-
-
- public void snapToScreen(int whichScreen){
-
- currentScreen = whichScreen;
-
-
-
-
-
- int dx = currentScreen*getWidth() - getScrollX();
-
- scroller.startScroll(getScrollX(), 0, dx, 0, Math.abs(dx) * 2);
-
- postInvalidate();
- }
-
- }
ViewGroup在布局文件的使用:
activity_main.xml
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <com.example.myflaydemo.MainGroup
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
-
- <include layout="@layout/activity_main2" />
-
- <include layout="@layout/activity_main3" />
- </com.example.myflaydemo.MainGroup>
-
- </RelativeLayout>
activity_main2.xml:- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
-
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/guide03" />
-
- </RelativeLayout>
activity_main3.xml- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
-
- <ImageView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/guide04" />
-
- </RelativeLayout>
注意 guide03和guide04两张图片 你随便在网上找两张图片就可以了.
手势滑动的相关链接:http://blog.csdn.net/shichaosong/article/details/20122513
0 0