使用android中的onTouch将一个图像替换为另一个图像

时间:2014-07-21 20:22:38

标签: android

我是一名使用图形的初学者。现在我有一个图像显示在屏幕上的随机位置。如果触摸图像我想做什么,它应该被另一个图像替换。我怎样才能做到这一点? 我目前在类中使用类的另一件事。谢谢

public class MainActivity extends ActionBarActivity implements OnTouchListener{

MyGameGraphics myGraphics;
TextView tvScore, tvPlayerScore;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tvScore = (TextView)findViewById(R.id.showScore);
    tvPlayerScore = (TextView)findViewById(R.id.playerScore);

    myGraphics = new GraphicsMain(this);

    myGraphics.setOnTouchListener(this);

    setContentView(myGraphics);




    }

@Override
protected void onPause(){

    super.onPause();
    myGraphics.pause();


}

@Override
protected void onResume(){

    super.onResume();
    myGraphics.resume();
}






@Override
public boolean onTouch(View arg0, MotionEvent event) {
    // TODO Auto-generated method stub

    // How to change image using touch event here?

    return false;

}





public class MyGameGraphics extends SurfaceView implements Runnable {

    SurfaceHolder ourHolder;
    Thread ourThread = null;
    Boolean isRunning = false;
    int[] images = new int[]{R.drawable.image1, R.drawable.image2};





    public MyGameGraphics(Context context) {

        super(context);
        ourHolder = getHolder();

    }

      public void pause(){

          isRunning = false;
          while(true){

              try {


                ourThread.join();


            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();


            }

             break; 
          }
          ourThread = null;

      }

      public void resume(){

          isRunning = true;
          ourThread = new Thread(this);
          ourThread.start();

      }


    @Override
    public void run() {



        while(isRunning){

            if(!ourHolder.getSurface().isValid())
             continue;

            Canvas ourCanvas = ourHolder.lockCanvas();
            ourCanvas.drawRGB(0, 0, 0);


            Random rn = new Random();
            int location = rn.nextInt(12);

            for(int i = 0; i < 500; i++){

                try {

            Random rn2 = new Random();
            int generateWidthLocation = rn2.nextInt(420);
            int generateHeightLocation = rn2.nextInt(600);


            Bitmap btmp = BitmapFactory.decodeResource(getResources(), images[location]);
            ourCanvas.drawBitmap(btmp, generateWidthLocation, generateHeightLocation,  null);

                ourThread.sleep(3000);




            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }






            ourHolder.unlockCanvasAndPost(ourCanvas);

        }



             }

}

}

1 个答案:

答案 0 :(得分:0)

在OnTouch方法中,event参数有两个功能:event.getX()event.getY()

然后你只需要为当前位置保留generateWidthLocation和generateHeightLocation,你就可以轻松确定图像是否被触摸(你还需要保持图像的高度/宽度)。

您可能需要检查当用户触摸屏幕,移动手指或停止触摸它时是否调用OnTouch方法:event.getAction()MotionEvent.ACTION_DOWNMotionEvent.ACTION_MOVEMotionEvent.ACTION_UP

编辑:在您的MyGameGraphics中添加以下属性:

    int[] images = new int[] { R.drawable.image1, R.drawable.image2 };
    int currentXImageLocation = 0;
    int currentYImageLocation = 0;
    int currentImageWidth = 0;
    int currentImageHeight = 0;

ourCanvas.drawBitmap(btmp, generateWidthLocation, generateHeightLocation, null);之后添加:

    currentXImageLocation = generateWidthLocation;
    currentYImageLocation = generateHeightLocation;
    currentImageHeight = btmp.getHeight();
    currentImageWidth = btmp.getWidth();

然后在MyGameGraphics类中添加此函数:

    public boolean isImageTouched(float x, float y)
    {
        boolean result = false;

        if(x > currentXImageLocation && x < currentXImageLocation + currentImageWidth 
                && y > currentYImageLocation && y < currentYImageLocation + currentImageHeight)
        {
            result = true;
        }

        return result;
    }

最后在OnTouch方法中:

@Override
public boolean onTouch(View arg0, MotionEvent event) {

    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        if(myGraphics.isImageTouched(event.getX(), event.getY()))
        {
            //Image was touched, do something...
        }
    }

    return true;
}