較實(shí)用的圖片選擇自定義控件

今天看Google-Simple-Topeka中項(xiàng)目中比較實(shí)用的圖片選擇自定義控件。選中后有個(gè)小的邊框效果。界面簡潔的不得了 ,有興趣可以直接去看項(xiàng)目源碼,這邊做了小的抽取總結(jié)

這里寫圖片描述

自定義實(shí)現(xiàn):

/**
 * A simple view that wraps an avatar.
 */
public class AvatarView extends ImageView implements Checkable {

    private boolean mChecked;
    private static final int NOT_FOUND = 0;

    public AvatarView(Context context) {
        this(context, null);
    }

    public AvatarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AvatarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AvatarView, defStyle, 0);
        try {
            final int avatarDrawableId = a.getResourceId(R.styleable.AvatarView_avatar, NOT_FOUND);
            if (avatarDrawableId != NOT_FOUND) {
                setAvatar(avatarDrawableId);
            }
        } finally {
            a.recycle();
        }
    }

    @Override
    public void setChecked(boolean b) {
        mChecked = b;
        invalidate();
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void toggle() {
        setChecked(!mChecked);
    }

    /**
     * Set the image for this avatar. Will be used to create a round version of this avatar.
     *
     * @param resId The image's resource id.
     */
    @SuppressLint("NewApi")
    public void setAvatar(@DrawableRes int resId) {
        if (ApiLevelHelper.isAtLeast(Build.VERSION_CODES.LOLLIPOP)) {
            setClipToOutline(true);
            setImageResource(resId);
        } else {
            setAvatarPreLollipop(resId);
        }
    }

    private void setAvatarPreLollipop(@DrawableRes int resId) {
        Drawable drawable = ResourcesCompat.getDrawable(getResources(), resId,
                getContext().getTheme());
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        @SuppressWarnings("ConstantConditions")
        RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(),
                bitmapDrawable.getBitmap());
        roundedDrawable.setCircular(true);
        setImageDrawable(roundedDrawable);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        if (mChecked) {
            Drawable border = ContextCompat.getDrawable(getContext(), R.drawable.selector_avatar);
            border.setBounds(0, 0, getWidth(), getHeight());
            border.draw(canvas);
        }
    }

    @Override
    @SuppressLint("NewApi")
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (ApiLevelHelper.isLowerThan(Build.VERSION_CODES.LOLLIPOP)) {
            return;
        }
        if (w > 0 && h > 0) {
            setOutlineProvider(new RoundOutlineProvider(Math.min(w, h)));
        }
    }
}

attrs.xml 自定義屬性

<resources>
    <declare-styleable name="AvatarView">
        <attr name="avatar" format="reference" />
    </declare-styleable>
</resources>

select_avatar.xml 定義了一個(gè)shape選中后的邊界效果

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <stroke
            android:width="@dimen/stroke_width_avatar"
            android:color="@color/topeka_primary" />
    <solid android:color="@android:color/transparent" />
</shape>

使用: AvatarView 控件全路徑

<com.google.samples.apps.topeka.widget.AvatarView
        android:id="@+id/avatar"
 xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="@dimen/size_fab"
        android:layout_height="@dimen/size_fab"
        android:layout_gravity="center"
        android:contentDescription="@string/an_avatar" />

代碼中具體使用:

.......
private List<int> mAvatars = new ArrayList<int>();
mAvatars.add(R.drawable.icon1);
mAvatars.add(R.drawable.icon2);
.......

AvatarView mIcon=(AvatarView)findviewbyid(R.id.avatar);
mIcon.setAvatar(avatars[position]);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容