View体系第二篇:View滑动

View滑动的基本思想:当点击事件传到View时,系统记下触摸点的坐标,手指移动时系统记下触摸后的坐标并计算出偏移量,然后根据偏移量修正View坐标.

实现View滑动共有6种方法:layout()方法,offsetTopAndBottom(),LayoutParams,动画,scrollTo与scrollBy,以及Scroller.

一.layout()

import ...;

public class CustomView extends View {
    private int lastX;
    private int lastY;

    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @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;
        //修改View的left,top,right,bottom属性重新放置View layout(getLeft()
+ offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY); break; } return true; } }

然后在布局文件中引用就好了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >

    <com.example.scroll_test.CustomView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@color/colorAccent"
        />

</LinearLayout>

二.offsetLeftandRight

这种方法与Layout()方法相似,只需将ACTION_MOVE修改成以下代码:

            case MotionEvent.ACTION_MOVE:
                int offsetX = x - lastX;
                int offsetY = y - lastY;
                offsetLeftAndRight(offsetX);
                offsetTopAndBottom(offsetY);
                break;
        }

三.LayoutParams(改变布局参数)

通过LayoutParams改变View的布局参数,从而达到修改View位置的效果.

 将ACTION_MOVE修改成以下代码:

            case MotionEvent.ACTION_MOVE:
                int offsetX = x - lastX;
                int offsetY = y - lastY;
           LinearLayout.LayoutParams layoutParams
= (LinearLayout.LayoutParams) getLayoutParams(); layoutParams.leftMargin=getLeft() + offsetX; layoutParams.topMargin=getTop() + offsetY; setLayoutParams(layoutParams); break; }

由于父控件是LinearLayout,所以用到了LineatLayout.LayoutParams.如果是RelativeLayout,则要使用RelativeLayot.LayoutParams.还可以用ViewGroup.MarginLayoutParams来实现:

ViewGroup.MarginLayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin=getLeft() + offsetX;
                layoutParams.topMargin=getTop() + offsetY;
                setLayoutParams(layoutParams);
                

猜你喜欢

转载自www.cnblogs.com/adressian/p/10771040.html