读(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
- 读(Android开发艺术探究)view 的滑动 (一)
- Android开发艺术探索笔记(9)- View的滑动
- Android开发艺术探索(连载)之View的事件体系(二)View的滑动
- Android开发艺术View滑动
- Android开发艺术探索笔记(10)- View的弹性滑动
- Android开发艺术探索笔记(12)- View的滑动冲突
- Android开发艺术探索学习-View的滑动
- View的滑动冲突解决-android开发艺术探索笔记
- 读《Android开发艺术探索》---View滑动冲突
- Android 开发艺术与探究 第三章 View的事件体系之View的基础知识
- Android开发艺术探索读书笔记-View的工作原理(一)
- Android 开发艺术探究V第三章之view的事件分发机制
- 《Android开发艺术》读书笔记(View的事件体系)
- 《Android开发艺术》之View的事件体系(上)
- Android开发艺术探索笔记(8)- View的基础
- View的工作原理(Android开发艺术探索读书笔记)
- 《Android开发艺术探索》笔记(View的知识体系)
- Android开发艺术探索读书笔记-View的事件体系(一)
- python初学笔记(-)
- 解析JSON数据
- C#线程安全
- Java 集合系列06之 Vector源码解析
- 大型网站技术架构演变过程
- 读(Android开发艺术探究)view 的滑动 (一)
- java-微信公众号充值
- OpenCV学习--saturate_cast防止数据溢出
- 鼓励自己
- display:none与visible:hidden的区别
- CSS3
- 使用注解来构造IoC容器
- hdu1174(点到直线的距离)
- 杭电HDU 1004 Let the Balloon Rise AC代码 简单题