Back to project page RoundImageView.
The source code is released under:
Apache License
If you think the Android project RoundImageView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* * Copyright (C) 2015 The Android Open Source Project. */*from w ww . jav a 2 s . co m*/ * yinglovezhuzhu@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.opensource.widget; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.PorterDuff.Mode; import android.graphics.PorterDuffXfermode; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.TransitionDrawable; import android.util.AttributeSet; import android.widget.ImageView; /** * * ???????ImageView??????????????????<br> * * <p>??????ImageView?????????????{@link Drawable}?<br> * ??{@link ImageView#getDrawable()}?????{@link Drawable}?<br> * ???{@link BitmapDrawable#getPaint()}????????<br> * ??saveLayer?????????????????<br> * ?????{@link Paint#setXfermode(android.graphics.Xfermode)}??PorterDuffXfermode?<br> * ??????????????<br> * <p>?????????ImageView?scaleType<br> * <p>???<br> * 1??????????????onDraw????ui???PorterDuffXfermode???SRC_IN????????????? * ??????????????????????????????????Anr?<br><br> * 2???????????????????????????????????{@link BitmapDrawable}?????????xml?????? * ??????{@link ImageView#setImageBitmap(android.graphics.Bitmap)}??{@link ImageView#setImageResource(int)} * ?????????????????????????????{@link ImageView#setImageDrawable(Drawable)}????????, * ?????????{@link BitmapDrawable} ??????????????????????????????????????? * ??????????{@link TransitionDrawable}???????????????? * * <p><font color="#FF0000"> * ?????????????????????????????? * ???????????????{@link ImageView.ScaleType#CENTER_CROP},?????????????????? * ??????????????????????????????????????????????????????? * ????????????????????????????????? * * </font> * * @author yinglovezhuzhu@gmail.com * * @version 1.0 * */ public class RoundImageView extends BaseRoundImageView { private final PorterDuffXfermode mModeSrcIn = new PorterDuffXfermode(Mode.SRC_IN); private final PorterDuffXfermode mModeDstOver = new PorterDuffXfermode(Mode.DST_OVER); public RoundImageView(Context context) { super(context); } public RoundImageView(Context context, AttributeSet attrs) { super(context, attrs); } public RoundImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { if(mCornerRate <= DEFAULT_CORNER_RATE && mCornerRadius <= DEFAULT_CORNER_RADIUS) { // ?????????????????????? super.onDraw(canvas); return; } Drawable drawable = this.getDrawable(); if (drawable == null) { super.onDraw(canvas); return; } if(mViewWidth <= 0 || mViewHeight <= 0) { super.onDraw(canvas); return; } if (drawable instanceof BitmapDrawable) { int diameter = mViewWidth > mViewHeight ? mViewHeight : mViewWidth; Paint paint = ((BitmapDrawable) drawable).getPaint(); paint.setAntiAlias(true); paint.setStyle(Style.FILL); mDrawRect.set(0, 0, mViewWidth, mViewHeight); int saveCount = canvas.saveLayerAlpha(mDrawRect, 255, Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG); canvas.drawARGB(0, 0, 0, 0); float radius = 0; if(mCornerRate > DEFAULT_CORNER_RATE) { // ???? radius = ((float) diameter) / mCornerRate; } else if(mCornerRadius > DEFAULT_CORNER_RADIUS) { // ??????? float halfDiameter = ((float) diameter) / 2; // ???????????????????????????????????? radius = mCornerRadius > halfDiameter ? halfDiameter : mCornerRadius; } else { // ???????????? super.onDraw(canvas); return; } if(mBorderThickness != DEFAUTL_BORDER_THICKNESS) { // ???? float drawableRadius = radius - mBorderThickness; paint.setColor(mFillColor); mTempRect.set(mDrawRect.left + mBorderThickness, mDrawRect.top + mBorderThickness, mDrawRect.right - mBorderThickness, mDrawRect.bottom - mBorderThickness); canvas.drawRoundRect(mTempRect, drawableRadius, drawableRadius, paint); paint.setXfermode(mModeSrcIn); super.onDraw(canvas); paint.setXfermode(mModeDstOver); paint.setColor(mBorderColor); canvas.drawRoundRect(mDrawRect, radius, radius, paint); } else if(mBorderInsideThickness != DEFAUTL_BORDER_THICKNESS || mBorderOutsideThickness != DEFAUTL_BORDER_THICKNESS) { // ???? float drawableRadius = radius - mBorderInsideThickness - mBorderOutsideThickness; if(mBorderInsideThickness == DEFAUTL_BORDER_THICKNESS) { // ?????? paint.setColor(mFillColor); mTempRect.set(mDrawRect.left + mBorderOutsideThickness, mDrawRect.top + mBorderOutsideThickness, mDrawRect.right - mBorderOutsideThickness, mDrawRect.bottom - mBorderOutsideThickness); canvas.drawRoundRect(mTempRect, drawableRadius, drawableRadius, paint); paint.setXfermode(mModeSrcIn); super.onDraw(canvas); paint.setColor(mBorderOutsideColor); } else if(mBorderOutsideThickness == DEFAUTL_BORDER_THICKNESS) { // ?????? paint.setColor(mFillColor); mTempRect.set(mDrawRect.left + mBorderInsideThickness, mDrawRect.top + mBorderInsideThickness, mDrawRect.right - mBorderInsideThickness, mDrawRect.bottom - mBorderInsideThickness); canvas.drawRoundRect(mTempRect, drawableRadius, drawableRadius, paint); paint.setXfermode(mModeSrcIn); super.onDraw(canvas); paint.setColor(mBorderInsideColor); } else { // ???? paint.setColor(mFillColor); mTempRect.set(mDrawRect.left + mBorderInsideThickness + mBorderOutsideThickness, mDrawRect.top + mBorderInsideThickness + mBorderOutsideThickness, mDrawRect.right - mBorderInsideThickness - mBorderOutsideThickness, mDrawRect.bottom - mBorderInsideThickness - mBorderOutsideThickness); canvas.drawRoundRect(mTempRect, drawableRadius, drawableRadius, paint); paint.setXfermode(mModeSrcIn); super.onDraw(canvas); float insideBorderRadius = radius - mBorderOutsideThickness; paint.setXfermode(mModeDstOver); paint.setColor(mBorderInsideColor); mTempRect.set(mDrawRect.left + mBorderOutsideThickness, mDrawRect.top + mBorderOutsideThickness, mDrawRect.right - mBorderOutsideThickness, mDrawRect.bottom - mBorderOutsideThickness); canvas.drawRoundRect(mTempRect, insideBorderRadius, insideBorderRadius, paint); paint.setColor(mBorderOutsideColor); } paint.setXfermode(mModeDstOver); canvas.drawRoundRect(mDrawRect, radius, radius, paint); } else { // ?? paint.setColor(Color.WHITE); canvas.drawRoundRect(mDrawRect, radius, radius, paint); paint.setXfermode(mModeSrcIn); super.onDraw(canvas); paint.setXfermode(mModeDstOver); paint.setColor(Color.TRANSPARENT); canvas.drawRoundRect(mDrawRect, radius, radius, paint); } canvas.restoreToCount(saveCount); } else { super.onDraw(canvas); } } }