读(Android开发艺术探究)view 的滑动 (一)

来源:互联网 发布:企业网络机房建设标准 编辑:程序博客网 时间:2024/06/05 14:39

在Android中实现view的滑动,一般有三种解决方案

1、使用ScrollTo/ScrollBy

      看源码不难看出,实际上ScrollBy也是调用了ScrollTo方法,这里就不贴源码了。

2、使用动画

      动画中的平移就是滑动,使用动画来移动view,主要是操作View 的translationX和translationY属性

3、改变布局参数

      就是改变View的LayoutParams,如给View添加一个Margin

     MarginLayoutParams params=(MarginLayoutParams)mTextView.this.getLayoutParams();

     params.width+=10;

     params.leftMargin+=10;

     mTextView.this.requestLayout();//mTextView.this.setLayoutParams(params);





测试代码,我继承了TextView

import android.annotation.TargetApi;import android.content.Context;import android.os.Build;import android.os.SystemClock;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.ViewConfiguration;import android.view.ViewGroup;import android.widget.TextView;import com.nineoldandroids.view.ViewHelper;/** * @类名称: CLASS * @类描述:  * @创建人:刘丽杰 * @创建时间:2017/7/18 14:41 * @备注: */public class mViewText extends TextView {    private int mLastX = 0;    private int mLastY = 0;    private long mLastTouchUpTime = 0;    private long mLastTouchMoveTime = 0;    public mViewText(Context context) {        super(context);        init();    }    public mViewText(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public mViewText(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    @TargetApi(Build.VERSION_CODES.LOLLIPOP)    public mViewText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);        init();    }    private void init() {        Log.e("mViewText", "初始化。。。。。");    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int x = (int) event.getRawX();        int y = (int) event.getRawY();        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                Log.e("mViewText", "action++++dowm");                mLastTouchDownTime = SystemClock.uptimeMillis();                break;            case MotionEvent.ACTION_MOVE:                Log.e("mViewText", "action+++move");                int currentX = x - mLastX;                int currentY = y - mLastY;                int translationx = (int) (ViewHelper.getTranslationX(this) + currentX);                int translationy = (int) (ViewHelper.getTranslationY(this) + currentY);                ViewHelper.setTranslationX(this, translationx);                ViewHelper.setTranslationY(this, translationy);//                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mViewText.this.getLayoutParams();//                params.width += 10;//                params.leftMargin += 10;//                mViewText.this.requestLayout();                break;            case MotionEvent.ACTION_UP:                Log.e("mViewText", "action+++up");                mLastTouchUpTime = SystemClock.uptimeMillis();                break;            default:                break;        }        mLastX = x;        mLastY = y;        //判断是否可以响应点击事件   时间差小于300ms        Log.e("mViewText", "time------" + (mLastTouchUpTime - mLastTouchDownTime));        Log.e("mViewText", "time------" + (ViewConfiguration.getDoubleTapTimeout()));        if (mLastTouchUpTime - mLastTouchDownTime <= ViewConfiguration.getDoubleTapTimeout()) {            return super.onTouchEvent(event);        } else {            return true;        }    }}



采用动画的方式来实现滑动,适用于Android3.0以上版本,在布局文件中添加控件,正常定义点击事件,就可实现滑动控件,点击事件



算是读android开发艺术探究的读后感和自己操作的测试哦大笑

阅读全文
0 0
原创粉丝点击