summaryrefslogtreecommitdiff
path: root/java/com/android/pump/widget/PlaceholderImageView.java
blob: 61fae76b1a588b08fd359361ce0744378699e8d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.android.pump.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

import androidx.annotation.AttrRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;

import com.android.pump.R;

@UiThread
public class PlaceholderImageView extends AppCompatImageView {
    private static final @DrawableRes int PLACEHOLDER_DRAWABLE = R.drawable.ic_placeholder;

    public PlaceholderImageView(@NonNull Context context) {
        super(context);
        initialize();
    }

    public PlaceholderImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public PlaceholderImageView(@NonNull Context context, @Nullable AttributeSet attrs,
            @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    @Override
    public void setImageDrawable(@Nullable Drawable drawable) {
        if (drawable == null) {
            drawable = ContextCompat.getDrawable(getContext(), PLACEHOLDER_DRAWABLE);
        }
        super.setImageDrawable(drawable);
    }

    private void initialize() {
        if (getDrawable() == null) {
            setImageDrawable(null);
        }
    }
}