JAVA + 图片 + 头像 + 圆形

布局:

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"
    tools:context="com.example.com.lianxi_01.MainActivity">
    <ImageView
        android:id="@+id/img_rect_rounded"
        android:layout_width="200dp"
        android:layout_height="300dp"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"/>

    <ImageView
        android:id="@+id/img_rounded"
        android:layout_marginTop="20dp"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"/>


LinearLayout>
代码:
package com.example.com.lianxi_01;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView mImgRectRound;
    private ImageView mImgRound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            mImgRectRound = (ImageView) findViewById(R.id.img_rect_rounded);
            mImgRound = (ImageView) findViewById(R.id.img_rounded);
            rectRoundBitmap();
            roundBitmap();
        }

    private void rectRoundBitmap(){
        //得到资源文件的BitMap
        Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.a4);
        //创建RoundedBitmapDrawable对象
        RoundedBitmapDrawable roundImg = RoundedBitmapDrawableFactory.create(getResources(),image);
        //抗锯齿
        roundImg.setAntiAlias(true);
        //设置圆角半径
        roundImg.setCornerRadius(30);
        //设置显示图片
        mImgRectRound.setImageDrawable(roundImg);
    }

    private void roundBitmap(){
        //如果是圆的时候,我们应该把bitmap图片进行剪切成正方形, 然后再设置圆角半径为正方形边长的一半即可
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.a4);
        Bitmap bitmap = null;
        //将长方形图片裁剪成正方形图片
        if (image.getWidth() == image.getHeight()) {
            bitmap = Bitmap.createBitmap(image, image.getWidth() / 2 - image.getHeight() / 2, 0, image.getHeight(), image.getHeight());
        } else {
            bitmap = Bitmap.createBitmap(image, 0, image.getHeight() / 2 - image.getWidth() / 2, image.getWidth(), image.getWidth());
        }
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
        //圆角半径为正方形边长的一半
        roundedBitmapDrawable.setCornerRadius(bitmap.getWidth() / 2);
        //抗锯齿
        roundedBitmapDrawable.setAntiAlias(true);
        mImgRound.setImageDrawable(roundedBitmapDrawable);

    }
}
报错:a4图片 + 必须是正方形

你可能感兴趣的:(JAVA + 图片 + 头像 + 圆形)