使用画布使位图可点击

发布于 2024-11-09 10:52:12 字数 2568 浏览 0 评论 0原文

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

    }
}  
}

如果可能,我只想播放按钮容纳 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;
}

}

Hopefully this is the last step in completing my app. I need to make a bitmap within a canvas clickable that will either call a new activity that will play a video (mp4) or within the current activity play the video.

The class that displays the canvas and bitmaps is a class I use over and over to display a full image of a thumbnail. The image id is passed through an Intent. Here is the code for the full image activity (I'm very much a noob and pieced my code together one step at a time using this site and others so I apologize if it's not clean):

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); 

    }
}  
}

If possible, I would like just the playButton to house the onClickListener but if it's easier, I'm Ok with making the whole canvas clickable (if that's even possible).

I read on another question where there was a suggestion to use TouchEvent. I tried going that route but could not get it to work. Is this the right path and I just need to play around with it more to get it to work?

Thanks, J

Additional Stuff:

here's a snippet of code I found in another question. Where would I put this code into the code provided above.

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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

计㈡愣 2024-11-16 10:52:12

我认为我们不能在画布中的位图上设置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
        }

I think we can't setOnClick on bitmap in canvas.
But we can use onTouch method for it. and check touch on bitmap or not using x-y position of touch.

try this code in onTouch method for get touch on bitmap...

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
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文