Home Which Image is attached with ImageView?
Reply: 2

Which Image is attached with ImageView?

Santosh Kumar
1#
Santosh Kumar Published in 2018-01-12 21:43:03Z
if(myImageView.getDrawable() == null) {
    return false;
}
else {
    return true;
}

This tells if an image is attached with ImageView or not. I want to compare the Image which is residing in Drawable and Image which attached with 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;
        }
    }

I have tried like this

anto004
2#
anto004 Reply to 2018-01-12 22:14:02Z
 Drawable d = getContext().getDrawable(R.drawable.your_drawable);
 if(d == imageView.getDrawable()){
     return true;
 }
Nero
3#
Nero Reply to 2018-01-12 22:24:55Z

There are two ways you can do this. First way is you can assign your imageview in your XML with a TAG attribute and use the TAG attribute to determine which image it is. Of course you are not comparing the image here but you are comparing the tag.

XML

<ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:tag="TEST"
        android:src="@drawable/ic_launcher_background"/>

Java Class

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

    }

This will determine the image via the value of the TAG.

However, if you are determined to compare the image itself; then you'll need to convert the images into Bitmap and compare the bitmap against each other. Here's a example:-

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{

    }

Let me know if you need further clarification or if I am wrong.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30516 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO