为什么更改ImageButton的图像资源而不更改按钮的图像?

时间:2016-09-07 12:21:28

标签: android android-imagebutton

我正在为android编写一个记忆游戏应用程序,并且调用ImageButton的setImageResource()方法通常没有任何效果。

所以我的想法是你在屏幕上有一个随机数量的隐藏图像(在这个特殊情况下我们有4个按钮),实现为带有R.drawable.image_default的ImageButtons作为它们的图像资源,当单击“开始”按钮时比赛开始了。当用户单击一个按钮时,它会显示它的图像。当用户点击另一个时,它会显示其背后的图像,并且在短暂的延迟后,可能会发生以下两种情况之一:

  1. 显示的图像相同,它们会从屏幕上消失
  2. 显示的图像不同,因此包含它们的两个按钮都将其图像还原为image_default
  3. 问题在于 - 当用户点击第一个按钮时,调用setImageResource()并且它(按钮)按照预期更改它的图像,但是当用户单击SECOND按钮时(其他三个按钮之一)按钮面朝下),再次调用setImageResource(),但按钮上的图像不会改变

    尽管如此,其他一切都好像发生了变化(意味着如果它们(图像)相同,按钮从屏幕上消失,但如果它们不同,按钮图像就会面朝下翻转(通过图像资源获取模拟)回到image_default))。

    问题是为什么点击第二的按钮不会改变它的图像资源并在屏幕上重新绘制?

    这是我的实施

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;
    
    public class ButtonExample extends AppCompatActivity implements View.OnClickListener {
    
    private ImageButton imageButton0, imageButton1, imageButton2, imageButton3;
    
    private ImageButton firstImage, secondImage;
    private Button buttonStart;
    
    private int[] arrayOfImages = {R.drawable.image0, R.drawable.image0, R.drawable.image1, R.drawable.image1};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_example);
    
        imageButton0 = (ImageButton) findViewById(R.id.imageButton0);
        imageButton0.setOnClickListener(this);
        imageButton1 = (ImageButton) findViewById(R.id.imageButton1);
        imageButton1.setOnClickListener(this);
        imageButton2 = (ImageButton) findViewById(R.id.imageButton2);
        imageButton2.setOnClickListener(this);
        imageButton3 = (ImageButton) findViewById(R.id.imageButton3);
        imageButton3.setOnClickListener(this);
    
        buttonStart = (Button) findViewById(R.id.button_start);
        buttonStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDefaultBackgrounds();
            }
        });
    }
    
    @Override
    public void onClick(View v) {
        //if this is the first image to be shown
        if (firstImage == null) {
            firstImage = (ImageButton) v;
            showImage(firstImage);
        } else {
            //else the first is already showing, so this must be the second image
            secondImage = (ImageButton) v;
    
            //if an already open image has been clicked
            if (firstImage.equals(secondImage)) {
                //just hide the image
                hideImage(firstImage);
            } else {
                /*
                else show the second image.
                (This is where the problem lies, this method gets called but the ImageButton does not get redrawn)
                 */
                showImage(secondImage);
    
                //sleep so to let the user see what image he clicked on
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
                //if the user opened two same images
                if (equalImages(firstImage, secondImage)) {
                    //make the buttons invisible
                    firstImage.setVisibility(View.INVISIBLE);
                    secondImage.setVisibility(View.INVISIBLE);
                } else {
                    //else just hide the images
                    hideImage(firstImage);
                    hideImage(secondImage);
                }
            }
            firstImage = null;
            secondImage = null;
        }
    }
    
    //initialize the button images
    private void setDefaultBackgrounds() {
        imageButton0.setImageResource(R.drawable.image_default);
        imageButton0.setVisibility(View.VISIBLE);
        imageButton0.setTag(arrayOfImages[0]);
    
        imageButton1.setImageResource(R.drawable.image_default);
        imageButton1.setVisibility(View.VISIBLE);
        imageButton1.setTag(arrayOfImages[1]);
    
        imageButton2.setImageResource(R.drawable.image_default);
        imageButton2.setVisibility(View.VISIBLE);
        imageButton2.setTag(arrayOfImages[2]);
    
        imageButton3.setImageResource(R.drawable.image_default);
        imageButton3.setVisibility(View.VISIBLE);
        imageButton3.setTag(arrayOfImages[3]);
    }
    
    //show the image
    private void showImage(ImageButton ib) {
        if (ib.equals(imageButton0)) {
            ib.setImageResource(arrayOfImages[0]);
        } else if (ib.equals(imageButton1)) {
            ib.setImageResource(arrayOfImages[1]);
        } else if (ib.equals(imageButton2)) {
            ib.setImageResource(arrayOfImages[2]);
        } else {
            ib.setImageResource(arrayOfImages[3]);
        }
    }
    
    //are images the same
    private boolean equalImages(ImageButton ib1, ImageButton ib2) {
        return ib1.getTag().equals(ib2.getTag());
    }
    
    //hide the image
    private void hideImage(ImageButton ib) {
        ib.setImageResource(R.drawable.image_default);
    }
    }
    

    这是布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="30dp"
    android:layout_marginEnd="30dp"
    android:layout_marginStart="30dp"
    android:layout_marginTop="30dp"
    android:background="#cdcdcb"
    android:baselineAligned="false"
    android:orientation="vertical"
    tools:context="com.example.schonn.myproject.ButtonExample">
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3">
    
    
        <Button
            android:id="@+id/button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="97dp"
            android:text="@string/start" />
    </RelativeLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
    
        <ImageButton
            android:id="@+id/imageButton0"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true" />
    
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
    
        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true" />
    
        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:adjustViewBounds="true" />
    </LinearLayout>
    
    </LinearLayout>
    

1 个答案:

答案 0 :(得分:0)

我可以建议您解决问题的另一种方法。从你的问题来看,我得到的是当你点击image1时,它应该改为image2,当你点击image2时,它应该改回image1。所以我将使用的方法就是这个

首先将两个图像放在FrameLayout中。然后默认Image2.setVisibility(View.INVISIBLE)使image2不可见。所以你只看到Image1。现在,当您单击Image1时,然后在Image1的onClick侦听器中,将Image1设置为不可见,将Image2设置为Visible。然后在Image2,OnclickListener中,将Image2设置为Invisible,将Image1设置为Visible