学习文章地址
Canvas开篇之drawBitmap方法讲解
[Android]实现RecyclerView和背景同步滑动的效果
说明
demo采用 [Android]实现RecyclerView和背景同步滑动的效果 的实现
目录
1、srcRect的宽高(100 * 100) < 原图宽高,dstRect宽高固定为1000 * 1000
2、srcRect的宽高(900 * 900) > 原图宽高,dstRect宽高固定为1000 * 1000
3、srcRect的宽高 == 原图宽高,dstRect宽高固定为1000 * 1000
4、srcRect的宽高 == 原图宽高,dstRect宽高固定为400 * 400
此Demo总结
1、srcRect的宽高< 原图宽高 < dstRect,先截取原图100 * 100 像素大小(srcRect),直接放大以适配dstRect
2、srcRect的宽高 > 原图宽高< dstRect,先截取原图900 * 900 像素大小(srcRect),最终显示效果比理想的小,会比原图大
3、srcRect的宽高 == 原图宽高< dstRect,直接放大适配dstRect
4、srcRect的宽高 == 原图宽高 > dstRect,dstRect宽高固定为400 * 400,整个图片会自动适配到dstRect宽高
所以如果截取的值都是<=原图宽高,最后都会适配dstRect
方法介绍
canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
bitmap为原图,srcRect表示截取原图哪块区域,dstRect表示需要在手机屏幕哪块区域显示,srcRect位图将被缩放/转换以适合此值的矩形。原图尺寸为650 * 887像素
![](https://img.haomeiwen.com/i1945114/8dece7de75dd0c2a.jpg)
1、srcRect的宽高(100 * 100) < 原图宽高,dstRect宽高固定为1000 * 1000
@Override
public void onDraw(Canvas canvas) {
if (bitmap != null && scrollY < drawHeight) {
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = 100;
srcRect.bottom = 100;
dstRect.left = 0;
dstRect.top = 0;
dstRect.right = 1000;
dstRect.bottom = 1000;
canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
}
super.onDraw(canvas);
}
截取原图100 * 100的像素大小,显示在1000 * 1000的屏幕区域
![](https://img.haomeiwen.com/i1945114/8351e2f30c4eabef.png)
那么1000 * 1000的范围是多大捏
![](https://img.haomeiwen.com/i1945114/fbada53aee39f94f.png)
由此可以见得,是先从原图截取100 * 100的像素大小,然后放大到 1000 * 1000的屏幕上
2、srcRect的宽高(900 * 900) > 原图宽高,dstRect宽高固定为1000 * 1000
if (bitmap != null && scrollY < drawHeight) {
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = 900;
srcRect.bottom = 900;
dstRect.left = 0;
dstRect.top = 0;
dstRect.right = 1000;
dstRect.bottom = 1000;
canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
红色
dstRect.bottom = 200;
paint.setColor(getContext().getResources().getColor(R.color.red_fa533d));
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(dstRect,paint);
蓝色
srcRect.bottom = 100;
paint.setColor(getContext().getResources().getColor(R.color.purple_500));
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(srcRect,paint);
青色
paint.setColor(getContext().getResources().getColor(R.color.teal_200));
paint.setStyle(Paint.Style.FILL);
Rect sourceRect = new Rect(0,0,bitmapWidth,150); 设置原图的宽
canvas.drawRect(sourceRect,paint);
}
super.onDraw(canvas);
![](https://img.haomeiwen.com/i1945114/5b22c3581285cd47.png)
蓝色的是理想截取900 * 900的Rect,青色的是原图宽度,我们截取的宽高是 > 原图的,但是展示效果会比900 * 900 实际的小,会比原图大
3、srcRect的宽高 == 原图宽高,dstRect宽高固定为1000 * 1000
@Override
public void onDraw(Canvas canvas) {
if (bitmap != null && scrollY < drawHeight) {
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = bitmapWidth;
srcRect.bottom = bitmapHeight;
dstRect.left = 0;
dstRect.top = 0;
dstRect.right = 1000;
dstRect.bottom = 1000;
canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
dstRect.bottom = 200;
paint.setColor(getContext().getResources().getColor(R.color.red_fa533d));
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(dstRect,paint);
srcRect.bottom = 100;
paint.setColor(getContext().getResources().getColor(R.color.purple_500));
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(srcRect,paint);
}
super.onDraw(canvas);
}
![](https://img.haomeiwen.com/i1945114/ca486a761488cc8e.png)
蓝色的是理想截取650* 887的Rect,如果srcRect宽高 == 原图宽高,最后会放大到和dstRect一样的大小
4、srcRect的宽高 == 原图宽高,dstRect宽高固定为400 * 400
@Override
public void onDraw(Canvas canvas) {
if (bitmap != null && scrollY < drawHeight) {
蓝色
srcRect.right = bitmapWidth;
srcRect.bottom = bitmapHeight;
paint.setColor(getContext().getResources().getColor(R.color.purple_500));
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(srcRect,paint);
srcRect.left = 0;
srcRect.top = 0;
srcRect.right = bitmapWidth;
srcRect.bottom = bitmapHeight;
dstRect.left = 0;
dstRect.top = 0;
dstRect.right = 400;
dstRect.bottom = 400;
canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
红色
dstRect.bottom = 200;
paint.setColor(getContext().getResources().getColor(R.color.red_fa533d));
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(dstRect,paint);
}
super.onDraw(canvas);
}
![](https://img.haomeiwen.com/i1945114/1b31bad13b5ebd39.png)
蓝色的是理想截取650* 887的Rect,如果srcRect宽高 == 原图宽高 && dstRect宽高是400 * 400,会发现会自动适配到dstRect的宽高
网友评论