Android:如何更改每次触摸的图像
问题描述:
我想在用户每次触摸屏幕时更改图像。我想让用户点击屏幕,图像每次都会改变,第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
+0
你可以给我他们的样本吗? =) – merrill
你有更多的样本或完成的样本来测试? =) – merrill
我刚刚为你做了这个?这不是样本。你不能让它工作吗? –