Android - 使自定义视图的透明区域无法点击

时间:2014-07-29 11:23:56

标签: java android android-view

我有一个圆形的自定义视图。

custom view that has a circle

圆圈有一个触摸侦听器,触摸时会显示动画。 (我将使用红点作为参考。)

responds view when it is touched

但问题是,当我触摸圆圈时,会显示动画,这是没用的。

when it is touched outside

所以,我的问题是,我可以让这个视图在圈内可触摸吗?

3 个答案:

答案 0 :(得分:1)

您可以覆盖视图中的触摸事件,并检查它与圆心的距离是否小于其半径。

这需要这样做,因为Android上的视图基本上都是矩形的。

答案 1 :(得分:1)

我知道这有点晚了,但是它可能会帮助正在寻找答案的人:

  • 首先:从ImageView继承如下:

    public class CustomImageView extends ImageView implements View.OnTouchListener {
    
    
    
     private Bitmap bitmap;
    
     private OnClickListener listener;
    
     private boolean clicking;
    
    
     private int pressedColor;
    
    
    
     public CustomImageView(Context context) {
    
      super(context);
    
      init();
    
     }
    
    
    
     public CustomImageView(Context context, AttributeSet attrs) {
    
      super(context, attrs);
    
      init();
    
     }
    
    
    
     public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    
      super(context, attrs, defStyleAttr);
    
      init();
    
     }
    
    
    
     @Override
    
     public void setOnClickListener(OnClickListener l) {
    
      listener = l;
    
      setOnTouchListener(this);
    
     }
    
    
    
     @Override
    
     public boolean onTouch(View view, MotionEvent motionEvent) {
    
      if (isInClickArea(motionEvent)) {
    
       if (motionEvent.getActionMasked() == MotionEvent.ACTION_DOWN) {
    
        clicking = true;
    
        setColorFilter(pressedColor, PorterDuff.Mode.MULTIPLY);
    
       } else if (motionEvent.getActionMasked() == MotionEvent.ACTION_UP) {
    
        if (clicking) {
    
         clicking = false;
    
         setColorFilter(null);
    
         listener.onClick(view);
    
        }
    
       }
    
       return true;
    
      }
    
      clicking = false;
    
      setColorFilter(null);
    
    
    
      return false;
    
     }
    
    
    
     private boolean isInClickArea(MotionEvent event) {
    
      int clickX = (int)(event.getX() * ((double) bitmap.getWidth() / (double) getWidth()));
    
      int clickY = (int)(event.getY() * ((double) bitmap.getHeight() / (double) getHeight()));
    
      if (bitmap != null) {
    
       if (clickX >= 0 && clickX < bitmap.getWidth() && clickY >= 0 && clickY < bitmap.getHeight()) {
    
        if (Color.alpha(bitmap.getPixel(clickX, clickY)) > 0) {
    
         return true;
    
        }
    
       }
    
      }
    
      return false;
    
     }
    
    
    
     private void init() {
    
      int color = (int)(255 * 0.96 f);
    
      pressedColor = Color.rgb(color, color, color);
    
      Drawable drawable = getDrawable();
    
      if (drawable != null) {
    
       if (drawable instanceof BitmapDrawable) {
    
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    
       } else if (drawable instanceof LayerDrawable) {
    
        bitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888);
    
        drawable.setBounds(0, 0, 128, 128);
    
        drawable.draw(new Canvas(bitmap));
    
       }
    
      }
    
     }
    
    
    
    }
    
  • 第二:在布局中设置一个ImageView:

    <FrameLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
    >
        <com.myapp.custom.CustomImageView 
          android:id="@+id/button_id"
          android:src="@drawable/image_with_transparent_area"
         .../>
    

现在它应该可以根据需要工作。

PS:您可以使用透明图像或在drawables中使用xml文件创建自己的形状,然后使用src normaly导入形状。

快乐编码:)

答案 2 :(得分:-1)

是的,我们可以在屏幕上制作可触摸/可点击的特定视图。您的主屏幕将在布局内,圆圈将作为视图的一部分。启动应用程序时,setcontentView将使布局膨胀,而您的主布局将成为所有其他视图的父级。我们可以使用finviewByID获取视图,我们可以将touchListner / clickListner实现到此视图而不是全屏/布局。这将解决您的问题。

相关问题