根据颜色判断有效区域(点击三角区域有效,实际图片是正方形,三角形以外都是透明区域,空白区域无效)
提供了两个自定位view 可以二选一
/**
* 一个点击区域不规则的ImageView,同时实现INVISIBLE状态下可以点击。
* 注意事项: 设置按压效果时要注意,否则点击无法响应
*/
public class IrregularImageView extends android.support.v7.widget.AppCompatImageView {
private static final String TAG = "IrregularImageView";
/**
* 透明图片,在INVISIBLE时设置透明背景,让点击事件有效。
*/
private Bitmap transparentBmp;
private Drawable transparentDrawable;
/**
* 原背景图片,用于VISIBLE时设回,以及获取点击坐标点的像素
*/
private Drawable srcDrawable;
private Bitmap srcBitmap;
private int mVisibility = -1;
public IrregularImageView(Context context) {
super(context);
}
public IrregularImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public IrregularImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@SuppressLint("WrongConstant")
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (transparentBmp == null) {
transparentBmp = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
}
transparentDrawable = new BitmapDrawable(transparentBmp);
srcDrawable = getBackground();
if (srcBitmap == null) {
srcBitmap = drawableToBitmap(srcDrawable);
}
//xml文件中设置的visibility不调用重写的setVisibility,需要转化一下。
if (mVisibility == -1) {
mVisibility = super.getVisibility();
}
super.setVisibility(View.VISIBLE);
setVisibility(mVisibility);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
}
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = null;
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
Bitmap.Config config =
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
bitmap = Bitmap.createBitmap(w, h, config);
//注意,下面三行代码要用到,否则在View或者SurfaceView里的canvas.drawBitmap会看不到图
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
@Override
public void setVisibility(int visibility) {
Log.d(TAG, "setVisibility : " + visibility);
if (visibility == View.INVISIBLE) {
if (transparentBmp != null) {
setBackground(transparentDrawable);
}
mVisibility = View.INVISIBLE;
return;
} else if (visibility == View.VISIBLE) {
if (srcDrawable != null) {
setBackground(srcDrawable);
}
mVisibility = View.VISIBLE;
return;
}
super.setVisibility(visibility);
}
@Override
@ExportedProperty(mapping = {@IntToString(from = 0, to = "VISIBLE"),
@IntToString(from = 4, to = "INVISIBLE"),
@IntToString(from = 8, to = "GONE")})
public int getVisibility() {
if (mVisibility == View.INVISIBLE || mVisibility == View.VISIBLE) {
return mVisibility;
} else {
return super.getVisibility();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
pressed();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
unPressed();
break;
default:
break;
}
//Bitmap bitmap = bitmaps[Integer.parseInt((String)getTag())];
if (event.getX() < 0 || event.getX() > getWidth() || event.getY() < 0 || event.getY() > getHeight()) {
unPressed();
return false;
}
if ((int) event.getX() <= srcBitmap.getWidth() && (int) event.getY() <= srcBitmap.getHeight()) {
if (srcBitmap.getPixel((int) event.getX(), (int) event.getY()) == 0) {
Log.d(TAG, " 透明区域");
unPressed();
return false;
}
}
Log.d(TAG, " 非透明区域");
return super.onTouchEvent(event);
}
private boolean isPressed = false;
private synchronized void pressed() {
if (getVisibility() != View.VISIBLE && !isPressed && isEnabled()) {
//取消按下的反应色,因为它会导致闪一下
//setVisibility(View.VISIBLE);
isPressed = true;
}
}
private synchronized void unPressed() {
if (isPressed && isEnabled()) {
//取消按下的反应色,因为它会导致闪一下
//setVisibility(View.INVISIBLE);
isPressed = false;
}
}
public void recycle() {
if (srcBitmap != null) {
srcBitmap.recycle();
srcBitmap = null;
}
if (transparentBmp != null) {
transparentBmp.recycle();
transparentBmp = null;
}
System.gc();
}
}
/**
* Created by ybf on 2019/9/20.
*/
public class Mybutton extends android.support.v7.widget.AppCompatImageButton {
private Bitmap bitmap;
private Canvas canvas;
public Mybutton(Context context) {
super(context);
}
public Mybutton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Mybutton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (isTouchPointInView(event.getX(), event.getY()) || event.getAction() != MotionEvent.ACTION_DOWN) {
return super.onTouchEvent(event);
} else {
return false;
}
}
protected boolean isTouchPointInView(float localX, float localY) {
if(bitmap == null){
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
canvas = new Canvas(bitmap);
}
draw(canvas);
int x = (int) localX;
int y = (int) localY;
if (x < 0 || x >= getWidth())
return false;
if (y < 0 || y >= getHeight())
return false;
int pixel = bitmap.getPixel(x, y);
if ((pixel & 0xff000000) != 0) { // 点在非透明区
return true;
} else {
return false;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.myapplication.MainActivity">
<com.example.myapplication.Mybutton
android:id="@+id/bt"
android:layout_width="191dp"
android:layout_height="wrap_content"
android:background="@drawable/button2"/>
<com.example.myapplication.Mybutton
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button4"
android:layout_marginStart="12dp"
android:layout_marginTop="106dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
public static class PassThroughButton extends Button {
private Bitmap mBitmap;
public PassThroughButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int color = mBitmap.getPixel((int) event.getX(),
(int) event.getY());
if (color == 0) {
return false;
}
}
return super.onTouchEvent(event);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (w == 0 && h == 0 && oldw == 0 && oldh == 0) {
super.onSizeChanged(w, h, oldw, oldh);
} else {
final StateListDrawable bkg = (StateListDrawable) getBackground();
mBitmap = Bitmap.createScaledBitmap(
((BitmapDrawable) bkg.getCurrent()).getBitmap(),
getWidth(), getHeight(), true);
}
}
}