Android中的图片圆角怎么实现
短信预约 -IT技能 免费直播动态提醒
这篇文章主要介绍了Android中的图片圆角怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Android中的图片圆角怎么实现文章都会有所收获,下面我们一起来看看吧。
Android 开发中,经常需要对图片进行二次处理,比如添加圆角效果 或 显示圆形图片;
方法一
通过第三方框架 Glide 设置圆角效果;
写法1:
RequestOptions options = new RequestOptions().error(R.drawable.img_load_failure).bitmapTransform(new RoundedCorners(30));//图片圆角为30Glide.with(this).load(URL) //图片地址 .apply(options) .into(ImagView);
写法2:
RequestOptions requestOptions = new RequestOptions();requestOptions.placeholder(R.drawable.ic_launcher_background);requestOptions.circleCropTransform();requestOptions.transforms( new RoundedCorners(30));Glide.with(this).load(URL) //图片地址 .apply(options) .into(ImagView);
写法3:
RequestOptions options = new RequestOptions().centerCrop() .transform(new RoundTransform(this,30)); Glide.with(this).load(URL) //图片地址 .apply(options) .into(ImagView);
public class RoundTransform extends BitmapTransformation { private static float radius = 0f; public RoundTransform(Context context) { this(context, 4); } public RoundTransform(Context context, int dp) { super(context); this.radius = Resources.getSystem().getDisplayMetrics().density * dp; } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { Bitmap bitmap = TransformationUtils.centerCrop(pool, toTransform, outWidth, outHeight); return roundCrop(pool, bitmap); } private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rectF, radius, radius, paint); return result; } public String getId() { return getClass().getName() + Math.round(radius); } @Override public void updateDiskCacheKey(MessageDigest messageDigest) { }}
方法二
自定义ImageView 设置圆角效果;
<ImageView android:id="@+id/iv" android:layout_width="300dp" android:layout_height="300dp" android:layout_centerHorizontal="true" />
ImageView iv = findViewById(R.id.iv); Bitmap bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.fengjing); Bitmap outBitmap =getRoundBitmapByShader(bitmap, 500,300,20, 3); iv.setImageBitmap(outBitmap);
public class RoundRectImageView extends ImageView{ private Paint paint; public RoundRectImageView(Context context) { this(context,null); } public RoundRectImageView(Context context, AttributeSet attrs) { this(context, attrs,0); } public RoundRectImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (null != drawable) { Bitmap bitmap = getBitmapFromDrawable(drawable);// Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Bitmap b = getRoundBitmapByShader(bitmap,getWidth(),getHeight(), 50,0); final Rect rectclass="lazy" data-src = new Rect(0, 0, b.getWidth(), b.getHeight()); final Rect rectDest = new Rect(0,0,getWidth(),getHeight()); paint.reset(); canvas.drawBitmap(b, rectclass="lazy" data-src, rectDest, paint); } else { super.onDraw(canvas); } } public static Bitmap getBitmapFromDrawable(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable .getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); //drawable.setBounds(-4, -4, width + 4, height + 4); drawable.draw(canvas); return bitmap; } public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) { if (bitmap == null) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); float widthScale = outWidth * 1f / width; float heightScale = outHeight * 1f / height; Matrix matrix = new Matrix(); matrix.setScale(widthScale, heightScale); //创建输出的bitmap Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上 Canvas canvas = new Canvas(desBitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //创建着色器 BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); //给着色器配置matrix bitmapShader.setLocalMatrix(matrix); paint.setShader(bitmapShader); //创建矩形区域并且预留出border RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder); //把传入的bitmap绘制到圆角矩形区域内 canvas.drawRoundRect(rect, radius, radius, paint); if (boarder > 0) { //绘制boarder Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); boarderPaint.setColor(Color.GREEN); boarderPaint.setStyle(Paint.Style.STROKE); boarderPaint.setStrokeWidth(boarder); canvas.drawRoundRect(rect, radius, radius, boarderPaint); } return desBitmap; } }
方法三
对图片进行处理,还可以加边框;
public static Bitmap getRoundBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int radius, int boarder) { if (bitmap == null) { return null; } int height = bitmap.getHeight(); int width = bitmap.getWidth(); float widthScale = outWidth * 1f / width; float heightScale = outHeight * 1f / height; Matrix matrix = new Matrix(); matrix.setScale(widthScale, heightScale); //创建输出的bitmap Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888); //创建canvas并传入desBitmap,这样绘制的内容都会在desBitmap上 Canvas canvas = new Canvas(desBitmap); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); //创建着色器 BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); //给着色器配置matrix bitmapShader.setLocalMatrix(matrix); paint.setShader(bitmapShader); //创建矩形区域并且预留出border RectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder); //把传入的bitmap绘制到圆角矩形区域内 canvas.drawRoundRect(rect, radius, radius, paint); if (boarder > 0) { //绘制boarder Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); boarderPaint.setColor(Color.GREEN); boarderPaint.setStyle(Paint.Style.STROKE); boarderPaint.setStrokeWidth(boarder); canvas.drawRoundRect(rect, radius, radius, boarderPaint); } return desBitmap;}
实现圆形和边框:
public static Bitmap getCircleBitmapByShader(Bitmap bitmap, int outWidth, int outHeight, int boarder) {int radius;int width = bitmap.getWidth();int height = bitmap.getHeight();float widthScale = outWidth * 1f / width;float heightScale = outHeight * 1f / height; Bitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);if (outHeight > outWidth) { radius = outWidth / 2;} else { radius = outHeight / 2;}//创建canvasCanvas canvas = new Canvas(desBitmap);Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);Matrix matrix = new Matrix();matrix.setScale(widthScale, heightScale);bitmapShader.setLocalMatrix(matrix);paint.setShader(bitmapShader);canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, paint);if (boarder > 0) { //绘制boarder Paint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); boarderPaint.setColor(Color.GREEN); boarderPaint.setStyle(Paint.Style.STROKE); boarderPaint.setStrokeWidth(boarder); canvas.drawCircle(outWidth / 2, outHeight / 2, radius - boarder, boarderPaint);}return desBitmap;}
关于“Android中的图片圆角怎么实现”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Android中的图片圆角怎么实现”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程网行业资讯频道。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341