日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android实现圆角照片和圆形照片

發(fā)布時(shí)間:2023/12/10 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android实现圆角照片和圆形照片 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

方法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);//設(shè)置透明度drawable.setAntiAlias(true);//設(shè)置抗鋸齒drawable.setDither(true);//設(shè)置防抖動(dòng)drawable.setGravity(Gravity.CENTER);if (circular) {drawable.setCircular(true);//設(shè)置正圓形} else {drawable.setCornerRadius(cornerRadius);//設(shè)置圓角半徑}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();//邊框?qū)挾?pixelint borderWidthHalf = 20;//轉(zhuǎn)換為正方形后的寬高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 來(lái)進(jìn)行一個(gè)中心裁剪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)實(shí)現(xiàn)圓角照片

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);//創(chuàng)建輸出的bitmapBitmap desBitmap = Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.ARGB_8888);//創(chuàng)建canvas并傳入desBitmap,這樣繪制的內(nèi)容都會(huì)在desBitmap上Canvas canvas = new Canvas(desBitmap);Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);//創(chuàng)建著色器BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);//給著色器配置matrixbitmapShader.setLocalMatrix(matrix);paint.setShader(bitmapShader);//創(chuàng)建矩形區(qū)域并且預(yù)留出borderRectF rect = new RectF(boarder, boarder, outWidth - boarder, outHeight - boarder);//把傳入的bitmap繪制到圓角矩形區(qū)域內(nèi)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>

?

總結(jié)

以上是生活随笔為你收集整理的Android实现圆角照片和圆形照片的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。