【发布时间】:2013-08-16 20:20:44
【问题描述】:
我想知道如何在我的 SurfaceView 类上找到哪个特定图像被触摸,它有四个不同的图像从一个角落移动到另一种轮盘赌风格???
我计划为每张图片启动不同的活动。
-感谢您的帮助。
【问题讨论】:
标签: android canvas surfaceview touch-event
我想知道如何在我的 SurfaceView 类上找到哪个特定图像被触摸,它有四个不同的图像从一个角落移动到另一种轮盘赌风格???
我计划为每张图片启动不同的活动。
-感谢您的帮助。
【问题讨论】:
标签: android canvas surfaceview touch-event
据我所知(仍然有点像 n00b)Canvas 没有“getImage”(或等效的)方法。所以我所做的是在每个图像或位图的尺寸上创建一个Rect。这是我正在做的游戏项目中用于角色控制的屏幕上画布绘制的 d-pad 示例:
public class World extends Activity implements OnTouchListener {
Rect viewRect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewImage1 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.left_key), 50, 50,
true);
viewImage2 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.right_key), 50, 50,
true);
//left,top,right,bottom
viewRect1 = new Rect(viewImage1X, viewImage1Y, viewImage1X+viewImage1Width,
viewImage1Y+viewImage1Height);
//left,top,right,bottom
viewRect2 = new Rect(viewImage2X, viewImage2Y, viewImage2X+viewImage2Width,
viewImage2Y+viewImage2Height);
然后在你的 onTouch() 方法中:
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Rectangle surrounding your view1 is touched
if (x <= viewRect1.right && x >= viewRect1.left
&& y >= viewRect1.top && y <= viewRect1.bottom) {
// Example Activity:
Intent intent = new Intent("com.example.Activity");
startActivity(intent);
}
// Rectangle surrounding your view2 is touched
//if onTouch was NOT in viewRect1, check here. Otherwise will be disregarded
else if (x <= viewRect2.right && x >= viewRect2.left
&& y >= viewRect2.top && y <= viewRect2.bottom) {
// Example Activity:
Intent intent = new Intent("com.example.Activity2");
startActivity(intent);
}
break;
}
return true;
}
【讨论】: