首先看下效果圖:
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;
}
}