在一些显示用户头像的时候,大多数都是显示圆形的,而不是显示一个正方形或者长方形,这样显得很呆板,视觉效果也不好看,今天就用二种方式实现图片圆形显示的效果,在先讲这个效果实现之前,讲下BitmapShader到底能干嘛,android有几个关于渲染的类,使用它能实现一些不错的效果,比如今天讲的BitmapShader的就是关于图像渲染,还有其他4种,这都会再以后都会以博客的形式写出来,今天就单独讲讲BitmapShader,进入BitmapShader源码发现这是你见过最简单的系统类源码了
package android.graphics;
public class BitmapShader extends android.graphics.Shader {
public BitmapShader(android.graphics.Bitmap bitmap, android.graphics.Shader.TileMode tileX, android.graphics.Shader.TileMode tileY) { /* compiled code */ }
}
这构造函数中就三个参数,简单说明下:
第一个bitmap:就是你渲染图像对象
第二个参数tileX:就是你x轴方向渲染模式
第三个参数tileY:就是你y轴方向渲染模式
关于渲染模式有三种:
从上面的代码中知道BitmapShader是继承了Shader类,而模式就是定义在Shader中的一个枚举而已
package android.graphics;
public class Shader {
public Shader() { /* compiled code */ }
public boolean getLocalMatrix(android.graphics.Matrix localM) { /* compiled code */ }
public void setLocalMatrix(android.graphics.Matrix localM) { /* compiled code */ }
protected void finalize() throws java.lang.Throwable { /* compiled code */ }
public static enum TileMode {
CLAMP, MIRROR, REPEAT;
private TileMode() { /* compiled code */ }
}
}
从代码中我们就清晰的看到定义的TileMode有三种
CLAMP:当view控件宽或者高大于你渲染图形的大小时,拉伸最后一个像素去铺满剩下的地方
MIRROR:当view控件宽或者高大于你渲染图形的大小时,通过镜像翻转铺满剩下的地方
REPEAT:当view控件宽或者高大于你渲染图形的大小时,重复图片平铺整个画面(电脑设置壁纸)
等下会说这个三个模式,先把第一种实现圆形的方式代码贴下:
package com.bitmapshaderdemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* Created by admin on 2016/11/22.
*/
public class RoundHeadView extends View {
private static final String TAG ="RoundHeadView" ;
private Paint mPaint;
private ShapeDrawable shapeDrawable;
private BitmapShader bitmapShaderp;
public RoundHeadView(Context context) {
this(context,null);
}
public RoundHeadView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public RoundHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
init();
setBackgroundColor(Color.RED);
}
private void init() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.gird);
Log.e(TAG,"宽-"+bitmap.getWidth()+"-"+"高"+bitmap.getHeight());
shapeDrawable = new ShapeDrawable(new OvalShape());
bitmapShaderp = new BitmapShader(bitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
shapeDrawable.getPaint().setShader(bitmapShaderp);
}
private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
shapeDrawable.draw(canvas);
}
}
运行起来看效果:
发现屏幕一片空白,这是为什么呢? 是因为你没给图片显示设置位置,哪可能会说我我这个自定义view的宽和高就是屏幕的宽和高,但是你必须要给shapeDrawable设置显示区域,
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
shapeDrawable.setBounds(200,200,400,400);
shapeDrawable.draw(canvas);
}
我们在onDraw()方法中指定了显示的区域,就是一个矩形区域,现在运行看效果
ok,一个美女头像出来了,这是一种实现方式,下面不使用ShapeDrawable再实现下:
package com.bitmapshaderdemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;