Android:如何更改每次触摸的图像

发布于 2024-12-06 06:07:10 字数 329 浏览 1 评论 0原文

我想在用户每次触摸屏幕时更改图像。我想让用户点击屏幕,图像每次都会发生变化,并且在第五次触摸时,它会循环回带有添加文本的原始图像。

我看过

如何设置 ImageButton 仅在手指触摸时才更改

我已经尝试过 IUevents 和 StateListdrawable。我不知道如何在每次触摸后实际更改图像(屏幕上的任何位置)。

I would like to change an image each time the user touches the screen. I would like to have user tap the screen and the image changes each time and at the 5th touch it would loop back to the original image with added text.

I have looked at

How would I set an ImageButton to change only when my finger is touching it

I have tried both IUevents and StateListdrawable. I couldn't figure out how to actually change the image after each touch (anywhere in the screen).

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

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

发布评论

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

评论(2

围归者 2024-12-13 06:07:10

为什么不能使用普通的ImageView呢?这样做:

ArrayList<Integer> picList = new ArrayList<Integer>(); 
// populate the list with the int value of the pictures in drawable.
picList.add(R.drawable.apple);
picList.add(R.drawable.horse);
//etc
int touches = 0;
int loop = 0;
ImageView picture = (ImageView) findViewById(R.id.your_picture);
picture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           if (touches < 4)
                v.setBackgroundDrawable(picList.get(touches));
                touches++;
           } else {
                touches = 0;
                loop = 1;
                v.setBackgroundDrawable(picList.get(touches));
           }
           if (loop = 1){
           // Create a TextView and set the text or make it in the xml with Visible="Gone" and make is visible here.
           }
    });

And why can't you use the normal ImageView? Do this:

ArrayList<Integer> picList = new ArrayList<Integer>(); 
// populate the list with the int value of the pictures in drawable.
picList.add(R.drawable.apple);
picList.add(R.drawable.horse);
//etc
int touches = 0;
int loop = 0;
ImageView picture = (ImageView) findViewById(R.id.your_picture);
picture.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
           if (touches < 4)
                v.setBackgroundDrawable(picList.get(touches));
                touches++;
           } else {
                touches = 0;
                loop = 1;
                v.setBackgroundDrawable(picList.get(touches));
           }
           if (loop = 1){
           // Create a TextView and set the text or make it in the xml with Visible="Gone" and make is visible here.
           }
    });
葵雨 2024-12-13 06:07:10

您需要创建扩展 ImageView 的自定义类,现在将此 imageview 显示到整个屏幕。

> First store the path/location of images into an array/list etc.
> In custom class implement the onTouch event.
> create the counter object in this custom class.
> now in onTouch check in down event that check the counter value with array size if counter==array.length()-1 then set counter as 0 otherwise increment in counter object.
> now get the path of the image and set into the imageview as background

you need to create the Custom class which extends the ImageView now display this imageview to whole screen.

> First store the path/location of images into an array/list etc.
> In custom class implement the onTouch event.
> create the counter object in this custom class.
> now in onTouch check in down event that check the counter value with array size if counter==array.length()-1 then set counter as 0 otherwise increment in counter object.
> now get the path of the image and set into the imageview as background
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文