2

希望这是完成我的应用程序的最后一步。我需要在可点击的画布中创建一个位图,该位图将调用将播放视频(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;
}

}

4

1 回答 1

5

我认为我们不能在画布中的位图上设置 OnClick。但是我们可以使用 onTouch 方法。并检查位图上的触摸或不使用 xy 触摸位置。

在 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
        }
于 2011-05-25T07:45:16.740 回答