简体   繁体   English

Android仅Java中可点击的位图图像

[英]Android clickable bitmap image in only java

I am trying to make it so when you click this "Play" bitmap it will run other code the thing is i don't know how to check for it. 我试图做到这一点,所以当您单击此“播放”位图时,它将运行其他代码,原因是我不知道如何检查它。

I tried drawing it as 我尝试将其绘制为

playX = WIDTH / 2;
        playY = HEIGHT / 2;
        Bitmap playButton = BitmapFactory.decodeResource(getResources(), R.drawable.play_button);
        playWidth = playX + playButton.getScaledWidth(canvas);
        playHeight = playY + playButton.getScaledHeight(canvas);

Then a touch Event 然后是触摸事件

@Override
public boolean onTouchEvent(MotionEvent event){
    if(event.getX() > playX && event.getX() < playWidth){
        if(event.getX() > playY && event.getY() < playHeight){
            System.out.println("Button Pushed");
        }
    }}

but it doesn't seem to work i dont know if there is a better way to run code when a bitmap is clicked? 但它似乎不起作用,我不知道单击位图时是否有更好的方法来运行代码? (I am only using the java class not xml) (我只使用java类而不是xml)

Actually you can just use imageView to hold the Play image and set onClickListener function to have a clickable image. 实际上,您可以只使用imageView来保存播放图像,并将onClickListener函数设置为具有可点击的图像。

Something like this: 像这样:

ImageView imageView = findViewById(R.id.image_view_icon);
imageView.setImageResource(R.drawable.play_button);
imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    System.out.println("Button Pushed");
                }
            });

Edit: 编辑:

As mentioned in comments that you are using Canvas drawing, try this instead: 如您在使用Canvas绘图的注释中所述,请尝试以下方法:

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    int x=(int)event.getX();
    int y=(int)event.getY();
    if (drawable.getBounds().contains(x,y)  &&
        event.getAction()==MotionEvent.ACTION_DOWN) {
        System.out.println("Button Pushed");
        return true;
    }
    return false;
}

Please refer to this for details How to make a bitmap using canvas clickable? 有关详细信息,请参阅此内容。 如何使用画布可点击来制作位图?

Hope this is what you want:) 希望这就是你想要的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM