Bitmap 绘制图片 常用的方法有一下几种
(1) drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)
(2) drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint)
(3) drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint)
1 drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)
1.1 使用BitmapFactory解析图片
-
paint.setAntiAlias(
true);
-
// 设置画笔的style (Paint.Style.FILL填充,Paint.Style.STROKE描边,Paint.Style.FILL_AND_STROKE填充加描边 )
-
paint.setStyle(Paint.Style.FILL);
-
// 设置画笔的颜色 这里只是绘制图片感觉用不到就注释掉了
-
// paint.setColor(Color.RED);
-
// paint.setStrokeWidth(10f);
-
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);
-
canvas.drawBitmap(bitmap,
100,
100, paint);
或者
-
paint.setAntiAlias(
true);
-
// 设置画笔的style (Paint.Style.FILL填充,Paint.Style.STROKE描边,Paint.Style.FILL_AND_STROKE填充加描边 )
-
paint.setStyle(Paint.Style.FILL);
-
// 设置画笔的颜色 这里只是绘制图片感觉用不到就注释掉了
-
// paint.setColor(Color.RED);
-
// paint.setStrokeWidth(10f);
-
// Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);
-
@SuppressLint("ResourceType")
-
Bitmap bitmap =BitmapFactory.decodeStream(getResources().openRawResource(R.mipmap.girl));
-
canvas.drawBitmap(bitmap,
100,
100, paint);
效果图
1.2 使用BitmapDrawable解析图片
-
paint.setAntiAlias(
true);
-
// 设置画笔的style (Paint.Style.FILL填充,Paint.Style.STROKE描边,Paint.Style.FILL_AND_STROKE填充加描边 )
-
paint.setStyle(Paint.Style.FILL);
-
// 设置画笔的颜色 这里只是绘制图片感觉用不到就注释掉了
-
// paint.setColor(Color.RED);
-
// paint.setStrokeWidth(10f);
-
// BitmapDrawable
-
BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.mipmap.girl);
-
// 得到Bitmap
-
Bitmap bitmap = bitmapDrawable.getBitmap();
-
canvas.drawBitmap(bitmap,
100,
100, paint);
效果图如下:
1.3 使用InputStream和BitmapDrawable绘制
-
paint.setAntiAlias(
true);
-
// 设置画笔的style (Paint.Style.FILL填充,Paint.Style.STROKE描边,Paint.Style.FILL_AND_STROKE填充加描边 )
-
paint.setStyle(Paint.Style.FILL);
-
// 设置画笔的颜色 这里只是绘制图片感觉用不到就注释掉了
-
// paint.setColor(Color.RED);
-
// paint.setStrokeWidth(10f);
-
// InputStream得到资源流
-
@SuppressLint("ResourceType")
-
InputStream
in = getResources().openRawResource(R.mipmap.girl);
-
// BitmapDrawable 解析数据流
-
BitmapDrawable bitmapDrawable = new BitmapDrawable(
in);
-
// 得到图片
-
Bitmap bitmap = bitmapDrawable.getBitmap();
-
// 得到Bitmap
-
canvas.drawBitmap(bitmap,
100,
100, paint);
效果图如下:
2 drawBitmap(@NonNull Bitmap bitmap, @NonNull Matrix matrix, @Nullable Paint paint)
Matrix 是变形矩阵 这里先简单的说下其一般的使用方法, new 一下然后使用其方法即可,后面在开一篇Matrix的文章
下面是图片放大的方法如下:
-
paint.setAntiAlias(
true);
-
// 设置画笔的style (Paint.Style.FILL填充,Paint.Style.STROKE描边,Paint.Style.FILL_AND_STROKE填充加描边 )
-
paint.setStyle(Paint.Style.FILL);
-
// 设置画笔的颜色 这里只是绘制图片感觉用不到就注释掉了
-
// paint.setColor(Color.RED);
-
// paint.setStrokeWidth(10f);
-
// decodeResource
-
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);
-
// 得到Bitmap
-
Matrix matrix =
new Matrix();
-
matrix.setScale(
1.2f,
1.2f);
-
canvas.drawBitmap(bitmap, matrix, paint);
效果图如下:
3 drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull RectF dst, @Nullable Paint paint)
注意:src 本次绘制的原区域 dst 本次绘制的目标区域
-
paint.setAntiAlias(
true);
-
// 设置画笔的style (Paint.Style.FILL填充,Paint.Style.STROKE描边,Paint.Style.FILL_AND_STROKE填充加描边 )
-
paint.setStyle(Paint.Style.FILL);
-
// 设置画笔的颜色 这里只是绘制图片感觉用不到就注释掉了
-
paint.setColor(Color.RED);
-
paint.setStrokeWidth(
10f);
-
// decodeResource
-
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.girl);
-
//src 本次绘制的原区域 dst 本次绘制的目标区域
-
Rect src =
new Rect(
0,
0, bitmap.getWidth(), bitmap.getHeight());
-
canvas.drawRect(src, paint);
-
Rect dst =
new Rect(
0,
0,
500,
300);
-
canvas.drawBitmap(bitmap, src, dst, paint);
效果图如下:
上面的方法我们估计在项目中没怎么使用过,在自定义view 中我们为了方便统一的管理一般都是结合xml
来一起加载bitmap 下面也加载一个图片看看
-
public
class TestView extends View {
-
// paint 初始化
-
private Paint paint =
new Paint();
-
-
private Bitmap testBitmap;
-
private
int testBitmapResID;
-
-
public TestView(Context context) {
-
super(context);
-
}
-
-
public TestView(Context context, @Nullable AttributeSet attrs) {
-
super(context, attrs);
-
init(context, attrs);
-
}
-
-
public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
-
super(context, attrs, defStyleAttr);
-
init(context, attrs);
-
}
-
-
private void init(Context context, AttributeSet attrs) {
-
paint.setAntiAlias(
true);
-
// 设置画笔的style (Paint.Style.FILL填充,Paint.Style.STROKE描边,Paint.Style.FILL_AND_STROKE填充加描边 )
-
paint.setStyle(Paint.Style.FILL);
-
// 设置画笔的颜色 这里只是绘制图片感觉用不到就注释掉了
-
paint.setColor(Color.RED);
-
paint.setStrokeWidth(
10f);
-
-
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FindImage);
-
testBitmapResID = typedArray.getResourceId(R.styleable.FindImage_test_image, R.mipmap.girl);
-
typedArray.recycle();
-
-
}
-
-
@Override
-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
}
-
-
@Override
-
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
-
super.onLayout(changed, left, top, right, bottom);
-
}
-
-
@Override
-
protected void onDraw(Canvas canvas) {
-
super.onDraw(canvas);
-
testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);
-
canvas.drawBitmap(testBitmap,
100,
100, paint);
-
}
-
-
}
attr.xml 的代码如下:
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<declare-styleable name="FindImage">
-
<attr name="test_image" format="reference" />
-
</declare-styleable>
-
</resources>
效果图如下:
上面的这个绘制图片的大家有没有发现,我没有放布局的xml 使用的默认的图片,当然我们也可以不使用默认的的图片
就是这个地方的
下面修改下默认的图片为android icon 然后在看下效果
自定义view:
-
public
class TestView extends View {
-
// paint 初始化
-
private Paint paint =
new Paint();
-
-
private Bitmap testBitmap;
-
private
int testBitmapResID;
-
-
public TestView(Context context) {
-
super(context);
-
}
-
-
public TestView(Context context, @Nullable AttributeSet attrs) {
-
super(context, attrs);
-
init(context, attrs);
-
}
-
-
public TestView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
-
super(context, attrs, defStyleAttr);
-
init(context, attrs);
-
}
-
-
private void init(Context context, AttributeSet attrs) {
-
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FindView);
-
testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);
-
testBitmapResID = typedArray.getResourceId(R.styleable.FindView_test_image,R.drawable.ic_launcher_background);
-
typedArray.recycle();
-
-
}
-
-
@Override
-
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
}
-
-
@Override
-
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
-
super.onLayout(changed, left, top, right, bottom);
-
}
-
-
@Override
-
protected void onDraw(Canvas canvas) {
-
super.onDraw(canvas);
-
paint.setStyle(Paint.Style.FILL);
-
testBitmap = BitmapFactory.decodeResource(getResources(), testBitmapResID);
-
canvas.drawBitmap(testBitmap,
100,
100, paint);
-
}
att.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<declare-styleable name="FindImage">
-
<attr name="test_image" format="reference" />
-
</declare-styleable>
-
</resources>
在布局中引用图片:
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
xmlns:app=
"http://schemas.android.com/apk/res-auto"
-
xmlns:tools=
"http://schemas.android.com/tools"
-
android:layout_width=
"match_parent"
-
android:layout_height=
"match_parent"
-
tools:context=
".MainActivity">
-
-
<com.hly.view.TestView
-
android:id=
"@+id/image"
-
android:layout_width=
"wrap_content"
-
android:layout_height=
"wrap_content"
-
app:test_image=
"@mipmap/girl" />
-
-
-
</LinearLayout>
这里注意test_image 是attr里面的name
效果图如下:
转载:https://blog.csdn.net/qq_33210042/article/details/108602911