Android圆形图片和圆角图片的绘制

转载请标明出处:http://blog.csdn.NET/lmj623565791/article/details/24555655   圆角图片绘制

FROM  GA_studio   http://blog.csdn.net/tianjian4592   //drawBitmap讲解

自定义ImageView

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.MeasureSpec;

import com.aynu.circleimageview.R;

/**
 *
 * @author zhy
 *
 */
public class CustomImageView extends View
{

    /**
     * TYPE_CIRCLE / TYPE_ROUND
     */
    private int type;
    private static final int TYPE_CIRCLE = 0;
    private static final int TYPE_ROUND = 1;

    /**
     *bitmap
     */
    private Bitmap mSrc;

    /**
     *半径
     */
    private int mRadius;

    /**
     * �ؼ�长度     */
    private int mWidth;
    /**
     * 高度     */
    private int mHeight;

    public CustomImageView(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
    }

    public CustomImageView(Context context)
    {
        this(context, null);
    }

    /**
     * 获取属性值��
     *
     * @param context
     * @param attrs
     * @param defStyle
     */
    public CustomImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0);
        int n = a.getIndexCount();
        for (int i = 0; i < n; i++)
        {
            int attr = a.getIndex(i);
            switch (attr)
            {
                case R.styleable.CustomImageView_src://获取图片,自定义属性,设置图片并获取
                    mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));
                    break;
                case R.styleable.CustomImageView_type://获取图片要设置圆形还是圆角图片
                    type = a.getInt(attr, 0);
                    break;
                case R.styleable.CustomImageView_borderRadius://半径值
                    mRadius = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f,
                            getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();
    }

    /**
     * ����ؼ��测量     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        /**
         * ���获取宽度测量模式和大小         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate精确的
        {
            mWidth = specSize;
        } else
        {
            //             int desireByImg = getPaddingLeft() + getPaddingRight() + mSrc.getWidth();//图片的大小,和测量出来的大小比较
            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
                mWidth = Math.min(desireByImg, specSize);
            }
        }

        /***
         * ��获取高度的测量模式         */

        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
        {
            mHeight = specSize;
        } else
        {
            int desire = getPaddingTop() + getPaddingBottom() + mSrc.getHeight();
            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
                mHeight = Math.min(desire, specSize);
            }
        }
        setMeasuredDimension(mWidth, mHeight);

    }

    /**
     * ����
     */
    @Override
    protected void onDraw(Canvas canvas)
    {

        switch (type)
        {
            // ���圆形图片
            case TYPE_CIRCLE:

                int min = Math.min(mWidth, mHeight);
                /**
                 * 使得当前的图片的宽和高一致Bitmap
                 */
                mSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);
                //绘制圆角图片
                canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);
                break;
            case TYPE_ROUND:
                //圆角图片
                canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);
                break;

        }

    }

    /**
     *
     * @param source
     * @param min
     * @return
     */
    private Bitmap createCircleImage(Bitmap source, int min)
    {
        //画笔
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        //创建Bitmap对象
        Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
        /**
         * 绘制         */
        Canvas canvas = new Canvas(target);
        /**
         * 画圆
         */
        canvas.drawCircle(min / 2, min / 2, min / 2, paint);
        /**
         * ʹ��设置mode���˵��
         */
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        /**
         * ���画图像
         */
        canvas.drawBitmap(source, 0, 0, paint);
        return target;
    }

    /**
     * ��
     *
     * @param source
     * @return
     */
    private Bitmap createRoundConerImage(Bitmap source)
    {
        final Paint paint = new Paint();
        paint.setAntiAlias(true);
        //创建bitmap
        Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
        //绘制图像
        Canvas canvas = new Canvas(target);
        RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());
        canvas.drawRoundRect(rect, 50f, 50f, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //图片,大小,绘制的地方
        canvas.drawBitmap(source, 0, 0, paint);
        return target;
    }
}

 

 

 

activity_main.xml文件:

<LinearLayout 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"
              xmlns:zhy="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:src="@mipmap/icon" />

    <com.aynu.circleimageview.CustomImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        zhy:src="@mipmap/icon"
        zhy:type="circle" />

    <com.aynu.circleimageview.CustomImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        zhy:borderRadius="10dp"
        zhy:src="@mipmap/icon"
        zhy:type="round" />

    <com.aynu.circleimageview.CustomImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        zhy:borderRadius="20dp"
        zhy:src="@mipmap/icon"
        zhy:type="round" />

</LinearLayout>

res/attrs文件

<?xml version="1.0" encoding="utf-8"?>
    <resources>

     <attr name="borderRadius" format="dimension" />
        <attr name="type">
          <enum name="circle" value="0" />
          <enum name="round" value="1" />
       </attr>
     <attr name="src" format="reference"></attr>

     <declare-styleable name="CustomImageView">
         <attr name="borderRadius" />
          <attr name="type" />
           <attr name="src" />
       </declare-styleable>

    </resources>

代码下载地址:

http://download.csdn.net/detail/lmj623565791/7269005

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值