触摸滑动方法-Scroll分析

学习了android群英传的Scroll方法,实践一番。实现的效果是当触摸View时,记下当前触摸点坐标;当手指移动式,几下移动后的触摸点坐标,从而得到两个前后触摸点的偏移量,并通过这个便宜鲁昂来修改View的坐标,如此反复,即可实现滑动过程。
先看布局文件

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

    <com.test.myapplication.MyView
        android:background="#000000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <com.test.myapplication.CustomTexView
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="android"/>
    <com.test.myapplication.DragView
        android:background="#000000"
        android:layout_width="80dp"
        android:layout_height="80dp"/>
</LinearLayout>

这里主要是重写自定义的DragView来实现滑动效果,完成代码如下:

package com.test.myapplication;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

/**
 * Created by wilsen on 2016/12/24.
 */
public class DragView extends View {
    private int lastx, lasty;
    private Scroller m_scroller;
    public DragView(Context context) {
        super(context);
    }

    public DragView(Context context, AttributeSet attrs) {
        super(context, attrs);
        m_scroller=new Scroller(context);
    }

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

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (m_scroller.computeScrollOffset()) {
            ((View)getParent()).scrollTo(m_scroller.getCurrX(),m_scroller.getCurrY());
            invalidate();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //return super.onTouchEvent(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;
                //方法1 方便有效,改变控件自身相对于父布局的位置
                layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);

                //方法2  方便有效,改变控件自身相对于父布局的位置
                /*offsetLeftAndRight(offsetX);
                offsetTopAndBottom(offsetY);*/

                //方法31  有一定局限,改变控件自身相对于父布局的位置
                /*LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams();
                layoutParams.leftMargin=getLeft()+offsetX;
                layoutParams.topMargin=getTop()+offsetY;
                setLayoutParams(layoutParams);*/
                //方法32 ,改变控件自身相对于父布局的位置
                /*ViewGroup.MarginLayoutParams layoutParams= (ViewGroup.MarginLayoutParams) getLayoutParams();
                layoutParams.leftMargin=getLeft()+offsetX;
                layoutParams.topMargin=getTop()+offsetY;
                setLayoutParams(layoutParams);*/

                //方法4   会移动父布局中的所有子布局和控件,改变控件的content位置,而非控件自身相对于父布局的位置
                ((View)getParent()).scrollBy(-offsetX,-offsetY);
                break;
            case MotionEvent.ACTION_UP:
                //方法5  需要与scrollBy或scrollTo配合使用,返回到之前的位置 ,改变控件的content位置,而非控件自身相对于父布局的位置
                View viewGroup=(View)getParent();
                m_scroller.startScroll(viewGroup.getScrollX(),viewGroup.getScrollY(),-viewGroup.getScrollX(),-viewGroup.getScrollY());
                invalidate();
                break;

        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值