频道栏目
首页 > 资讯 > Android > 正文

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

17-02-22        来源:[db:作者]  
收藏   我要投稿

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

自定义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">
 
</com.aynu.circleimageview.customimageview></com.aynu.circleimageview.customimageview></com.aynu.circleimageview.customimageview></imageview></linearlayout>

res/attrs文件
 

<!--?xml version="1.0" encoding="utf-8"?-->
    <resources>
 
      
         
          <enum name="circle" value="0">
          <enum name="round" value="1">
       </enum></enum></attr>
     </attr>
 
     <declare-styleable name="CustomImageView">
          
           
            
       </attr></attr></attr></declare-styleable>
 
    </attr></resources>

 

相关TAG标签
上一篇:Android项目数据存储之SharedPreferences
下一篇:通过ContentProvider拿到手机所有短信
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站