Android:滑动
2016-02-17 14:15
393 查看
1、普通
2、滑动时移动父布局中所有内容
package com.example.textcolordemo.drag; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.MarginLayoutParams; public class DragView1 extends View { private int lastX ,lastY,lastL,lastR,lastT,lastB; public DragView1(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); setBackgroundColor(Color.YELLOW); } public DragView1(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setBackgroundColor(Color.YELLOW); } public DragView1(Context context, AttributeSet attrs) { super(context, attrs); setBackgroundColor(Color.YELLOW); } @Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = x; lastY = y; lastL= getLeft(); lastR = getRight(); lastB = getBottom(); lastT= getTop(); break; case MotionEvent.ACTION_MOVE: int offsetX = x - lastX; int offsetY = y - lastY; //方法1: layout layout(getLeft()+offsetX, getTop()+offsetY, getRight()+offsetX, getBottom()+offsetY); //方法2: setLayoutParams ViewGroup.MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams(); lp.leftMargin = getLeft()+offsetX; lp.topMargin = getTop()+offsetY; setLayoutParams(lp); //方法3:scrollBy 移动ViewGroup中的子View或者是View中的内容 ((View) getParent()).scrollBy(-offsetX, -offsetY); //方法4: api封装好的方法 offsetLeftAndRight(offsetX); offsetTopAndBottom(offsetY); break; case MotionEvent.ACTION_UP: layout(lastL, lastT, lastR, lastB); break; default: break; } return true; } }
2、滑动时移动父布局中所有内容
package com.example.textcolordemo.drag; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.Scroller; public class DragView_Scroller extends View { private int lastX ,lastY,lastL,lastR,lastT,lastB; private Scroller mScroller; public DragView_Scroller(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); setBackgroundColor(Color.YELLOW); mScroller = new Scroller(context); } public DragView_Scroller(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setBackgroundColor(Color.YELLOW); mScroller = new Scroller(context); } public DragView_Scroller(Context context, AttributeSet attrs) { super(context, attrs); setBackgroundColor(Color.YELLOW); mScroller = new Scroller(context); } @Override public void computeScroll() { super.computeScroll(); if (mScroller.computeScrollOffset()) { ((View)getParent()).scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); invalidate(); } } @Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = x; lastY = y; lastL= getLeft(); lastR = getRight(); lastB = getBottom(); lastT= getTop(); break; case MotionEvent.ACTION_MOVE: int offsetX = x - lastX; int offsetY = y - lastY; ((View) getParent()).scrollBy(-offsetX, -offsetY);//使父布局中的所有内容移动 break; case MotionEvent.ACTION_UP: View vg = ((View)getParent()); mScroller .startScroll(vg.getScrollX(), vg.getScrollY(), -vg.getScrollX(), (int) -vg.getScrollY()); invalidate(); break; default: break; } return true; } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories