不规则Button实现有效点击区域

根据颜色判断有效区域(点击三角区域有效,实际图片是正方形,三角形以外都是透明区域,空白区域无效)

在这里插入图片描述

package com.example.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
import android.widget.ImageButton;


/**
 * Created by ybf on 2019/9/20.
 */

public class Mybutton extends android.support.v7.widget.AppCompatImageButton {
    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) {
        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas 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);
			}
		}
 
	}
发布了83 篇原创文章 · 获赞 19 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/weixin_38148680/article/details/101082205