码迷,mamicode.com
首页 > 移动开发 > 详细

Android滑动页面返回(自定义控件)

时间:2014-08-28 22:36:06      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   os   java   io   ar   2014   

今天看了下腾讯微博,看到“赞你的人”可以向右滑动然后直接返回上一级,兴趣一来,抽点空就把这个控件给做了。先看几张效果图,本人不会做gif,亲们能理解就OK了。

bubuko.com,布布扣    bubuko.com,布布扣     bubuko.com,布布扣

话不多说,现在直接贴出源码吧。

首先看下调用方式

package com.fay.backwidget;

import android.app.Activity;
import android.os.Bundle;

import com.fay.backwidget.SlidingBackLinearLayout.FinishCallBack;
/**
 * @version 1.0
 * @author Fay
 * @since 2014/8/28
 */
public class SlidingBackActivity extends Activity {
	private SlidingBackLinearLayout mBackLinearLayout = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_two);
		mBackLinearLayout = (SlidingBackLinearLayout) findViewById(R.id.back_linearlayout);
		mBackLinearLayout.setFinishCallBack(new FinishCallBack() {
			
			@Override
			public void finish() {
				// TODO Auto-generated method stub
				SlidingBackActivity.this.finish();
			}
		});
	}

}

  可以看到调用的方式十分简单,这就是我们每一个开发者追求的,高内聚低耦合,下面我把自定义控件贴出来吧。

 

package com.fay.backwidget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;
/**
 * @version 1.0
 * @author Fay
 * @since 2014/8/28
 */
@SuppressLint("NewApi")
public class SlidingBackLinearLayout extends LinearLayout {
    private int xLastTouchLocation;
    private int xCurrentTouchLocation;
    private int detaX;
    private boolean isClose = false;
    private int windowWidth ;
    private Scroller mScroller = null;
    private Interpolator mInterpolator = null; 
	private FinishCallBack mFinishCallBack = null;
	
	public SlidingBackLinearLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public SlidingBackLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init(context);
	}

	public SlidingBackLinearLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init(context);
	}
	
	private void init(Context mContext) {
		mInterpolator = new DecelerateInterpolator();
		mScroller = new Scroller(mContext, mInterpolator);
		DisplayMetrics mDisplayMetrics = new DisplayMetrics();
		WindowManager mWindowManager = (WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE);
		mWindowManager.getDefaultDisplay().getMetrics(mDisplayMetrics);
		windowWidth = mDisplayMetrics.widthPixels;
	}
	
	public interface FinishCallBack{
		void finish();
	}
	
	public void setFinishCallBack(FinishCallBack mFinishCallBack) {
		this.mFinishCallBack = mFinishCallBack;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			xLastTouchLocation = (int) event.getX();
			return true;
		case MotionEvent.ACTION_MOVE:
			xCurrentTouchLocation = (int) event.getX();
			detaX = xLastTouchLocation - xCurrentTouchLocation;
			if (detaX < 0) {
				if (null != mFinishCallBack) {
					scrollTo( detaX, 0);
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			xCurrentTouchLocation = (int) event.getX();
			detaX = xLastTouchLocation - xCurrentTouchLocation;
			if (detaX < 0) {
				if (null != mFinishCallBack) {
					if (- detaX >= windowWidth / 2) {
						// close
						isClose = true;
						startMove( -(windowWidth + detaX));
					} else {
						startMove(- detaX);
					}
				}
			}
			break;
		}
		return super.onTouchEvent(event);
	}
	
	private void startMove(int deta) {
		mScroller.startScroll(getScrollX(), 0, deta, 0);
		invalidate();
	}

	@Override
	public void computeScroll() {
		// TODO Auto-generated method stub
		super.computeScroll();
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		} else {
			if (isClose) {
				if (null != mFinishCallBack) {
					mFinishCallBack.finish();
				}
			}
		}
	}

}

  其实就是这么简单,希望大家有所收获。

 

Android滑动页面返回(自定义控件)

标签:android   style   blog   http   os   java   io   ar   2014   

原文地址:http://www.cnblogs.com/yinweiliang/p/3942418.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!