android绘制圆形图片、圆圈以及图片缩放

这两天做开发,需要将图片资源做成圆形,并且要对图片进行缩放,研究了半天,终于整出来了,效果如下:



代码如下:

package com.android.mywidget;


import android.content.Context;
import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import com.android.contacts.R;
import android.util.Log;


/**
 * Created with IntelliJ IDEA.
 * User: XXXXXX
 * Module: XXXXXXX
 * Date: 13-9-24
 * Time: 上午11:31
 */
public class CircleImageView extends ImageView implements View.OnClickListener {
    private static final String TAG = "CircleImageView";
    private Paint mPaint = new Paint();

    private static final int STROKE_WIDTH = 8;

    public CircleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        setOnClickListener(this);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (null != drawable) {
            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            Bitmap b = circleBitmap(scaleBitmap(bitmap));
            final Rect rect = new Rect(0, 0, b.getWidth(), b.getHeight());
            mPaint.reset();
            canvas.drawBitmap(b, rect, rect, mPaint);
        } else {
            super.onDraw(canvas);
        }
    }

    // 将矩形图片绘制成圆形图片并在最外围画一圈白色
    private Bitmap circleBitmap(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff000000;
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        mPaint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        mPaint.setColor(color);
        final int width = bitmap.getWidth();
        canvas.drawCircle(width / 2, width / 2, width / 2, mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, mPaint);//将图片绘制成白色图片

        //画白色圆圈
        mPaint.reset();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(STROKE_WIDTH);
        mPaint.setAntiAlias(true);
        canvas.drawCircle(width / 2, width / 2, width / 2 - STROKE_WIDTH / 2, mPaint);


        return output;
    }


    /**
     * change the bitmap size
     * @param src : Bitmap source
     * @return : The scaled bitmap
     */
    private Bitmap scaleBitmap(Bitmap src) {
        int width = src.getWidth();//原来尺寸大小
        int height = src.getHeight();

        final float destSize = this.getWidth();//缩放到这个大小

//图片缩放比例,新尺寸/原来的尺寸
        float scaleWidth = ((float) destSize) / width;
        float scaleHeight = ((float) destSize) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

//返回缩放后的图片
        return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
    }

    @Override
    public void onClick(View v) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值