用下面的代码画图时图片总是被放大,超出了屏幕。资料说跟density和targetDensity有关系,当两者不一致时就会被放大或缩小。
1
2
|
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.board);
canvas.drawBitmap(bitmap,
0
,
0
,
null
);
|
为了在画图时保持其原有大小,可以使用以下代码解决:
1
2
3
4
|
DisplayMetrics dm = getResources().getDisplayMetrics();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
bitmap.setDensity((
int
)(bitmap.getDensity() * dm.density +
0
.5f));
canvas.drawBitmap(bitmap,
0
,
0
,
null
);
|
这样解决比较麻烦,必须给每个位图设置density,而且获得的图片高度和宽度仍是放大后的值。
要获取图片的原始大小可以设置这样:
1
2
3
4
5
|
BitmapFactory.Options options =
new
BitmapFactory.Options();
options.inJustDecodeBounds =
true
;
BitmapFactory.decodeResource(getResources(), R.drawable.background, options);
int
rawWidth = options.outWidth;
int
rawHeight = options.outHeight;
|
其中也尝试过设置options.inScaled为false来禁止缩放,结果失败,也不知道咋回事。
最后找到个办法就是将图片放入到drawable-nodpi目录中。在该文件夹中的图片将不会被缩放,9patch图片格式不受此限制。
http://helloandroid.iteye.com/blog/1206610介绍了给ImageView中的图片边缘添加光晕的方法。其实还有种方式是使用BlurMaskFilter来实现:
1
2
3
4
5
6
7
|
BlurMaskFilter blurMaskFilter =
new
BlurMaskFilter(
3
, BlurMaskFilter.Blur.OUTER);
Paint shadowPaint =
new
Paint();
shadowPaint.setMaskFilter(blurMaskFilter);
shadowPaint.setColor(COLOR.BLUE);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.peg);
Bitmap shadowBitmap = bitmap.extractAlpha();
canvas.drawBitmap(shadowBitmap,
null
, peg.getRect(), shadowPaint);
|