1. 程式人生 > >android RoundedBitmapDrawable最簡單方式實現圓角

android RoundedBitmapDrawable最簡單方式實現圓角

轉載自原部落格地址

demo

<?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>
  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);

效果如圖:

在這裡插入圖片描述

看看,是不是圓角和圓形圖片出來了,但是有沒有發現有個問題,我的女神臉變短了,被壓縮的感覺,誰幹的,還我女神。。
其實你去檢視下原始碼不難發現這是由於系統通過矩陣對我們的圖片進行了縮放處理,系統吧圖片縮放成了正方形,所以圖片就有了這種效果,解決辦法有多種
第一種,最簡單的方法,上傳圖片的時候直接傳正方形就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;
    }

在這裡插入圖片描述

備註

  1. 如果頭像或者其他的地方用這個方法值得推薦,可是如果在listview類的adapter中使用的話,老實說,我記憶體溢位了,貼一下我的部分程式碼;還是推薦鴻洋的一篇文章參考洪洋
Glide.with(getActivity()).load(imgUrl).asBitmap().error(R.drawable.icon_no_pic_long).placeholder(R.drawable.icon_no_pic_long).into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
                    RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
                    roundedBitmapDrawable.setCornerRadius(15);
                    finalHolder.image.setImageDrawable(roundedBitmapDrawable);//這個roundedBitmapDrawable 其實是應該回收的,可惜我還放在了adapter裡邊,
                }
            });