麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

【自定義】帶圓環(huán)和字的圓形ImageView

2019-11-09 16:11:51
字體:
供稿:網(wǎng)友

示例圖:

廢話不多說,直接上代碼

public class CircleImageView extends ImageView {    PRivate static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;    private static final int COLORDRAWABLE_DIMENSION = 2;    private static final int DEFAULT_BORDER_WIDTH = 0;    private static final int DEFAULT_BORDER_COLOR = Color.BLACK;    private static final int DEFAULT_FILL_COLOR = Color.TRANSPARENT;    private static final boolean DEFAULT_BORDER_OVERLAY = false;    private final RectF mDrawableRect = new RectF();    private final RectF mBorderRect = new RectF();    private final Matrix mShaderMatrix = new Matrix();    private final Paint mBitmapPaint = new Paint();    private final Paint mBorderPaint = new Paint();    private final Paint mFillPaint = new Paint();	//畫筆,用于繪制圖形時使用      private Paint paint = new Paint();;    private int mBorderColor = DEFAULT_BORDER_COLOR;    private int mBorderWidth = DEFAULT_BORDER_WIDTH;    private int mFillColor = DEFAULT_FILL_COLOR;    private int mytextSize = 30;    public String mytextContent = "";    private int mytextContentColor = Color.BLACK;     //矩形對象,用于計算文字位置時使用    private Rect rect;    private Bitmap mBitmap;    private BitmapShader mBitmapShader;    private int mBitmapWidth;    private int mBitmapHeight;    private float mDrawableRadius;    private float mBorderRadius;    private ColorFilter mColorFilter;    private boolean mReady;    private boolean mSetupPending;    private boolean mBorderOverlay;    private boolean mDisableCircularTransformation;    public CircleImageView(Context context) {        super(context);        init();    }    public CircleImageView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public CircleImageView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);        mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_civ_border_width, DEFAULT_BORDER_WIDTH);        mBorderColor = a.getColor(R.styleable.CircleImageView_civ_border_color, DEFAULT_BORDER_COLOR);        mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_civ_border_overlay, DEFAULT_BORDER_OVERLAY);        mFillColor = a.getColor(R.styleable.CircleImageView_civ_fill_color, DEFAULT_FILL_COLOR);        mytextSize = a.getDimensionPixelSize(R.styleable.CircleImageView_mytextSize,30);        mytextContentColor = a.getColor(R.styleable.CircleImageView_mytextContent_color,DEFAULT_BORDER_COLOR);        a.recycle();        init();    }    private void init() {        super.setScaleType(SCALE_TYPE);        mReady = true;        if (mSetupPending) {            setup();            mSetupPending = false;        }    }    @Override    public ScaleType getScaleType() {        return SCALE_TYPE;    }    @Override    public void setScaleType(ScaleType scaleType) {        if (scaleType != SCALE_TYPE) {            throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));        }    }    @Override    public void setAdjustViewBounds(boolean adjustViewBounds) {        if (adjustViewBounds) {            throw new IllegalArgumentException("adjustViewBounds not supported.");        }    }    @Override    protected void onDraw(Canvas canvas) {        //將畫筆的文字大小設置為我們定義的大小        paint.setTextSize(mytextSize);        paint.setColor(mytextContentColor);        rect = new Rect();        /**         * 此方法可以獲得文字所在的矩形區(qū)域,并賦給rect         * 參數(shù)1:傳入文字的內(nèi)容         * 參數(shù)2:傳入文字起始的長度,一般為0         * 參數(shù)3:傳入文字結(jié)束的長度,一般為text.length         * 參數(shù)4:傳入一個Rect矩形對象         */        if (mDisableCircularTransformation) {            super.onDraw(canvas);            return;        }        if (mBitmap == null) {            return;        }        if (mFillColor != Color.TRANSPARENT) {            canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mFillPaint);        }        canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mBitmapPaint);        if(!"".equals(mytextContent)){            paint.getTextBounds(mytextContent, 0, mytextContent.length(), rect);            canvas.drawText(mytextContent, getWidth() / 2 - rect.width() / 2, getHeight() / 2 + rect.height() / 2, paint);        }        if (mBorderWidth > 0) {            canvas.drawCircle(mBorderRect.centerX(), mBorderRect.centerY(), mBorderRadius, mBorderPaint);        }    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        setup();    }    @Override    public void setPadding(int left, int top, int right, int bottom) {        super.setPadding(left, top, right, bottom);        setup();    }    @Override    public void setPaddingRelative(int start, int top, int end, int bottom) {        super.setPaddingRelative(start, top, end, bottom);        setup();    }    public int getBorderColor() {        return mBorderColor;    }    public void setBorderColor(@ColorInt int borderColor) {        if (borderColor == mBorderColor) {            return;        }        mBorderColor = borderColor;        mBorderPaint.setColor(mBorderColor);        invalidate();    }    /**     * @deprecated Use {@link #setBorderColor(int)} instead     */    @Deprecated    public void setBorderColorResource(@ColorRes int borderColorRes) {        setBorderColor(getContext().getResources().getColor(borderColorRes));    }    /**     * Return the color drawn behind the circle-shaped drawable.     *     * @return The color drawn behind the drawable     *     * @deprecated Fill color support is going to be removed in the future     */    @Deprecated    public int getFillColor() {        return mFillColor;    }    /**     * Set a color to be drawn behind the circle-shaped drawable. Note that     * this has no effect if the drawable is opaque or no drawable is set.     *     * @param fillColor The color to be drawn behind the drawable     *     * @deprecated Fill color support is going to be removed in the future     */    @Deprecated    public void setFillColor(@ColorInt int fillColor) {        if (fillColor == mFillColor) {            return;        }        mFillColor = fillColor;        mFillPaint.setColor(fillColor);        invalidate();    }    /**     * Set a color to be drawn behind the circle-shaped drawable. Note that     * this has no effect if the drawable is opaque or no drawable is set.     *     * @param fillColorRes The color resource to be resolved to a color and     *                     drawn behind the drawable     *     * @deprecated Fill color support is going to be removed in the future     */    @Deprecated    public void setFillColorResource(@ColorRes int fillColorRes) {        setFillColor(getContext().getResources().getColor(fillColorRes));    }    public int getBorderWidth() {        return mBorderWidth;    }    public void setBorderWidth(int borderWidth) {        if (borderWidth == mBorderWidth) {            return;        }        mBorderWidth = borderWidth;        setup();    }    public boolean isBorderOverlay() {        return mBorderOverlay;    }    public void setBorderOverlay(boolean borderOverlay) {        if (borderOverlay == mBorderOverlay) {            return;        }        mBorderOverlay = borderOverlay;        setup();    }    public boolean isDisableCircularTransformation() {        return mDisableCircularTransformation;    }    public void setDisableCircularTransformation(boolean disableCircularTransformation) {        if (mDisableCircularTransformation == disableCircularTransformation) {            return;        }        mDisableCircularTransformation = disableCircularTransformation;        initializeBitmap();    }    @Override    public void setImageBitmap(Bitmap bm) {        super.setImageBitmap(bm);        initializeBitmap();    }    @Override    public void setImageDrawable(Drawable drawable) {        super.setImageDrawable(drawable);        initializeBitmap();    }    @Override    public void setImageResource(@DrawableRes int resId) {        super.setImageResource(resId);        initializeBitmap();    }    @Override    public void setImageURI(Uri uri) {        super.setImageURI(uri);        initializeBitmap();    }    @Override    public void setColorFilter(ColorFilter cf) {        if (cf == mColorFilter) {            return;        }        mColorFilter = cf;        applyColorFilter();        invalidate();    }    @Override    public ColorFilter getColorFilter() {        return mColorFilter;    }    private void applyColorFilter() {        if (mBitmapPaint != null) {            mBitmapPaint.setColorFilter(mColorFilter);        }    }    private Bitmap getBitmapFromDrawable(Drawable drawable) {        if (drawable == null) {            return null;        }        if (drawable instanceof BitmapDrawable) {            return ((BitmapDrawable) drawable).getBitmap();        }        try {            Bitmap bitmap;            if (drawable instanceof ColorDrawable) {                bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);            } else {                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);            }            Canvas canvas = new Canvas(bitmap);            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());            drawable.draw(canvas);            return bitmap;        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    private void initializeBitmap() {        if (mDisableCircularTransformation) {            mBitmap = null;        } else {            mBitmap = getBitmapFromDrawable(getDrawable());        }        setup();    }    private void setup() {        if (!mReady) {            mSetupPending = true;            return;        }        if (getWidth() == 0 && getHeight() == 0) {            return;        }        if (mBitmap == null) {            invalidate();            return;        }        mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);        mBitmapPaint.setAntiAlias(true);        mBitmapPaint.setShader(mBitmapShader);        mBorderPaint.setStyle(Paint.Style.STROKE);        mBorderPaint.setAntiAlias(true);        mBorderPaint.setColor(mBorderColor);        mBorderPaint.setStrokeWidth(mBorderWidth);        mFillPaint.setStyle(Paint.Style.FILL);        mFillPaint.setAntiAlias(true);        mFillPaint.setColor(mFillColor);        mBitmapHeight = mBitmap.getHeight();        mBitmapWidth = mBitmap.getWidth();        mBorderRect.set(calculateBounds());        mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2.0f, (mBorderRect.width() - mBorderWidth) / 2.0f);        mDrawableRect.set(mBorderRect);        if (!mBorderOverlay && mBorderWidth > 0) {            mDrawableRect.inset(mBorderWidth - 1.0f, mBorderWidth - 1.0f);        }        mDrawableRadius = Math.min(mDrawableRect.height() / 2.0f, mDrawableRect.width() / 2.0f);        applyColorFilter();        updateShaderMatrix();        invalidate();    }    private RectF calculateBounds() {        int availableWidth  = getWidth() - getPaddingLeft() - getPaddingRight();        int availableHeight = getHeight() - getPaddingTop() - getPaddingBottom();        int sideLength = Math.min(availableWidth, availableHeight);        float left = getPaddingLeft() + (availableWidth - sideLength) / 2f;        float top = getPaddingTop() + (availableHeight - sideLength) / 2f;        return new RectF(left, top, left + sideLength, top + sideLength);    }    private void updateShaderMatrix() {        float scale;        float dx = 0;        float dy = 0;        mShaderMatrix.set(null);        if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {            scale = mDrawableRect.height() / (float) mBitmapHeight;            dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;        } else {            scale = mDrawableRect.width() / (float) mBitmapWidth;            dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;        }        mShaderMatrix.setScale(scale, scale);        mShaderMatrix.postTranslate((int) (dx + 0.5f) + mDrawableRect.left, (int) (dy + 0.5f) + mDrawableRect.top);        mBitmapShader.setLocalMatrix(mShaderMatrix);    }}

attrs文件:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="CircleImageView">        <attr name="civ_border_width" format="dimension" />        <attr name="civ_border_color" format="color" />        <attr name="mytextContent_color" format="color" />        <attr name="civ_border_overlay" format="boolean" />        <attr name="civ_fill_color" format="color" />        <attr name="mytextSize" format="dimension" type="dimension"></attr>    </declare-styleable></resources>

使用方法:

layout.xml里面:	<com....CircleImageView>  	    android:id="@+id/tou"            android:layout_width="100dp"            android:layout_height="100dp"            android:src="@mipmap/a"            android:layout_marginTop="20dp"            android:layout_centerHorizontal="true"            app:civ_border_width="2dp"            app:civ_border_color="@color/colorAccent"            app:mytextSize="30sp"            app:mytextContent_color="@color/colorAccent"	</com....CircleImageView>activity里面:  CircleImageView iv = findv...(R.id.tou);  iv.setImageView()或者其他方法,設置圖片  iv.mytextContect = "張三"


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: asiass极品裸体女pics | 日本免费不卡一区二区 | 91久久国产综合精品女同国语 | 青青草免费观看完整版高清 | 久久久资源网 | 在线播放中文 | 免费网站看v片在线a | 久久久久久久久久久久免费 | 中文字幕 亚洲一区 | 欧美人xx| 成人福利视频在线观看 | 国产一级毛片网站 | 成人福利视频 | 在线免费观看麻豆 | 日韩视频一区 | 国产亚洲精品综合一区91555 | 亚洲第五色综合网 | 日韩色视频 | 羞羞视频免费观看网站 | 国产羞羞视频在线观看免费应用 | 激情视频免费观看 | 麻豆视频在线观看免费网站 | 久草在线高清 | 精品久久久久久久久久久aⅴ | 欧美中文字幕一区二区三区亚洲 | 日韩精品免费看 | 国产成人精品一区二区仙踪林 | 日美av在线| 91网站在线观看视频 | 国产精品视频六区 | 在线成人精品视频 | 午夜色视频在线观看 | 欧美成人免费香蕉 | 中文字幕一区2区 | 国产69精品久久久久久 | 国产日韩久久久久69影院 | 成人爽a毛片免费啪啪红桃视频 | 国产免费一区二区三区最新不卡 | 久久免费视频一区 | 日韩一级片一区二区三区 | 一本到免费视频 |