Android ImageButton可点击区域大于图片 如何仅让梯形区域响应点击
核心逻辑是在触摸事件触发时,判断点击位置对应图片的像素透明度:若为透明区域则不响应该次点击,仅非透明的梯形区域响应点击。
方案1:自定义通用不规则形状ImageButton(推荐)
直接继承AppCompatImageButton重写触摸事件逻辑,后续所有不规则形状按钮都可以复用这个控件:
class IrregularImageButton @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AppCompatImageButton(context, attrs, defStyleAttr) { override fun onTouchEvent(event: MotionEvent): Boolean { if (event.action == MotionEvent.ACTION_DOWN) { val drawable = drawable ?: return super.onTouchEvent(event) // 获取控件内点击坐标 val x = event.x.toInt() val y = event.y.toInt() // 转换为drawable对应的坐标(适配centerInside缩放类型) val drawableBounds = drawable.bounds val scale: Float = Math.min( width / drawableBounds.width().toFloat(), height / drawableBounds.height().toFloat() ) val drawableActualWidth = (drawableBounds.width() * scale).toInt() val drawableActualHeight = (drawableBounds.height() * scale).toInt() val leftOffset = (width - drawableActualWidth) / 2 val topOffset = (height - drawableActualHeight) / 2 // 点击在drawable范围外直接不响应 if (x < leftOffset || x > leftOffset + drawableActualWidth || y < topOffset || y > topOffset + drawableActualHeight ) { return false } // 计算点击位置对应drawable的像素坐标 val drawableX = ((x - leftOffset) / scale).toInt() val drawableY = ((y - topOffset) / scale).toInt() // 判断像素透明度,透明则不响应 val pixel = (drawable as BitmapDrawable).bitmap.getPixel(drawableX, drawableY) if (Color.alpha(pixel) == 0) { return false } } return super.onTouchEvent(event) } }
方案2:直接给现有ImageButton设置触摸监听(无需自定义控件)
如果不想单独封装控件,也可以直接给目标ImageButton设置OnTouchListener实现相同逻辑:
btnAgua.setOnTouchListener { v, event -> if (event.action == MotionEvent.ACTION_DOWN) { val btn = v as ImageButton val drawable = btn.drawable ?: return@setOnTouchListener false val x = event.x.toInt() val y = event.y.toInt() val drawableBounds = drawable.bounds val scale: Float = Math.min( btn.width / drawableBounds.width().toFloat(), btn.height / drawableBounds.height().toFloat() ) val drawableActualWidth = (drawableBounds.width() * scale).toInt() val drawableActualHeight = (drawableBounds.height() * scale).toInt() val leftOffset = (btn.width - drawableActualWidth) / 2 val topOffset = (btn.height - drawableActualHeight) / 2 if (x < leftOffset || x > leftOffset + drawableActualWidth || y < topOffset || y > topOffset + drawableActualHeight ) { return@setOnTouchListener false } val drawableX = ((x - leftOffset) / scale).toInt() val drawableY = ((y - topOffset) / scale).toInt() val pixel = (drawable as BitmapDrawable).bitmap.getPixel(drawableX, drawableY) if (Color.alpha(pixel) == 0) { return@setOnTouchListener false } } false }
布局替换示例
把你原布局中的ImageButton标签替换为自定义控件的全类名即可:
<你的包名.IrregularImageButton android:id="@+id/btnAgua" android:layout_width="93dp" android:layout_height="170dp" android:layout_marginStart="28dp" android:layout_marginLeft="28dp" android:adjustViewBounds="true" android:background="@null" android:scaleType="centerInside" android:src="@drawable/agua" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.499" />
注意事项
- 需确保你的梯形图片
@drawable/agua是带透明通道的PNG或矢量图,不能是无透明通道的JPG格式 - 如果修改了
scaleType属性,需要对应调整代码中的坐标转换逻辑,上述代码适配的是你当前使用的centerInside缩放类型 - 如果使用矢量图作为src,需要先把矢量图转换为Bitmap再做像素判断
内容的提问来源于stack exchange,提问作者Isaac Demian
相关产品推荐
相关产品推荐

