浅谈Android - BitmapShade 并用BitmapShade 实现一个圆形图片

来源:互联网 发布:shell编程题 编辑:程序博客网 时间:2024/05/22 11:49

1,BitmapShader的介绍

BitmapShader是Shader的子类,可以通过Paint.setShader(Shader shader)进行设置、

构造方法:
BitmapShader mBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);

参数1:bitmap

参数2,参数3:TileMode;

TileMode的取值有三种:

CLAMP 拉伸

REPEAT 重复

MIRROR 镜像

如果大家给电脑屏幕设置屏保的时候,如果图片太小,可以选择重复、拉伸、镜像;

重复:就是横向、纵向不断重复这个bitmap

镜像:横向不断翻转重复,纵向不断翻转重复;

拉伸:这个和电脑屏保的模式应该有些不同,这个拉伸的是图片最后的那一个像素;横向的最后一个横行像素,不断的重复,纵项的那一列像素,不断的重复;

现在大概明白了,BitmapShader通过设置给mPaint,然后用这个mPaint绘图时,就会根据你设置的TileMode,对绘制区域进行着色。

这里需要注意一点:就是BitmapShader是从你的画布的左上角开始绘制的,不在view的右下角绘制个正方形,它不会在你正方形的左上角开始。

2,通过BitmapShader 实现一个圆形图片

代码如下:

package com.example.administrator.myscrollview.view;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapShader;import android.graphics.Canvas;import android.graphics.Matrix;import android.graphics.Paint;import android.graphics.Shader;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.widget.ImageView;/** * Created by Administrator on 2016/9/29. */public class RoundImageView extends ImageView {    /**     * 绘图的Paint     */    private Paint mBitmapPaint;    /**     * view的宽度     */    private int mWidth;    /**     * 3x3 矩阵,主要用于缩小放大     */    private Matrix mMatrix;    /**     * 渲染图像,使用图像为绘制图形着色     */    private BitmapShader mBitmapShader;    /**    * 圆角的半径    */    private int mRadius;    public RoundImageView(Context context) {        this(context,null);    }    public RoundImageView(Context context, AttributeSet attrs) {        this(context, attrs,-1);    }    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        mMatrix = new Matrix();        mBitmapPaint = new Paint();        mBitmapPaint.setAntiAlias(true);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        mWidth = Math.min(getMeasuredWidth(),getMeasuredHeight());        mRadius = mWidth / 2;        setMeasuredDimension(mWidth,mWidth);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        Drawable drawable = getDrawable();        if (drawable == null) {            return;        }else {            Bitmap bitmap = drawableToBitmap(drawable);            drawable.setBounds(0,0,0,0);            mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);            int bSize = Math.min(bitmap.getWidth(), bitmap.getHeight());            float scale = mWidth * 1.0f / bSize;            mMatrix.setScale(scale, scale);            mBitmapShader.setLocalMatrix(mMatrix);            mBitmapPaint.setShader(mBitmapShader);            canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint);        }    }    private Bitmap drawableToBitmap(Drawable drawable){        if ( drawable instanceof BitmapDrawable){            BitmapDrawable bitmapDrawable = (BitmapDrawable)drawable;            return bitmapDrawable.getBitmap();        }        int w = drawable.getIntrinsicWidth();        int h = drawable.getIntrinsicHeight();        Bitmap bitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);        return bitmap;    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);    }}

效果图:这里写图片描述

0 0