package com.example.dell.mychecknextimages;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
public class BitmapShaderView extends ImageView {
private float mRadius;
private int mWidth;
private Paint mPaint;
private Bitmap mBitmap;
public BitmapShaderView(Context context) {
this(context, null);
}
public BitmapShaderView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BitmapShaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());
mRadius = mWidth * 1.0f / 2;
setMeasuredDimension(mWidth, mWidth);
}
@Override
protected void onDraw(Canvas canvas) {
setBitmapShader(canvas);
canvas.drawCircle(mWidth/2, mWidth/2, mWidth/2, mPaint);
Log.d("tgw", "onDraw: "+mRadius+"---"+mWidth+"===");
// canvas.drawCircle(mRadius,mRadius,mRadius-10.0f,mPaint);
//第二个参数rx:x方向上的圆角半径。第三个参数ry:y方向上的圆角半径。
// canvas.drawRoundRect(new RectF(0, 0, mWidth, mWidth), 250, 250, mPaint);
}
private Bitmap DrawableToBitmap(Drawable drawable) {
//首先明白instanceof是一种运算符是用来在运行时指出对象是否是特定类的一个实例。例如:result = object instanceof class 主要判断前面是否是后面的一个实例 如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false。
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
return bitmapDrawable.getBitmap();
}
int w = drawable.getIntrinsicWidth();
int h = drawable.getIntrinsicHeight();
// ARGB指的是一种 色彩模式,里面A代表Alpha,R表示red,G表示green,B表示blue. ARGB_8888就是由4个8位组成即32位 ,代表32位ARGB位图.
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
private void setBitmapShader(Canvas canvas) {
Drawable drawable = getDrawable();
if (null == drawable) {
return;
} else {
//首先获取到bitmap类型的图片
Bitmap bitmap = DrawableToBitmap(drawable);
//构造渲染器BitmapShader TileMode的取值有三种:CLAMP 拉伸 REPEAT 重复 MIRROR 镜像
BitmapShader mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
int mBitWidth = Math.min(bitmap.getWidth(), bitmap.getHeight());
float scale = mWidth * 1.0f / mBitWidth;
// shader的变换矩阵,我们这里主要用于放大或者缩小
Log.d("tgw", "setBitmapShader:scale: "+scale+"mwidth:"+mWidth+"mBitWidth"+mBitWidth);
Matrix mMatrix = new Matrix();
mMatrix.setScale(scale, scale);
mBitmapShader.setLocalMatrix(mMatrix);
mPaint.setColor(Color.RED);
mPaint.setShader(mBitmapShader);
// canvas.drawPaint(mPaint);
}
}
}
Android自定义圆形图片,使用BitmapShader制作圆形
最新推荐文章于 2024-03-31 23:40:32 发布