Android自定义圆角ImageView控件
目前一些比较火的图片加载库虽然支持圆角加载,若你是接的别人作了一半的项目,刚好别人用的图片加载库刚好不支持圆角加载,那么这颗控件你值得拥有.(支持网络图片的加载)
1.创建CustomImageView类在你的项目中(源码如下)
importandroid.content.Context; importandroid.content.res.TypedArray; importandroid.graphics.Bitmap; importandroid.graphics.Bitmap.Config; importandroid.graphics.BitmapFactory; importandroid.graphics.Canvas; importandroid.graphics.Matrix; importandroid.graphics.Paint; importandroid.graphics.PorterDuff; importandroid.graphics.PorterDuffXfermode; importandroid.graphics.RectF; importandroid.graphics.drawable.BitmapDrawable; importandroid.graphics.drawable.Drawable; importandroid.util.AttributeSet; importandroid.widget.ImageView; importcom.towatt.charge.towatt.R; /** *@authorMr.lynn *@version1.0
*图片圆角实现 */ publicclassCustomImageViewextendsandroid.support.v7.widget.AppCompatImageView{ privatePaintpaint; privatePaintpaintBorder; privateBitmapmSrcBitmap; /** *圆角的弧度 */ privatefloatmRadius; privatebooleanmIsCircle; publicCustomImageView(finalContextcontext){ this(context,null); } publicCustomImageView(Contextcontext,AttributeSetattrs){ this(context,attrs,R.attr.customImageViewStyle); } publicCustomImageView(Contextcontext,AttributeSetattrs,intdefStyle){ super(context,attrs,defStyle); TypedArrayta=context.obtainStyledAttributes(attrs, R.styleable.CustomImageView,defStyle,0); mRadius=ta.getDimension(R.styleable.CustomImageView_radius,0); mIsCircle=ta.getBoolean(R.styleable.CustomImageView_circle,false); intsrcResource=attrs.getAttributeResourceValue( "http://schemas.android.com/apk/res/android","src",0); if(srcResource!=0) mSrcBitmap=BitmapFactory.decodeResource(getResources(), srcResource); ta.recycle(); paint=newPaint(); paint.setAntiAlias(true); paintBorder=newPaint(); paintBorder.setAntiAlias(true); } @Override publicvoidonDraw(Canvascanvas){ intwidth=canvas.getWidth()-getPaddingLeft()-getPaddingRight(); intheight=canvas.getHeight()-getPaddingTop()-getPaddingBottom(); Bitmapimage=drawableToBitmap(getDrawable()); if(mIsCircle){ BitmapreSizeImage=reSizeImageC(image,width,height); canvas.drawBitmap(createCircleImage(reSizeImage,width,height), getPaddingLeft(),getPaddingTop(),null); }else{ BitmapreSizeImage=reSizeImage(image,width,height); canvas.drawBitmap(createRoundImage(reSizeImage,width,height), getPaddingLeft(),getPaddingTop(),null); } } /** *画圆角 * *@paramsource *@paramwidth *@paramheight *@return */ privateBitmapcreateRoundImage(Bitmapsource,intwidth,intheight){ Paintpaint=newPaint(); paint.setAntiAlias(true); Bitmaptarget=Bitmap.createBitmap(width,height,Config.ARGB_8888); Canvascanvas=newCanvas(target); RectFrect=newRectF(0,0,width,height); canvas.drawRoundRect(rect,mRadius,mRadius,paint); //核心代码取两个图片的交集部分 paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source,0,0,paint); returntarget; } /** *画圆 * *@paramsource *@paramwidth *@paramheight *@return */ privateBitmapcreateCircleImage(Bitmapsource,intwidth,intheight){ Paintpaint=newPaint(); paint.setAntiAlias(true); Bitmaptarget=Bitmap.createBitmap(width,height,Config.ARGB_8888); Canvascanvas=newCanvas(target); canvas.drawCircle(width/2,height/2,Math.min(width,height)/2, paint); //核心代码取两个图片的交集部分 paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source,(width-source.getWidth())/2, (height-source.getHeight())/2,paint); returntarget; } @Override protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){ intwidth=MeasureSpec.getSize(widthMeasureSpec); intheight=MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(width,height); } /** *drawable转bitmap * *@paramdrawable *@return */ privateBitmapdrawableToBitmap(Drawabledrawable){ if(drawable==null){ if(mSrcBitmap!=null){ returnmSrcBitmap; }else{ returnnull; } }elseif(drawableinstanceofBitmapDrawable){ return((BitmapDrawable)drawable).getBitmap(); } Bitmapbitmap=Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),Bitmap.Config.ARGB_8888); Canvascanvas=newCanvas(bitmap); drawable.setBounds(0,0,canvas.getWidth(),canvas.getHeight()); drawable.draw(canvas); returnbitmap; } /** *重设Bitmap的宽高 * *@parambitmap *@paramnewWidth *@paramnewHeight *@return */ privateBitmapreSizeImage(Bitmapbitmap,intnewWidth,intnewHeight){ intwidth=bitmap.getWidth(); intheight=bitmap.getHeight(); //计算出缩放比 floatscaleWidth=((float)newWidth)/width; floatscaleHeight=((float)newHeight)/height; //矩阵缩放bitmap Matrixmatrix=newMatrix(); matrix.postScale(scaleWidth,scaleHeight); returnBitmap.createBitmap(bitmap,0,0,width,height,matrix,true); } /** *重设Bitmap的宽高 * *@parambitmap *@paramnewWidth *@paramnewHeight *@return */ privateBitmapreSizeImageC(Bitmapbitmap,intnewWidth,intnewHeight){ intwidth=bitmap.getWidth(); intheight=bitmap.getHeight(); intx=(newWidth-width)/2; inty=(newHeight-height)/2; if(x>0&&y>0){ returnBitmap.createBitmap(bitmap,0,0,width,height,null,true); } floatscale=1; if(width>height){ //按照宽度进行等比缩放 scale=((float)newWidth)/width; }else{ //按照高度进行等比缩放 //计算出缩放比 scale=((float)newHeight)/height; } Matrixmatrix=newMatrix(); matrix.postScale(scale,scale); returnBitmap.createBitmap(bitmap,0,0,width,height,matrix,true); } }
2.在values目录下创建attrs.xml(若是已存在该文件)直接复制如下代码既可
3.正确的使用方式(在布局文件中)
注意此路径是你控件所在包
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。