我想在用戶每次觸摸屏幕時更改圖像。我想讓用戶點擊屏幕,圖像每次都會改變,第5次觸摸時,它會循環回到帶有添加文本的原始圖像。Android:如何更改每次觸摸的圖像
我已經看過
How would I set an ImageButton to change only when my finger is touching it
我曾經嘗試都IUevents和StateListdrawable。我無法弄清楚如何在每次觸摸後實際更改圖像(屏幕中的任何位置)。
我想在用戶每次觸摸屏幕時更改圖像。我想讓用戶點擊屏幕,圖像每次都會改變,第5次觸摸時,它會循環回到帶有添加文本的原始圖像。Android:如何更改每次觸摸的圖像
我已經看過
How would I set an ImageButton to change only when my finger is touching it
我曾經嘗試都IUevents和StateListdrawable。我無法弄清楚如何在每次觸摸後實際更改圖像(屏幕中的任何位置)。
爲什麼你不能使用正常的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.
}
});
您需要創建自定義類,它擴展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
你可以給我他們的樣本嗎? =) – merrill
你有更多的樣本或完成的樣本來測試? =) – merrill
我剛剛爲你做了這個?這不是樣本。你不能讓它工作嗎? –