1. 程式人生 > >Android 簡單顯示圓角圖片

Android 簡單顯示圓角圖片

1.新增一個ImgaView。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="Hello World!" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/test" />
</RelativeLayout>

2.編寫修改bitmap的函式

    private RoundedBitmapDrawable rectRoundBitmap(Bitmap bitmap){
        //建立RoundedBitmapDrawable物件
        RoundedBitmapDrawable roundImg = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
        //抗鋸齒
        roundImg.setAntiAlias(true);
        //設定圓角半徑
        roundImg.setCornerRadius(15);
        return roundImg;
    }

3.顯示修改完後的bitmap

        ImageView im=(ImageView)findViewById(R.id.imageView);
        Bitmap image= BitmapFactory.decodeResource(getResources(), R.drawable.test);
        im.setImageDrawable( rectRoundBitmap(image));

4.效果圖