Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 哪个图像附加了ImageView?_Java_Android - Fatal编程技术网

Java 哪个图像附加了ImageView?

Java 哪个图像附加了ImageView?,java,android,Java,Android,这说明图像是否附加了ImageView。我想比较一下Drawable中的图像和ImageView附带的图像 if(myImageView.getDrawable() == null) { return false; } else { return true; } 我试过这样做有两种方法可以做到这一点。第一种方法是可以在XML中为imageview指定标记属性,并使用标记属性确定它是哪个图像。当然,这里不是比较图像,而是比较标签 private static final int

这说明图像是否附加了ImageView。我想比较一下Drawable中的图像和ImageView附带的图像

if(myImageView.getDrawable() == null) {
    return false;
}
else {
    return true;
}

我试过这样做

有两种方法可以做到这一点。第一种方法是可以在XML中为imageview指定标记属性,并使用标记属性确定它是哪个图像。当然,这里不是比较图像,而是比较标签

private static final int WHITE_PAWN=R.drawable.wp,BLACK_PAWN=R.drawable.bp;
mSetOnClickListener(i,j,chess_array[i][j]);

protected void mSetOnClickListener(final int i,final int j,ImageView v){
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isTroop(i,j)){
                   //for Events on ImageView
                    view.setBackgroundColor(getResources().getColor(R.color.select_troops_color));

                }
            }
        });
    }



protected boolean isTroop(final int i,final int j){
        Drawable d=getBaseContext().getDrawable(BLACK_PAWN);
        if(chess_array[i][j].getDrawable()==d){
            return true;
        }
        else{
            return false;
        }
    }
XML

这将通过标签的值确定图像。

但是,如果您决定比较图像本身;然后,您需要将图像转换为位图,并相互比较位图。下面是一个例子:-

imageView = findViewById(R.id.image);
    if("TEST".equals(imageView.getTag())){

    }

如果您需要进一步澄清或我错了,请告诉我。

您尝试了什么?这些尝试有什么不起作用?我正在尝试制作一个两人的简单国际象棋游戏,因此,我必须检查单击的imageView何时是驻留在我的Drawable中的bishop或rook。我的意思是,你真的尝试做了你想做的事情吗?“当单击imageView时,检查驻留在我的Drawable中的bishop或rook”您尝试过这个吗?如果是这样,告诉我们你尝试了什么——如果没有,你应该先尝试。
<ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="TEST"
        android:src="@drawable/ic_launcher_background"/>
imageView = findViewById(R.id.image);
    if("TEST".equals(imageView.getTag())){

    }
imageView = findViewById(R.id.image);

    final Bitmap myImage = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher_background);
    final Bitmap comparingImg = ((BitmapDrawable) drawable).getBitmap();

    if(myImage.sameAs(comparingImg)){

    }
    else{

    }