如何将按钮图像与另一个图像进行比较以查看它们是否相等?

时间:2014-11-19 14:12:16

标签: android image matching

我想检查图像按钮的图像是否等于另一个图像。这就是我一直在尝试的。

if(String.valueOf(image1.getId()) == (selectedQ.image))){
                ResultAlertFragment alertDialog = new ResultAlertFragment();
                alertDialog.show(getFragmentManager(), "list alert");
                }
                else if(!(String.valueOf(image1.getId()) == pictureCorrect)){
                    ResultAlertFragmentIncorrect alertDialog = new ResultAlertFragmentIncorrect          ();
                    alertDialog.show(getFragmentManager(), "list alert");
                }

这是我的图像选择Q设置

RandomImage[] questionArray = { imageBoy, imageGirl, imageMan,
            imageWoman, imageCow, imageDog, imageCat, imageFox };

    // random int for Button
    Random randomeQuestion = new Random(System.currentTimeMillis());

    int nextQuestion = randomeQuestion.nextInt((questionArray.length) - 1);
    selectedQ.person = questionArray[nextQuestion].person;
    selectedQ.image = questionArray[nextQuestion].image;
    selectedQ.displayed = questionArray[nextQuestion].displayed;

    // Selecting a random question
    Random randomerSelection = new Random(System.currentTimeMillis());
    int randomposition = randomerSelection.nextInt(3);
    ImageView imagePosition = (ImageView) findViewById(imageViews[randomposition]);
    imagePosition.setImageResource(selectedQ.image);

    ArrayList<Integer> imagesAlreadyInUse = new ArrayList<Integer>();
    for (int i = 0; i < 4; i++) {
        if (!(i == randomposition)) {
            int nextImage = 0;
            Random randomImageForButtons = new Random(
                    System.currentTimeMillis());

            Boolean condition = false;
            while (condition == false) {
                nextImage = randomImageForButtons
                        .nextInt((questionArray.length) - 1);
                if (!(nextImage == nextQuestion)) {
                    if (!(imagesAlreadyInUse
                            .contains(questionArray[nextImage].image))) {
                        imagesAlreadyInUse
                                .add(questionArray[nextImage].image);
                        condition = true;
                    }
                }
            }

            int v = imageViews[i];
            ImageView iv = (ImageView) findViewById(v);
            iv.setImageResource(questionArray[nextImage].image);
            questionArray[nextImage].displayed = true;
        }
    }

    tv1.setText(selectedQ.person);
    pictureCorrect = String.valueOf(selectedQ.image);

我的应用向用户询问了一个问题。这个问题是一个有图片和字符串的问题对象。用户必须从4个中选择最适合该问题的1张图片。这些是随机生成的。 我不确定如何检查它们是否正确,即他们选择的图片与问题相符。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您无法跟踪imageview的可绘制资源。See this 但作为一种解决方法,你可以使用标签

做这样的事情
imageView0 = (ImageView) findViewById(R.id.imageView0);
imageView0.setImageResource(R.drawable.image0);
imageView0.setTag(R.drawable.image0);

imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView0.setImageResource(R.drawable.image1);
imageView1.setTag(R.drawable.image1);

然后您可以比较代码中的代码,例如

if( view1.getTag() == view2.getTag())
{
  //do something
}