已更新
此更新旨在澄清原始问题。我创建了自定义视图以制作可点击的点。我这样做是为了确保我可以始终将点放在不同屏幕尺寸的精确位置。即使自定义视图在屏幕的一小部分上只有一个点,它也会使整个屏幕可以点击,因此无法单击其他部分。我在屏幕上放了两个点,但由于一个点的视图实际上占据了整个屏幕,因此无法点击另一个点。如何将自定义视图的可点击区域限制为仅在其上绘制点的部分?
以下是相关代码:
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);
}
}
答案 0 :(得分: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);