Android: как менять изображение при каждом касании

Я хотел бы менять изображение каждый раз, когда пользователь касается экрана. Я хотел бы, чтобы пользователь нажимал на экран, и изображение каждый раз менялось, а при 5-м касании оно возвращалось к исходному изображению с добавленным текстом.

я посмотрел на

Как бы Я настроил кнопку ImageButton так, чтобы она менялась только тогда, когда мой палец касается ее

Я пробовал как IUevents, так и StateListdrawable. Я не мог понять, как на самом деле менять изображение после каждого касания (в любом месте экрана).


person merrill    schedule 23.09.2011    source источник


Ответы (2)


А почему нельзя использовать обычный 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.
           }
    });
person Finn Larsen    schedule 23.09.2011
comment
у вас есть еще образец или готовый образец, чтобы проверить это? знак равно - person merrill; 23.09.2011
comment
Я только что сделал это для тебя? Это не образец. Вы не можете заставить его работать? - person Finn Larsen; 23.09.2011

вам нужно создать класс Custom, который расширяет 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
person Pratik    schedule 23.09.2011
comment
Можете ли вы дать мне их образцы? знак равно - person merrill; 23.09.2011