一招叫你如何绘制图片跟着手势滑动

首先看下效果图:
一招叫你如何绘制图片跟着手势滑动_第1张图片
1,实现指定区域内触摸后显示图片,超出指定区域则不显示图片
2,滑动时超过指定区域,不显示图片
3,手指放开,图片隐藏,手指触摸图片显示
实现逻辑,自定义一个view,通过onTouchEvent事件,判定触摸的坐标,然后不断调用invalidate方法绘制图片实现,逻辑非常的简单。代码也是非常的简单。
贴核心代码

package com.hitv.dialogdemo;

/**
 * Created by zzl on 2018/3/16 0016.
 */
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**

 * @author zzl
 *
 */
public class TouchMoveView extends View {

    private String TAG = "TouchMoveView";
    /**
     * the default bitmap for the TouchMoveView
     */
    private Bitmap defaultBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    /**
     * the width of the default bitmap
     */
    private int width = defaultBitmap.getWidth();

    /**
     * the height of the default bitmap
     */
    private int height = defaultBitmap.getHeight();

    /**
     * the x-Location of the bitmap
     */
    private float xLocation = 0;
    /**
     * the y-Location of the bitmap
     */
    private float yLocation = 0;
    private boolean mIsup;
    private int mLeft=0;
    private int mTop=0;
    private float mRight=0;
    private int mBottom=0;
    private int outDistance=-600;

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

    public TouchMoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Matrix matrix = new Matrix();
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(defaultBitmap, xLocation-width/2, yLocation-height/2, null);
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.d(TAG, "onTouchEvent: "+"  "+mLeft+"  "+mTop+" "+mRight+" "+mBottom+" " +event.getY()+" "+event.getX());
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float x = event.getX();
                float y = event.getY();
                if (mRight!=0&&mLeft <= x && x <= mRight && mTop <= y && y <= mBottom) {
                    //continue
                    Log.d(TAG, "onTouchEvent: ss");
                    xLocation = event.getX();
                    yLocation =  event.getY();
                } else {
                    xLocation = outDistance;
                    yLocation = outDistance;  //end the event,目的是将图片隐藏起来
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float xMove = event.getX();
                float yMove = event.getY();
                if (mLeft <= xMove && xMove <= mRight && mTop <= yMove && yMove <= mBottom) {
                    xLocation = xMove;
                    yLocation = yMove;//移动过程中超过这个趋于就将它隐藏起来
                } else {
                    xLocation = outDistance;
                    yLocation = outDistance;
                }
//                xLocation =event.getX();
//                yLocation =  event.getY();
                break;
            case MotionEvent.ACTION_UP:  //如果up就将图片绘制出去
                xLocation = outDistance;
                yLocation = outDistance;
                break;
        }
        invalidate();
        return true;
    }

    public Bitmap getDefaultBitmap() {
        return defaultBitmap;
    }

    public void setDefaultBitmap(Bitmap defaultBitmap) {
        this.defaultBitmap = defaultBitmap;
        //update the width and the height of the default bitmap
        width = defaultBitmap.getWidth();
        height = defaultBitmap.getHeight();
    }

    public float getxLocation() {
        return xLocation;
    }

    /**
     * set the initialized X-Location
     * @param  xLocation
     */
    public void setxLocation(float xLocation) {
        this.xLocation = xLocation;
    }

    public float getyLocation() {
        return yLocation;
    }

    /**
     * set the initialized y-Location
     * @param  yLocation
     */
    public void setyLocation(float yLocation) {
        this.yLocation = yLocation;
    }

    /**
     * 该方法主要是限制坐标的x,y值
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    public void setlocationReal(int left, int top, float right, int bottom) {

        mLeft = left;
        mTop = top;
        mRight = right;
        mBottom = bottom;
    }
}

你可能感兴趣的:(安卓技术,自定义view,手势识别)