此位置仅适用于480x800屏幕设备。但是,不要使用1024x600设备。我该如何解决?
sinif = (ImageView) findViewById(R.id.imageSinif);
sinif.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
Toast.makeText(getApplicationContext(),"x= " +x + " y= " +y, Toast.LENGTH_SHORT).show();
if ((610 < event.getX() && event.getX() < 703)
&& (11 < event.getY() && event.getY() < 128)
&& deger == 0) {
// i want run any method in there. for example:
myAnimation.setVisibility(View.INVISIBLE);
sinif.setEnabled(false);
sinif.setVisibility(View.INVISIBLE);
sorulist.setVisibility(View.VISIBLE);
text1.setOnClickListener(dinle);
text2.setOnClickListener(dinle);
}
return false;
}
});
答案 0 :(得分:0)
问题是你要将触摸X和Y的值与硬编码值比较,如601,703,11,128。但由于android规范像素值与设备有关,因此您需要将其转换为与设备相关的大小。
if ((convertSizeToDeviceDependent(610) < event.getX() && event.getX() < convertSizeToDeviceDependent(703))
&& (convertSizeToDeviceDependent(11) < event.getY() && event.getY() < convertSizeToDeviceDependent(128)) && deger == 0) {
// i want run any method in there. for example:
myAnimation.setVisibility(View.INVISIBLE);
sinif.setEnabled(false);
sinif.setVisibility(View.INVISIBLE);
sorulist.setVisibility(View.VISIBLE);
text1.setOnClickListener(dinle);
text2.setOnClickListener(dinle);
}
将此方法放入您的活动
public int convertSizeToDeviceDependent(int value) {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
return ((dm.densityDpi * value) / 160);
}