1 package com.android.pump.widget; 2 3 import android.content.Context; 4 import android.graphics.drawable.Drawable; 5 import android.util.AttributeSet; 6 7 import androidx.annotation.AttrRes; 8 import androidx.annotation.DrawableRes; 9 import androidx.annotation.NonNull; 10 import androidx.annotation.Nullable; 11 import androidx.annotation.UiThread; 12 import androidx.appcompat.widget.AppCompatImageView; 13 import androidx.core.content.ContextCompat; 14 15 import com.android.pump.R; 16 17 @UiThread 18 public class PlaceholderImageView extends AppCompatImageView { 19 private static final @DrawableRes int PLACEHOLDER_DRAWABLE = R.drawable.ic_placeholder; 20 PlaceholderImageView(@onNull Context context)21 public PlaceholderImageView(@NonNull Context context) { 22 super(context); 23 initialize(); 24 } 25 PlaceholderImageView(@onNull Context context, @Nullable AttributeSet attrs)26 public PlaceholderImageView(@NonNull Context context, @Nullable AttributeSet attrs) { 27 super(context, attrs); 28 initialize(); 29 } 30 PlaceholderImageView(@onNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr)31 public PlaceholderImageView(@NonNull Context context, @Nullable AttributeSet attrs, 32 @AttrRes int defStyleAttr) { 33 super(context, attrs, defStyleAttr); 34 initialize(); 35 } 36 37 @Override setImageDrawable(@ullable Drawable drawable)38 public void setImageDrawable(@Nullable Drawable drawable) { 39 if (drawable == null) { 40 drawable = ContextCompat.getDrawable(getContext(), PLACEHOLDER_DRAWABLE); 41 } 42 super.setImageDrawable(drawable); 43 } 44 initialize()45 private void initialize() { 46 if (getDrawable() == null) { 47 setImageDrawable(null); 48 } 49 } 50 } 51