绘制圆形的图片(增强版)

来源:互联网 发布:微服务架构 java框架 编辑:程序博客网 时间:2024/05/29 18:02
重绘方法
postInvalidate();这个方法是直接重绘的
//100毫秒重绘一次        postInvalidateDelayed(100);
使用方法:
<com.example.k.shoppingapp.Other.MyView            android:id="@+id/baby_user"            android:layout_width="30dp"            android:layout_height="30dp"            />
代码:
myView.bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.user);        myView.postInvalidate();

主类:
package com.example.k.test;/** * Created by k on 2016/7/26. */import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.util.AttributeSet;import android.view.View;/** * Created by k on 2016/6/7. */public class MyView extends View {Context context;    int mHeight = 0,mWidth = 0;    public Bitmap bitmap = null;    public MyView(Context context, AttributeSet attrs) {        super(context, attrs);this.context = context;        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bb);    }    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int f = measuredWidth(widthMeasureSpec);        int g = measuredHeight(heightMeasureSpec);        setMeasuredDimension(f, g);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int min = Math.min(mWidth, mHeight);        /**         * 长度如果不一致,按小的值进行压缩         */        bitmap = Bitmap.createScaledBitmap(bitmap, min, min, false);        canvas.drawBitmap(createCircleImage(bitmap, min), 0, 0, null);    }    //************************************************************************************************    private int measuredWidth(int widthMeasureSpec) {        int Mode = MeasureSpec.getMode(widthMeasureSpec);        int Size = MeasureSpec.getSize(widthMeasureSpec);        if (Mode == MeasureSpec.EXACTLY) {            mWidth = Size;        } else {            //由图片决定大小            int value = getPaddingLeft()+getPaddingRight()+bitmap.getWidth();            if (Mode == MeasureSpec.AT_MOST) {                //由图片和Padding决定宽度,但是不能超过View的宽                mWidth = Math.min(value,Size);            }        }        return mWidth;    }    //**********************************************************************************************    private int measuredHeight(int heightMeasureSpec) {        int Mode = MeasureSpec.getMode(heightMeasureSpec);        int Size = MeasureSpec.getSize(heightMeasureSpec);        if (Mode == MeasureSpec.EXACTLY) {            mHeight = Size;        } else {            //由图片决定高度            int intvalur1 = getPaddingTop()+getPaddingBottom()+bitmap.getHeight();            if (Mode == MeasureSpec.AT_MOST) {                //由图片和Padding决定大小,但是不能超过View的高                mHeight = Math.min(intvalur1,Size);            }        }        return mHeight;    }    //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    private Bitmap createCircleImage(Bitmap source, int min)    {        final Paint paint = new Paint();        final Paint paint1 = new Paint();        final Paint paint2 = new Paint();        //消除锯齿,让绘制出来的画更清晰        paint.setAntiAlias(true);        paint1.setAntiAlias(true);        paint2.setAntiAlias(true);        paint2.setColor(Color.parseColor("#553638"));        //创建一个Bitmap        Bitmap target = Bitmap.createBitmap(min, min, Bitmap.Config.ARGB_8888);        Bitmap target1 = Bitmap.createBitmap(min,min,Bitmap.Config.ARGB_8888);        /**         * 产生一个同样大小的画布         */        //以后再Canvas上的画图操作都会作用在target上        Canvas canvas = new Canvas(target);        Canvas canvas1 = new Canvas(target1);        /**         * 首先绘制圆形         */        //第一个参数为圆心的x坐标,第二个参数为圆心的y坐标,第三个为圆心的半径        canvas.drawCircle(min / 2, min / 2, min / 2-Adaptation.dp2px(context,1)/2, paint);        canvas1.drawCircle(min / 2, min / 2, min / 2, paint2);        /**         * 使用SRC_IN模式显示后画图的交集处         */        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        //这种模式是前图和后图一起显示,如果有相交的部分只显示后图的        paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));        /**         * 绘制图片         */        canvas.drawBitmap(source, 0, 0, paint);        canvas1.drawBitmap(target,0,0,paint1);        return target1;    }   }


配合适配类使用:
package com.example.k.shoppingapp.Util;import android.content.Context;/** * Created by k on 2016/8/14. */public class Adaptation {    public static int dp2px(Context context,float dp) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (dp * scale + 0.5f);    }    //sp转px    public static int sp2px(Context context,float sp) {        final float scale = context.getResources().getDisplayMetrics().scaledDensity;        return (int) (sp * scale + 0.5f);    }}


下面是一些配置内容

1.PorterDuff.Mode.CLEAR  

  所绘制不会提交到画布上。

2.PorterDuff.Mode.SRC

   显示上层绘制图片

3.PorterDuff.Mode.DST

  显示下层绘制图片

4.PorterDuff.Mode.SRC_OVER

  正常绘制显示,上下层绘制叠盖。

5.PorterDuff.Mode.DST_OVER

  上下层都显示。下层居上显示。

6.PorterDuff.Mode.SRC_IN

   取两层绘制交集。显示上层。

7.PorterDuff.Mode.DST_IN

  取两层绘制交集。显示下层。

8.PorterDuff.Mode.SRC_OUT

 取上层绘制非交集部分。

9.PorterDuff.Mode.DST_OUT

 取下层绘制非交集部分。

10.PorterDuff.Mode.SRC_ATOP

 取下层非交集部分与上层交集部分

11.PorterDuff.Mode.DST_ATOP

 取上层非交集部分与下层交集部分

12.PorterDuff.Mode.XOR

  异或:去除两图层交集部分

13.PorterDuff.Mode.DARKEN

  取两图层全部区域,交集部分颜色加深

14.PorterDuff.Mode.LIGHTEN

  取两图层全部,点亮交集部分颜色

15.PorterDuff.Mode.MULTIPLY

  取两图层交集部分叠加后颜色

16.PorterDuff.Mode.SCREEN

  取两图层全部区域,交集部分变为透明色

0 0
原创粉丝点击