Android实现滑动的方法之一
layout方法
View绘制时,会调用onLayout()方法,那么这里就是通过修改View的left、top、right、bottom四个属性控制View坐标。我们获取的坐标可以是Android坐标系也可以是视图坐标系。
public class DragView1 extends View {
private int lastX;
private int lastY;
public DragView1(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(Color.BLUE);
}
@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;
break;
case MotionEvent.ACTION_MOVE:
// 计算偏移量
int offsetX = x - lastX;
int offsetY = y - lastY;
// 重写布局
layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
break;
}
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<me.zcy.dragview1.DragView1
android:layout_width="100dp"
android:layout_height="100dp" />
</RelativeLayout>
运行效果
通过系统对上下左右移动的封装
public class DragView1 extends View {
private int lastX;
private int lastY;
public DragView1(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(Color.BLUE);
}
@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;
break;
case MotionEvent.ACTION_MOVE:
// 计算偏移量
int offsetX = x - lastX;
int offsetY = y - lastY;
// 重写布局
// layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
// 系统的封装
offsetLeftAndRight(offsetX);
offsetTopAndBottom(offsetY);
break;
}
return true;
}
}
通过布局参数LayoutParams
LayoutParams保存了一个View的布局参数
通过getLayoutParams()获取LayoutParams时需要根据View所在父布局的类型类设置不同的类型,如果View在LinearLayout中,那么就使用LinearLayout.LayoutParams
如果不想考虑父布局的类型,那么使用ViewGroup.MarginLayoutParams,当然本质是一样的
通过改变View的margin值来实现偏移
public class DragView1 extends View {
private int lastX;
private int lastY;
public DragView1(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundColor(Color.BLUE);
}
@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;
break;
case MotionEvent.ACTION_MOVE:
// 计算偏移量
int offsetX = x - lastX;
int offsetY = y - lastY;
// 重写布局
// layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);
// 系统的封装
// offsetLeftAndRight(offsetX);
// offsetTopAndBottom(offsetY);
// MarginLayoutParams
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
// LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams();
layoutParams.leftMargin = getLeft() + offsetX;
layoutParams.topMargin = getTop() + offsetY;
setLayoutParams(layoutParams);
break;
}
return true;
}
}
运行效果与上图一直
浙公网安备 33010602011771号