androID开发中常常涉及到一种情况,就是将用户上传的图片以圆形样式显示,但是用户上传的图片可以有直角、圆角、正方形等多种不确定样式,这时就用到了自定义ImageVIEw控件,在安卓客户端使接收到的图片全部以圆形样式显示
circleimageVIEw.java
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; // 圆形边框的厚度默认值。 // 如果是0,则没有天蓝色渐变的边框。 private static final int DEFAulT_border_WIDTH = 0; private static final int DEFAulT_border_color = color.BLACK; 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 int mbordercolor = DEFAulT_border_color; private int mborderWIDth = DEFAulT_border_WIDTH; private Bitmap mBitmap; private BitmapShader mBitmapShader; private int mBitmapWIDth; private int mBitmapHeight; private float mDrawableRadius; private float mborderRadius; private boolean mReady; private boolean mSetupPending; private final Paint mFlagBackgroundPaint = new Paint(); private final TextPaint mFlagTextPaint = new TextPaint(); private String mFlagText; private boolean mShowFlag = false; private Rect mFlagTextBounds = new Rect(); Shader mSweepGradIEnt = null; 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,defStyle); 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) { if (getDrawable() == null) { return; } canvas.drawCircle(getWIDth() / 2,getHeight() / 2,mDrawableRadius,mBitmapPaint); if (mborderWIDth != 0) { canvas.save(); canvas.rotate(20,getWIDth() / 2,getHeight() / 2); canvas.drawCircle(getWIDth() / 2,mborderRadius,mborderPaint); canvas.restore(); } if (mShowFlag && mFlagText != null) { canvas.drawArc(mborderRect,40,100,false,mFlagBackgroundPaint); mFlagTextPaint.getTextBounds(mFlagText,mFlagText.length(),mFlagTextBounds); canvas.drawText(mFlagText,(float) ((3 + Math.cos((float) (Math.PI * 5 / 18))) * getHeight() / 4 + mFlagTextBounds.height() / 3),mFlagTextPaint); } } @OverrIDe protected voID onSizeChanged(int w,int h,int olDW,int oldh) { super.onSizeChanged(w,h,olDW,oldh); setup(); } public int getbordercolor() { return mbordercolor; } public voID setbordercolor(int bordercolor) { if (bordercolor == mbordercolor) { return; } mbordercolor = bordercolor; mborderPaint.setcolor(mbordercolor); invalIDate(); } public int getborderWIDth() { return mborderWIDth; } /** * @param borderWIDth * 圆形的边框厚度。 */ public voID setborderWIDth(int borderWIDth) { if (borderWIDth == mborderWIDth) { return; } mborderWIDth = borderWIDth; setup(); } @OverrIDe public voID setimageBitmap(Bitmap bm) { super.setimageBitmap(bm); mBitmap = bm; setup(); } @OverrIDe public voID setimageDrawable(Drawable drawable) { super.setimageDrawable(drawable); mBitmap = getBitmapFromDrawable(drawable); setup(); } @OverrIDe public voID setimageResource(int resID) { super.setimageResource(resID); mBitmap = getBitmapFromDrawable(getDrawable()); setup(); } @OverrIDe public voID setimageURI(Uri uri) { super.setimageURI(uri); mBitmap = getBitmapFromDrawable(getDrawable()); setup(); } 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,canvas.getWIDth(),canvas.getHeight()); drawable.draw(canvas); return bitmap; } catch (OutOfMemoryError e) { return null; } } private voID setup() { if (!mReady) { mSetupPending = true; return; } if (mBitmap == null) { 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); mBitmapHeight = mBitmap.getHeight(); mBitmapWIDth = mBitmap.getWIDth(); mborderRect.set(0,getWIDth(),getHeight()); mborderRadius = Math.min((mborderRect.height() - mborderWIDth) / 2,(mborderRect.wIDth() - mborderWIDth) / 2); mDrawableRect.set(mborderWIDth,mborderWIDth,mborderRect.wIDth() - mborderWIDth,mborderRect.height() - mborderWIDth); mDrawableRadius = Math.min(mDrawableRect.height() / 2,mDrawableRect.wIDth() / 2); mFlagBackgroundPaint.setcolor(color.BLACK & 0x66FFFFFF); mFlagBackgroundPaint.setFlags(TextPaint.ANTI_AliAS_FLAG); mFlagTextPaint.setFlags(TextPaint.ANTI_AliAS_FLAG); mFlagTextPaint.setTextAlign(Align.CENTER); mFlagTextPaint.setcolor(color.WHITE); mFlagTextPaint .setTextSize(getResources().getdisplayMetrics().density * 18); mSweepGradIEnt = new SweepGradIEnt(getWIDth() / 2,new int[] { color.rgb(255,255,255),color.rgb(1,209,255) },null); mborderPaint.setShader(mSweepGradIEnt); updateShaderMatrix(); invalIDate(); } 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) + mborderWIDth,(int) (dy + 0.5f) + mborderWIDth); mBitmapShader.setLocalMatrix(mShaderMatrix); } public voID setShowFlag(boolean show) { mShowFlag = show; invalIDate(); } public voID setFlagText(String text) { mFlagText = text; invalIDate(); } }
activity_main.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <com.example.vIEw.circleimageVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/></linearLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的android自定义控件ImageView实现圆形图片全部内容,希望文章能够帮你解决android自定义控件ImageView实现圆形图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)