【问题标题】:Android - Custom view with custom button makes entire screen clickable instead of just in button areaAndroid - 带有自定义按钮的自定义视图使整个屏幕可点击,而不仅仅是在按钮区域
【发布时间】:2016-06-26 04:08:16
【问题描述】:

更新

此更新旨在澄清原始问题。我创建了自定义视图来制作可点击的点。我这样做是为了确保我总是可以将点定位在不同屏幕尺寸的确切位置。尽管自定义视图在屏幕的一小部分上只有一个点,但它使整个屏幕都可以点击,因此其他部分无法点击。我在屏幕上放置了两个点,但由于一个点的视图实际上占据了整个屏幕,因此无法单击另一个点。如何将自定义视图的可点击区域限制为仅绘制了点的部分?

以下是相关代码:

public class MainActivity extends AppCompatActivity {

final float dotScale = 0.3f;
Dot dot1, dot2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    RelativeLayout myLayout = (RelativeLayout) findViewById(R.id.mainView);
    MyView myView = new MyView(this);
    myLayout.addView(myView);

    // Two dots are created.

    dot1 = new Dot(this);
    dot1.xOffset = 2.9f;
    dot1.yOffset = 3.3f;
    myLayout.addView(dot1);
    dot2 = new Dot(this);
    dot2.xOffset = -2.4f;
    dot2.yOffset = 1.1f;
    myLayout.addView(dot2);

    // Makes dots clickable

    dot1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!dot1.isClicked) {
                dot1.animate().setDuration(300).setInterpolator(new AnticipateInterpolator())
                        .scaleXBy(dotScale).scaleYBy(dotScale).alpha(1.0f);
                dot1.isClicked = true;
            }
        }
    });

    dot2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (!dot2.isClicked) {
                dot2.animate().setDuration(300).setInterpolator(new AnticipateInterpolator())
                        .scaleXBy(dotScale).scaleYBy(dotScale).alpha(1.0f);
                dot2.isClicked = true;
            }
        }
    });

}

.......

//custom view for Dots

class Dot extends View {

    int radius;
    float xOffset;
    float yOffset;
    boolean isClicked = false;

    public Dot(Context context) {
        super(context);
        setClickable(true);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int x = getWidth();
        int y = getHeight();
        double ratio = (547d / 828d);
        float circleX = (float)((x / 2) - (y * ratio) / xOffset);
        float circleY = (float)(y / yOffset);

        radius = (int)((float)y/13);
        setPivotX(circleX);
        setPivotY(circleY);

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawCircle(circleX, circleY, radius, paint);
    }


}

【问题讨论】:

    标签: java android android-layout


    【解决方案1】:

    我解决了自己的问题。用于创建每个点的视图占据了整个屏幕,即使某些部分是透明的。有一种方法可以使点击透明区域时不会注册。但是,这不是一个好的解决方案。我解决问题的方法是放弃自定义视图的想法并切换到标准视图。在我无法让标准视图在不同的屏幕尺寸上正确排列之前。但是,在移除顶部菜单栏后,计算变得更容易了,并且我能够进行计算,因此点始终会在所有屏幕尺寸上正确显示。下面是我用来创建允许两者都可点击的点的代码:

        //get screen size
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        width = metrics.widthPixels;
        height = metrics.heightPixels;
    
        //create dots
        dot1 = new View(this);
        dot2 = new View(this);
        dot1.setBackgroundResource(R.drawable.circle);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(height/7, height/7);
        params.leftMargin = (int)((width / 2) - height * ratio / 2.6f);
        params.topMargin = (int)(height / 4.6f);
        myLayout.addView(dot1, params);
    
    
        dot2.setBackgroundResource(R.drawable.circle);
        RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(height/7, height/7);
        params2.leftMargin = (int)((width / 2) - height * ratio / -4.6f);
        params2.topMargin = (int)(height / 1.4f);
        myLayout.addView(dot2, params2);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      相关资源
      最近更新 更多