首先看一下页面布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.zpw.views.exercise7.CameraImageView
android:id="@+id/img_camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"/>
<com.zpw.views.exercise7.NoticeImageView
android:id="@+id/img_notice"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@mipmap/test_alpha_click"/>
</RelativeLayout>
复制代码
test_alpha_click图片:
首先需要做的是判断点击的NoticeImageView位置是否为透明。可以在NoticeImageView中设置监听事件:
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
int pixel = bitmap.getPixel((int) motionEvent.getX(), (int) motionEvent.getY());
if (pixel == 0) {
Toast.makeText(mContext, TAG + "->透明区域", Toast.LENGTH_SHORT).show();
return true;//消费掉事件,不会传递到onClick
}
}
return false;
}
});
复制代码
然后需要将事件传递到CameraImageView。因为CameraImageView与NoticeImageView同属于一个父View,我们可以获取到父View,然后获取CameraImageView的实例。调用对应的方法。
ViewGroup parent = (ViewGroup) getParent();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
if (child.getId() == R.id.img_camera && child instanceof CameraImageView) {
((CameraImageView)child).click(child);
break;
}
}
复制代码
整体代码如下:
NoticeImageView
public class NoticeImageView extends AppCompatImageView {
private final String TAG = "NoticeImageView";
private Context mContext;
public NoticeImageView(Context context) {
this(context, null);
}
public NoticeImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
Bitmap bitmap = ((BitmapDrawable) getDrawable()).getBitmap();
int pixel = bitmap.getPixel((int) motionEvent.getX(), (int) motionEvent.getY());
if (pixel == 0) {
Toast.makeText(mContext, TAG + "->透明区域", Toast.LENGTH_SHORT).show();
ViewGroup parent = (ViewGroup) getParent();
int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
if (child.getId() == R.id.img_camera && child instanceof CameraImageView) {
((CameraImageView)child).click(child);
break;
}
}
return true;
}
}
return false;
}
});
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(mContext, TAG + "->点击事件", Toast.LENGTH_SHORT).show();
}
});
}
}
复制代码
CameraImageView
public class CameraImageView extends AppCompatImageView {
private final String TAG = "CameraImageView";
private Context mContext;
public CameraImageView(Context context) {
this(context, null);
}
public CameraImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public void click(View view) {
Toast.makeText(mContext, TAG + "->点击事件", Toast.LENGTH_SHORT).show();
}
}
复制代码