Android 使用画布使位图可单击

Android 使用画布使位图可单击,android,canvas,clickable,Android,Canvas,Clickable,希望这是完成我的应用程序的最后一步。我需要在画布中创建一个可点击的位图,该位图将调用一个新活动来播放视频mp4,或者在当前活动中播放视频 显示画布和位图的类是我反复使用以显示缩略图的完整图像的类。图像id通过一个Intent传递。以下是完整图像活动的代码,我是一个非常愚蠢的人,使用此网站和其他网站一步一步地拼凑代码,因此如果不干净,我道歉: public class full_image extends Activity { @Override protected void onCreate(

希望这是完成我的应用程序的最后一步。我需要在画布中创建一个可点击的位图,该位图将调用一个新活动来播放视频mp4,或者在当前活动中播放视频

显示画布和位图的类是我反复使用以显示缩略图的完整图像的类。图像id通过一个Intent传递。以下是完整图像活动的代码,我是一个非常愚蠢的人,使用此网站和其他网站一步一步地拼凑代码,因此如果不干净,我道歉:

public class full_image extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(new BitmapView(this));
    getWindow().setBackgroundDrawableResource(R.drawable.bground);
    getWindow().setWindowAnimations(0);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                           WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

class BitmapView extends View {
    public BitmapView(Context context) {
        super(context);
    }

    @Override
    public void onDraw(Canvas canvas) {
        int imgid = getIntent().getIntExtra("Full",0);
        Bitmap fullimage = BitmapFactory.decodeResource(getResources(),imgid);
        Bitmap playButton = BitmapFactory.decodeResource(getResources(), R.drawable.bt_play); //Added for Video
        Display display = getWindowManager().getDefaultDisplay();
        int fpWidth = fullimage.getWidth();
        int fpHeight = fullimage.getHeight();
        int playWidth = playButton.getWidth(); 
        int playHeight = playButton.getHeight(); 
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();
        int leftPoint = screenWidth/2 - fpWidth/2;
        int topPoint = screenHeight/2 - fpHeight/2;
        int leftPlayPoint = screenWidth/2 - playWidth/2; 
        int topPlayPoint = screenHeight/2 - playHeight/2; 
        canvas.drawBitmap(fullimage,leftPoint,topPoint,null);
        canvas.drawBitmap(playButton,leftPlayPoint,topPlayPoint,null); 

    }
}  
}
如果可能的话,我只想用playButton来容纳onClickListener,但是如果更简单的话,如果可能的话,我可以让整个画布都可以点击

我读到另一个问题,其中有人建议使用TouchEvent。我试着走那条路线,但没能使它起作用。这是一条正确的道路吗?我只需要更多地利用它来让它发挥作用

谢谢,J

其他材料:

下面是我在另一个问题中找到的一段代码。我将在何处将此代码放入上面提供的代码中

public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
int x = event.getX()  // or getRawX();
int y = event.getY();

switch(action){
case MotionEvent.ACTION_DOWN:
    if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth())
            && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) {
        //tada, if this is true, you've started your click inside your bitmap
    }
    break;
}

}我想我们不能在画布上点击位图。 但是我们可以使用onTouch方法来实现它。并检查触摸位图是否使用触摸的x-y位置

在onTouch方法中尝试此代码以获得位图

if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth())
                && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) {
            //tada, if this is true, you've started your click inside your bitmap
        }

我将把onTouch方法放在哪里?我试着把它放在OnDraw区域之外,但是我无法引用我的位图。当我试着把它放在OnDraw区域时,我出错了。见上面我的原始问题。我在底部添加了一些附加代码。关于我在哪里添加此代码有什么建议吗?感谢在OnDraw外部和BitmapViewyour类内部创建位图的局部变量…你不知道我现在有多高兴。第一次尝试就成功了。非常感谢你的帮助。