【问题标题】:Android: how to change image each touchAndroid:如何在每次触摸时更改图像
【发布时间】:2011-09-23 06:36:43
【问题描述】:

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

我看过

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

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

【问题讨论】:

    标签: android image button touch uievent


    【解决方案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.
               }
        });
    

    【讨论】:

    • 你有更多的样品或完整的样品来测试这个吗? =)
    • 我刚给你做了这个?这不是一个样本。你不能让它工作吗?
    【解决方案2】:

    您需要创建扩展 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
    

    【讨论】:

    • 你能给我样品吗? =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多