Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the picture fillet in Android

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Android picture fillet how to achieve the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Android picture fillet how to achieve the article will have a harvest, let's take a look.

In Android development, it is often necessary to reprocess pictures, such as adding rounded corners or displaying round pictures.

Method one

Set the fillet effect through the third-party frame Glide

Write 1:

RequestOptions options = new RequestOptions () .error (R.drawable.img_load_failure) .bitmapTransform (new RoundedCorners (30)); / / 30Glide.with (this) .load (URL) / / Picture address .apply (options) .into (ImagView)

Write method 2:

RequestOptions requestOptions = new RequestOptions (); requestOptions.placeholder (R.drawable.ic_launcher_background); requestOptions.circleCropTransform (); requestOptions.transforms (new RoundedCorners (30)); Glide.with (this) .load (URL) / / picture address .apply (options) .into (ImagView)

Write 3:

RequestOptions options = new RequestOptions (). CenterCrop () .transform (new RoundTransform (this,30)); Glide.with (this) .load (URL) / / picture address .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) {}} method 2

Customize ImageView to set fillet effect

ImageView iv = findViewById (R.id.iv); Bitmap bitmap = BitmapFactory.decodeResource (getResources (), R.drawable.fengjing); Bitmap outBitmap = getRoundBitmapByShader (bitmap, 500pint 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 ();} / * draw rounded rectangle picture * / @ 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 (), 50final Rect rectDest ()); final Rect rectSrc = new Rect (0Ling 0, b.getWidth (), b.getHeight ()); final Rect rectDest = new Rect (0Ling 0 getWidth (), getHeight ()); paint.reset (); canvas.drawBitmap (b, rectSrc, rectDest, paint) } else {super.onDraw (canvas);}} / * convert resource images into Bitmap * @ param drawable * resource images * @ return bitmaps * / 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); / / create output bitmap Bitmap desBitmap = Bitmap.createBitmap (outWidth, outHeight, Bitmap.Config.ARGB_8888) / / create canvas and pass desBitmap so that the content drawn will Canvas canvas = new Canvas (desBitmap); Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG) on desBitmap; / / create shader BitmapShader bitmapShader = new BitmapShader (bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); / / configure shader matrix bitmapShader.setLocalMatrix (matrix); paint.setShader (bitmapShader) / / create a rectangular region and reserve border RectF rect = new RectF (boarder, boarder, outWidth-boarder, outHeight-boarder); / / draw the incoming bitmap into the rounded rectangular area canvas.drawRoundRect (rect, radius, radius, paint); if (boarder > 0) {/ / draw boarderPaint 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;}} method 3

To deal with the picture, you can also add borders

/ * * achieve a circular border through BitmapShader * @ param bitmap * @ param outWidth output picture width * @ param outHeight output picture height * @ param radius fillet size * @ param boarder border width * / 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); / / create output bitmap Bitmap desBitmap = Bitmap.createBitmap (outWidth, outHeight, Bitmap.Config.ARGB_8888); / / create canvas and pass in desBitmap, so that the drawn content will be Canvas canvas = new Canvas (desBitmap) on desBitmap Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG); / / create shader BitmapShader bitmapShader = new BitmapShader (bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); / / configure shader matrix bitmapShader.setLocalMatrix (matrix); paint.setShader (bitmapShader); / / create a rectangular region and reserve border RectF rect = new RectF (boarder, boarder, outWidth-boarder, outHeight-boarder) / draw the incoming bitmap to the rounded rectangle canvas.drawRoundRect (rect, radius, radius, paint); if (boarder > 0) {/ / draw boarderPaint 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;}

Implement circles and borders:

/ * * achieve a circular border through BitmapShader * @ param bitmap * @ param outWidth output picture width * @ param outHeight output picture height * @ param boarder border size * / 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;} / / create 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) {/ / draw boarderPaint 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 } this is the end of the article on "how to realize the fillet of pictures in Android". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to realize the picture fillet in Android". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report