«

Android学习 - 圆形ImageView

时间:2024-3-2 17:40     作者:韩俊     分类: Android


先看下效果图:

public class RoundedImageView extends ImageView {

    private int borderThickness;
    private int borderColor;
    private Bitmap image;
    private Context context;
    private int width = 0;
    private int height = 0;

    public RoundedImageView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public RoundedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.roundedimageview);
        borderThickness = typedArray.getDimensionPixelSize(
                R.styleable.roundedimageview_border_thickness, 10);
        borderColor = typedArray.getColor(
                R.styleable.roundedimageview_border_color, 0xFFFFFFFF);
        typedArray.recycle();
        init();
    }

    public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
        init();
    }

    public void init() {
        image = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.main_portrait);
    }

    public void setImage(String path) {
        if (path == null || "".equals(path)) {
            return;
        }
        File file = new File(path);
        if ((!file.exists()) || (!file.isFile())) {
            return;
        }
        try {
            this.image = BitmapFactory.decodeStream(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            this.image = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.ic_launcher);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (width == 0) {
            width = getWidth();
        }
        if (height == 0) {
            height = getHeight();
        }
        if (width == 0 || height == 0) {
            return;
        }
        if (image == null) {
            return;
        }
        this.measure(0, 0);
        int radius = (width < height ? width : height) / 2 - borderThickness;
        Bitmap bitmap = image.copy(Bitmap.Config.ARGB_8888, true);
        drawCircleBorder(canvas, radius + borderThickness / 2);
        Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius);
        canvas.drawBitmap(roundBitmap, width / 2 - radius, height / 2 - radius,
                null);
    }

    public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
        Bitmap scaledSrcBmp;
        int diameter = radius * 2;

        int bmpWidth = bmp.getWidth();
        int bmpHeight = bmp.getHeight();
        int squareWidth = 0, squareHeight = 0;
        int x = 0, y = 0;
        Bitmap squareBitmap;
        if (bmpHeight > bmpWidth) {
            squareWidth = squareHeight = bmpWidth;
            x = 0;
            y = (bmpHeight - bmpWidth) / 2;
            squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,
                    squareHeight);
        } else if (bmpHeight < bmpWidth) {
            squareWidth = squareHeight = bmpHeight;
            x = (bmpWidth - bmpHeight) / 2;
            y = 0;
            squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,
                    squareHeight);
        } else {
            squareBitmap = bmp;
        }

        if (squareBitmap.getWidth() != diameter
                || squareBitmap.getHeight() != diameter) {
            scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter,
                    diameter, true);

        } else {
            scaledSrcBmp = squareBitmap;
        }
        Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),
                scaledSrcBmp.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),
                scaledSrcBmp.getHeight());

        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        paint.setDither(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(scaledSrcBmp.getWidth() / 2,
                scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,
                paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
        bmp.recycle();
        squareBitmap.recycle();
        scaledSrcBmp.recycle();
        bmp = null;
        squareBitmap = null;
        scaledSrcBmp = null;
        return output;
    }

    private void drawCircleBorder(Canvas canvas, int radius) {
        Paint myPaint = new Paint();
        myPaint.setAntiAlias(true);
        myPaint.setFilterBitmap(true);
        myPaint.setDither(true);
        myPaint.setColor(borderColor);
        myPaint.setStyle(Paint.Style.STROKE);
        myPaint.setStrokeWidth(borderThickness);
        canvas.drawCircle(width / 2, height / 2, radius, myPaint);
    }
}

attrs.xml

<!-- 主界面圆形头像roundedimageview -->
<declare-styleable name="roundedimageview">
    <attr name="border_thickness" format="dimension" />
    <attr name="border_color" format="color" />
</declare-styleable>

使用方式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:imagecontrol="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/ehr_backgroud"
    android:orientation="vertical" >
    <com.android.wolflz.RoundedImageView
        android:id="@+id/index_photo_imageview"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp"
        android:contentDescription="@string/contentDescription"
        imagecontrol:border_color="#ffffff"
        imagecontrol:border_thickness="4dp" />
</LinearLayout>

标签: android

热门推荐