android RoundedBitmapDrawable最簡單方式實現圓角圖片(一)
阿新 • • 發佈:2019-02-04
一次偶然的機會,讓我發現了新大陸RoundedBitmapDrawable,不難看出他的作用是圓角圖片。今天來看下史上最簡單的方式,為啥說最簡單呢,因為系統supportV4已經提供了api,你只需一句話呼叫就完事,你說能不簡單嗎。。
先看如何使用,效果圖我放後面,我擔心有人看到圖就忘記看程式碼了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.apple.Custom.Matrix.DrawableActivity">
<ImageView
android:id="@+id/mm1"
android:layout_width ="150dp"
android:layout_height="150dp" />
<ImageView
android:id="@+id/mm2"
android:layout_width="150dp"
android:layout_height="150dp" />
<ImageView
android:id="@+id/mm3"
android:layout_width="150dp"
android:layout_height="150dp" />
</LinearLayout>
看activity裡面只需這麼簡單的幾步就可以,簡不簡單
ImageView mm1 = (ImageView) findViewById(R.id.mm1);
ImageView mm2 = (ImageView) findViewById(R.id.mm2);
ImageView mm3 = (ImageView) findViewById(R.id.mm3);
RoundedBitmapDrawable roundedBitmapDrawable1 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.ns2));
RoundedBitmapDrawable roundedBitmapDrawable2 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.ns2));
RoundedBitmapDrawable roundedBitmapDrawable3 = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.ns2));
roundedBitmapDrawable1.setCircular(true);
mm1.setImageDrawable(roundedBitmapDrawable1);
roundedBitmapDrawable2.setCornerRadius(150);
mm2.setImageDrawable(roundedBitmapDrawable2);
roundedBitmapDrawable3.setCornerRadius(30);
mm3.setImageDrawable(roundedBitmapDrawable3);
run下就可以了,好不要眨眼,效果圖來了,我保證,你覺對沒見過這麼好看的圖
看看,是不是圓角和圓形圖片出來了,但是有沒有發現有個問題,我的女神臉變短了,被壓縮的感覺,誰幹的,還我女神。。
其實你去檢視下原始碼不難發現這是由於系統通過矩陣對我們的圖片進行了縮放處理,系統吧圖片縮放成了正方形,所以圖片就有了這種效果,解決辦法有多種
第一種,最簡單的方法,上傳圖片的時候直接傳正方形就ok,這個。。。有點雞肋,這樣處理非得把人累死。。我還想再活五百年
第二種,不管你傳的是啥,程式碼給你加工,不會累死你了,只是我會少活一年 囧 ,好,看大招
private Drawable createRoundImageWithBorder(Bitmap bitmap){
//原圖寬度
int bitmapWidth = bitmap.getWidth();
//原圖高度
int bitmapHeight = bitmap.getHeight();
//邊框寬度 pixel
int 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.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidthHalf);
borderPaint.setColor(Color.WHITE);
//新增邊框
canvas.drawCircle(canvas.getWidth()/2, canvas.getWidth()/2, newBitmapSquareWidth/2, borderPaint);
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),roundedBitmap);
roundedBitmapDrawable.setGravity(Gravity.CENTER);
roundedBitmapDrawable.setCircular(true);
return roundedBitmapDrawable;
}
看看我的漂亮女神回來了
下篇繼續講其他方式實現圓角圖片,敬請期待
上面圖片來自網路,如果有不妥的請留言,我將第一時間刪除