Android自定义Drawable实现圆形和圆角
本文实例为大家分享了自定义Drawable实现圆形和圆角的具体代码,供大家参考,具体内容如下
圆形
packagecom.customview.widget; importandroid.graphics.Bitmap; importandroid.graphics.BitmapShader; importandroid.graphics.Canvas; importandroid.graphics.ColorFilter; importandroid.graphics.Paint; importandroid.graphics.PixelFormat; importandroid.graphics.Shader; importandroid.graphics.drawable.Drawable; importandroid.support.annotation.IntRange; importandroid.support.annotation.NonNull; importandroid.support.annotation.Nullable; /** *自定义Drawable实现圆形图片 *Createdbyhust_twjon2017/9/26. */ publicclassCircleDrawableextendsDrawable{ privatePaintmPaint; privateintmWidth;//宽/高,直径 privateBitmapmBitmap; publicCircleDrawable(Bitmapbitmap){ this.mBitmap=bitmap; BitmapShaderbitmapShader=newBitmapShader(mBitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);//着色器水平和竖直都需要填充满 mPaint=newPaint(); mPaint.setAntiAlias(true); mPaint.setShader(bitmapShader); mWidth=Math.min(mBitmap.getWidth(),mBitmap.getHeight());//宽高的最小值作为直径 } @Override publicvoiddraw(@NonNullCanvascanvas){ canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2,mPaint);//绘制圆形 } @Override publicvoidsetAlpha(@IntRange(from=0,to=255)inti){ mPaint.setAlpha(i); } @Override publicvoidsetColorFilter(@NullableColorFiltercolorFilter){ mPaint.setColorFilter(colorFilter); } @Override publicintgetOpacity(){ returnPixelFormat.TRANSLUCENT;//设置系统默认,让drawable支持和窗口一样的透明度 } //还需要从重写以下2个方法,返回drawable实际宽高 @Override publicintgetIntrinsicWidth(){ returnmWidth; } @Override publicintgetIntrinsicHeight(){ returnmWidth; } }
圆角:
packagecom.customview.widget; importandroid.graphics.Bitmap; importandroid.graphics.BitmapShader; importandroid.graphics.Canvas; importandroid.graphics.ColorFilter; importandroid.graphics.Paint; importandroid.graphics.PixelFormat; importandroid.graphics.RectF; importandroid.graphics.Shader; importandroid.graphics.drawable.Drawable; importandroid.support.annotation.IntRange; importandroid.support.annotation.NonNull; importandroid.support.annotation.Nullable; /** *自定义Drawable实现圆角图片 *Createdbyhust_twjon2017/9/26. */ publicclassRoundCornerDrawableextendsDrawable{ privatePaintmPaint; privateRectFrectF;//定义矩形区域 privateBitmapmBitmap; publicRoundCornerDrawable(Bitmapbitmap){ this.mBitmap=bitmap; BitmapShaderbitmapShader=newBitmapShader(mBitmap,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);//着色器水平和竖直都需要填充满 mPaint=newPaint(); mPaint.setAntiAlias(true); mPaint.setShader(bitmapShader); //rectF=newRectF(0,0,bitmap.getWidth(),bitmap.getHeight()); } @Override publicvoiddraw(@NonNullCanvascanvas){ //RectF:圆角矩形区域 canvas.drawRoundRect(newRectF(0,0,mBitmap.getWidth(),mBitmap.getHeight()),100,100,mPaint);//rx:x方向上的圆角半径;ry:y方向上的圆角半径。 } /*@Override publicvoidsetBounds(intleft,inttop,intright,intbottom){ super.setBounds(left,top,right,bottom); rectF=newRectF(left,top,right,bottom); }*/ @Override publicvoidsetAlpha(@IntRange(from=0,to=255)inti){ mPaint.setAlpha(i); } @Override publicvoidsetColorFilter(@NullableColorFiltercolorFilter){ mPaint.setColorFilter(colorFilter); } @Override publicintgetOpacity(){ returnPixelFormat.TRANSLUCENT;//设置系统默认,让drawable支持和窗口一样的透明度 } //还需要从重写以下2个方法,返回drawable实际宽高 @Override publicintgetIntrinsicWidth(){ returnmBitmap.getWidth(); } @Override publicintgetIntrinsicHeight(){ returnmBitmap.getHeight(); } }
MainActivity中:
Bitmapbitmap=BitmapFactory.decodeResource(getResources(),R.drawable.XXX); imageView.setImageDrawable(newRoundCornerDrawable(bitmap));
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。