package com.example.a04_circular_bead_imageview.circularbead; import android.annotation.SuppressLint; import android.content.Context; import android.content.res.ColorStateList; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PixelFormat; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Shader; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.LayerDrawable; import android.net.Uri; import android.util.AttributeSet; import android.util.Log; import android.widget.ImageView; import com.example.a04_circular_bead_imageview.R; /** * Created by yujie on 2017/12/1. */ @SuppressLint("AppCompatCustomView") public class SelectableRoundedImageView extends ImageView{ public static final String TAG = "SelectableRoundedImageView"; private int mResource = 0; private static final ScaleType[] sScaleTypeArray = { ScaleType.MATRIX, ScaleType.FIT_XY, ScaleType.FIT_START, ScaleType.FIT_CENTER, ScaleType.FIT_END, ScaleType.CENTER, ScaleType.CENTER_CROP, ScaleType.CENTER_INSIDE }; // Set default scale type to FIT_CENTER, which is default scale type of // original ImageView. private ScaleType mScaleType = ScaleType.FIT_CENTER; private float mLeftTopCornerRadius = 0.0f; private float mRightTopCornerRadius = 0.0f; private float mLeftBottomCornerRadius = 0.0f; private float mRightBottomCornerRadius = 0.0f; private float mBorderWidth = 0.0f; private static final int DEFAULT_BORDER_COLOR = Color.BLACK; private ColorStateList mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR); private boolean isOval = false; private Drawable mDrawable; private float[] mRadii = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 }; public SelectableRoundedImageView(Context context) { super(context); } public SelectableRoundedImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SelectableRoundedImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SelectableRoundedImageView, defStyle, 0); final int index = a.getInt(R.styleable.SelectableRoundedImageView_android_scaleType, -1); if (index >= 0) { setScaleType(sScaleTypeArray[index]); } mLeftTopCornerRadius = a.getDimensionPixelSize( R.styleable.SelectableRoundedImageView_sriv_left_top_corner_radius, 0); mRightTopCornerRadius = a.getDimensionPixelSize( R.styleable.SelectableRoundedImageView_sriv_right_top_corner_radius, 0); mLeftBottomCornerRadius = a.getDimensionPixelSize( R.styleable.SelectableRoundedImageView_sriv_left_bottom_corner_radius, 0); mRightBottomCornerRadius = a.getDimensionPixelSize( R.styleable.SelectableRoundedImageView_sriv_right_bottom_corner_radius, 0); if (mLeftTopCornerRadius < 0.0f || mRightTopCornerRadius < 0.0f || mLeftBottomCornerRadius < 0.0f || mRightBottomCornerRadius < 0.0f) { throw new IllegalArgumentException("radius values cannot be negative."); } mRadii = new float[] { mLeftTopCornerRadius, mLeftTopCornerRadius, mRightTopCornerRadius, mRightTopCornerRadius, mRightBottomCornerRadius, mRightBottomCornerRadius, mLeftBottomCornerRadius, mLeftBottomCornerRadius }; mBorderWidth = a.getDimensionPixelSize( R.styleable.SelectableRoundedImageView_sriv_border_width, 0); if (mBorderWidth < 0) { throw new IllegalArgumentException("border width cannot be negative."); } mBorderColor = a .getColorStateList(R.styleable.SelectableRoundedImageView_sriv_border_color); if (mBorderColor == null) { mBorderColor = ColorStateList.valueOf(DEFAULT_BORDER_COLOR); } isOval = a.getBoolean(R.styleable.SelectableRoundedImageView_sriv_oval, false); a.recycle(); updateDrawable(); } @Override protected void drawableStateChanged() { super.drawableStateChanged(); invalidate(); } @Override public ScaleType getScaleType() { return mScaleType; } @Override public void setScaleType(ScaleType scaleType) { super.setScaleType(scaleType); mScaleType = scaleType; updateDrawable(); } @Override public void setImageDrawable(Drawable drawable) { mResource = 0; mDrawable = SelectableRoundedCornerDrawable.fromDrawable(drawable, getResources()); super.setImageDrawable(mDrawable); updateDrawable(); } @Override public void setImageBitmap(Bitmap bm) { mResource = 0; mDrawable = SelectableRoundedCornerDrawable.fromBitmap(bm, getResources()); super.setImageDrawable(mDrawable); updateDrawable(); } @Override public void setImageResource(int resId) { if (mResource != resId) { mResource = resId; mDrawable = resolveResource(); super.setImageDrawable(mDrawable); updateDrawable(); } } @Override public void setImageURI(Uri uri) { super.setImageURI(uri); setImageDrawable(getDrawable()); } private Drawable resolveResource() { Resources rsrc = getResources(); if (rsrc == null) { return null; } Drawable d = null; if (mResource != 0) { try { d = rsrc.getDrawable(mResource); } catch (Resources.NotFoundException e) { Log.w("NotFound", "Unable to find resource: " + mResource, e); // Don't try again. mResource = 0; } } return SelectableRoundedCornerDrawable.fromDrawable(d, getResources()); } private void updateDrawable() { if (mDrawable == null) { return; } ((SelectableRoundedCornerDrawable) mDrawable).setScaleType(mScaleType); ((SelectableRoundedCornerDrawable) mDrawable).setCornerRadii(mRadii); ((SelectableRoundedCornerDrawable) mDrawable).setBorderWidth(mBorderWidth); ((SelectableRoundedCornerDrawable) mDrawable).setBorderColor(mBorderColor); ((SelectableRoundedCornerDrawable) mDrawable).setOval(isOval); } public float getCornerRadius() { return mLeftTopCornerRadius; } /** * Set radii for each corner. * * @param leftTop The desired radius for left-top corner in dip. * @param rightTop The desired desired radius for right-top corner in dip. * @param leftBottom The desired radius for left-bottom corner in dip. * @param rightBottom The desired radius for right-bottom corner in dip. * */ public void setCornerRadiiDP(float leftTop, float rightTop, float leftBottom, float rightBottom) { final float density = getResources().getDisplayMetrics().density; final float lt = leftTop * density; final float rt = rightTop * density; final float lb = leftBottom * density; final float rb = rightBottom * density; mRadii = new float[] { lt, lt, rt, rt, rb, rb, lb, lb }; updateDrawable(); } public float getBorderWidth() { return mBorderWidth; } /** * Set border width. * * @param width * The desired width in dip. */ public void setBorderWidthDP(float width) { float scaledWidth = getResources().getDisplayMetrics().density * width; if (mBorderWidth == scaledWidth) { return; } mBorderWidth = scaledWidth; updateDrawable(); invalidate(); } public int getBorderColor() { return mBorderColor.getDefaultColor(); } public void setBorderColor(int color) { setBorderColor(ColorStateList.valueOf(color)); } public ColorStateList getBorderColors() { return mBorderColor;}
宁波好的整形医院www.zuanno.com
自定义View圆角ImageView
最新推荐文章于 2024-03-31 11:23:42 发布