Android实现圆角照片和圆形照片
生活随笔
收集整理的這篇文章主要介紹了
Android实现圆角照片和圆形照片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法1: 使用RoundedBitmapDrawable
public static RoundedBitmapDrawable bitmapToRoundedDrawable(@NonNull Resources res, @NonNull Bitmap bitmap,boolean circular, float cornerRadius) {RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(res, bitmap);drawable.setAlpha(255);//設置透明度drawable.setAntiAlias(true);//設置抗鋸齒drawable.setDither(true);//設置防抖動drawable.setGravity(Gravity.CENTER);if (circular) {drawable.setCircular(true);//設置正圓形} else {drawable.setCornerRadius(cornerRadius);//設置圓角半徑}return drawable;}final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test); //圓形照片 final Drawable circleDrawable = Util.bitmapToRoundedDrawable(getResources(), bitmap, true, 0); mImageView1.setImageDrawable(circleDrawable); //圓角照片 final Drawable roundedDrawable = Util.bitmapToRoundedDrawable(getResources(), bitmap, false, 100); mImageView2.setImageDrawable(roundedDrawable);使用RoundedBitmapDrawable生成帶邊框的圓形照片:
public static Drawable bitmapToRoundedDrawableWithBorder(Resources res, Bitmap bitmap) {//原圖寬度int bitmapWidth = bitmap.getWidth();//原圖高度int bitmapHeight = bitmap.getHeight();//邊框寬度 pixelint borderWidthHalf = 20;//轉換為正方形后的寬高int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);//最終圖像的寬高int newBitmapSquareWidth = bitmapSquareWidth + borderWidthHalf;Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquareWidth, newBitmapSquareWidth, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(roundedBitmap);int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;//裁剪后圖像,注意X,Y要除以2 來進行一個中心裁剪canvas.drawBitmap(bitmap, x / 2, y / 2, null);Paint borderPaint = new Paint();borderPaint.setAntiAlias(true);borderPaint.setStyle(Paint.Style.STROKE);borderPaint.setStrokeWidth(borderWidthHalf);borderPaint.setColor(Color.GRAY);//添加邊框canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquareWidth / 2, borderPaint);return bitmapToRoundedDrawable(res, roundedBitmap, true, 0);} final Drawable roundedDrawableWithBorder = Util.bitmapToRoundedDrawableWithBorder(getResources(), bitmap); mImageView3.setImageDrawable(roundedDrawableWithBorder);方法2: 使用PorterDuffXfermode(PorterDuff.Mode.SRC_IN)實現圓角照片
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {try {int width = bitmap.getWidth();int height = bitmap.getHeight();Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(output);final Paint paint = new Paint();final Rect rect = new Rect(0, 0, width, height);final RectF rectF = new RectF(rect);final float roundPx = 200;paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(Color.BLACK);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));final Rect src = new Rect(0, 0, width, bitmap.getHeight());canvas.drawBitmap(bitmap, src, rect, paint);return output;} catch (Exception e) {e.printStackTrace();return bitmap;}} final Bitmap roundBitmap = Util.getRoundedCornerBitmap(bitmap); mImageView4.setImageBitmap(roundBitmap);方法3: 使用BitmapShader帶邊框的圓形照片
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);//創建輸出的bitmapBitmap 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);//給著色器配置matrixbitmapShader.setLocalMatrix(matrix);paint.setShader(bitmapShader);//創建矩形區域并且預留出borderRectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);//把傳入的bitmap繪制到圓角矩形區域內canvas.drawRoundRect(rect, radius, radius, paint);if (boarder > 0) {//繪制boarderPaint boarderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);boarderPaint.setColor(Color.BLACK);boarderPaint.setStyle(Paint.Style.STROKE);boarderPaint.setStrokeWidth(boarder);canvas.drawRoundRect(rect, radius, radius, boarderPaint);}return desBitmap;} final Bitmap roundBitmap1 = Util.getRoundBitmapByShader(bitmap, 800, 800, 400, 50); mImageView5.setImageBitmap(roundBitmap1);方法4: 使用CareView使用圓角
<android.support.v7.widget.CardViewandroid:layout_width="300dp"android:layout_height="300dp"app:cardBackgroundColor="#1ac"app:cardCornerRadius="12dp"app:cardElevation="0dp"app:cardPreventCornerOverlap="false"><ImageViewandroid:id="@+id/iv6"android:layout_width="300dp"android:layout_height="300dp"android:scaleType="centerCrop" /></android.support.v7.widget.CardView>?
總結
以上是生活随笔為你收集整理的Android实现圆角照片和圆形照片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【vue-router①】router-
- 下一篇: Vue表单类的父子组件数据传递示例_vu