【问题标题】:With SurfaceView and 4 Canvas Images How to find which image was touched ?使用 SurfaceView 和 4 个 Canvas 图像如何找到被触摸的图像?
【发布时间】:2013-08-16 20:20:44
【问题描述】:

我想知道如何在我的 SurfaceView 类上找到哪个特定图像被触摸,它有四个不同的图像从一个角落移动到另一种轮盘赌风格???

我计划为每张图片启动不同的活动。

-感谢您的帮助。

【问题讨论】:

    标签: android canvas surfaceview touch-event


    【解决方案1】:

    据我所知(仍然有点像 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;
    }
    

    【讨论】:

    • 非常感谢一位菜鸟对另一位菜鸟的帮助。你能解释一下 if 语句“if (x = bLeftRect.left && y >= bLeftRect.top && y
    • 感谢澄清只是必须确定。我使用以下代码沿着这条路走下去...” if (eventX >= (x - bitmap.getWidth() / 2) && (eventX = (y - bitmap.getHeight() / 2) && (y
    • 不确定你希望“else if”在哪里是完整的语句...“public void handleActionDown(int eventX, int eventY) { if (eventX >= (x - bitmap .getWidth() / 2) && (eventX = (y - bitmap.getHeight() / 2) && (y
    • 已更新答案,希望对您有所帮助。但是问题:你为什么说“如果(eventX> =(x - bitmap.getWidth()/ 2)......这是你的问题。你需要检查eventX是否大于矩形的左侧&&小于围绕您的视图的矩形的右侧。对于 eventY 也是如此
    • 你是我的英雄...乔治·奥威尔、乔纳斯·索尔克、M.L.K.现在是马特马特 !!!!!!! Thaaannnnkkk 你太棒了!!!!!!你抓住了它“这是你的问题......”哈哈哈......它是......你拯救了我的周末。我希望能给你买六包。如果您有贝宝电子邮件,请按我的方式发送。谢谢你的耐心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多