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

@(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();
        }

    }
}

布局代码

<com.example.glf.myapplication.MyView
        android:id="@+id/imgView"
        android:layout_width="100dp"
        android:layout_height="400dp"
        android:orientation="horizontal"
        android:background="@color/colorPrimary" >
    <ImageView
        android:id="@+id/img"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:background="@color/colorAccent"/>
    </com.example.glf.myapplication.MyView>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值