实现一个随手指移动而滑动的效果---爬坑

@(Android笔记)

scrollTo和scrollBy爬坑初体验

我只是想要实现一个可以随着手指移动而移动的小滑块

当我实现 public class MyView extends View的时候直接使用是有问题的

package com.example.glf.myapplication;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * Created by glf on 17-6-24.
 */

public class MyView extends View {

    int lastX = 0;
    int lastY = 0;
    public MyView(Context context) {
        super(context);

    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int curentX = (int) event.getX();
        int curentY = (int) event.getY();
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:

                break;
            case MotionEvent.ACTION_MOVE:
                int detalX = curentX - lastX;
                int detalY = curentY - lastY;
                scrollBy(-detalX,-detalY);
                break;
            case MotionEvent.ACTION_UP:        
                break;
            default:
                break;

        }

        lastX = curentX;
        lastY = curentY;
        return true;
    }
}
  • 实现上面的代码,为我们的View设置背景色
  • 以上的代码会发现我们定义的View并不会随着手指的移动而移动

问题在于:

  • srcollTo和scrollBy是移动View的视图,不包括背景色
  • 当我们将View改为ViewGroup类型的时候,并且在MyView中放置一个View的时候,放置的View就会随着手指的移动而移动

自定义View的代码

package com.example.glf.myapplication;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;

/**
 * Created by glf on 17-6-24.
 */

public class MyView extends LinearLayout {
    Scroller scroller = null;
    int lastX = 0;
    int lastY = 0;
    public MyView(Context context) {
        super(context);
        if(scroller==null){
            scroller = new Scroller(context);
        }
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        if(scroller==null){
            scroller = new Scroller(context);
        }
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if(scroller==null){
            scroller = new Scroller(context);
        }
    }



    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int curentX = (int) event.getX();
        int curentY = (int) event.getY();
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                if(!scroller.isFinished()){
                    scroller.abortAnimation();//事件的开始如果上次没有结束就控制结束
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int detalX = curentX - lastX;
                int detalY = curentY - lastY;
                scrollBy(-detalX,-detalY);
                break;
            case MotionEvent.ACTION_UP:
                detalX = curentX - lastX;
                detalY = curentY - lastY;
                scroller.startScroll(getScrollX(),getScrollY(),detalX,detalY,100);
                    invalidate();
                break;
            default:
                break;

        }

        lastX = curentX;
        lastY = curentY;
        return true;
    }

    @Override
    public void computeScroll() {
        if(scroller.computeScrollOffset()){//没有完成
            scrollTo(scroller.getCurrX(),scroller.getCurrY());
            postInvalidate();
        }

    }
}

布局代码

"@+id/imgView"
        android:layout_width="100dp"
        android:layout_height="400dp"
        android:orientation="horizontal"
        android:background="@color/colorPrimary" >
    "@+id/img"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:background="@color/colorAccent"/>
    

你可能感兴趣的:(android)