Get associated image (Drawable) in ImageView android

前端 未结 8 897
心在旅途
心在旅途 2021-02-07 18:34

I have set an image to an ImageView control in android:

iv.setImageResource(R.drawable.image1);

I want to get this associated Drawab

8条回答
  •  -上瘾入骨i
    2021-02-07 18:43

    You can compare drawable resources like

         if (imageView.getDrawable().getConstantState() == 
            ContextCompat.getDrawable(getContext(), R.drawable.image1).getConstantState()) {
    }
    

    BitmapDrawables created from the same resource will for instance share a unique -bitmap stored in their ConstantState https://developer.android.com/reference/android/graphics/drawable/Drawable.ConstantState

    getResources.getDrawable is deprecated so you can use ContextCompat.getDrawable inplace of it.
    

提交回复
热议问题