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

时间:2011-09-23 06:36:44

标签: android image button touch uievent

我想在每次用户触摸屏幕时更改图像。我想让用户点击屏幕,每次图像都会改变,在第5次触摸时,它会循环回原始图像并添加文字。

我看了

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

我已经尝试了IUevents和StateListdrawable。我无法弄清楚如何在每次触摸后(屏幕中的任何位置)实际更改图像。

2 个答案:

答案 0 :(得分:1)

您需要创建自定义类,扩展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

答案 1 :(得分:1)

为什么不能使用普通的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.
           }
    });