[英]canvas.drawBitmap() doesn't scale images
I'm a newbe when it comes to Android coding.我是 Android 编码的新手。 As a first project I decided to port ZX Spectrum game.作为第一个项目,我决定移植 ZX Spectrum 游戏。 I have prepared bitmap frames of the car and buttons that are about to be scaled depending on screen size.我准备了汽车的 bitmap 框架和即将根据屏幕尺寸缩放的按钮。 I've put the images both in "drawable" and "drawable-hdpi" folders and it seems to work on my LG Leon phone, but not working on other phones.我将图像放在“drawable”和“drawable-hdpi”文件夹中,它似乎可以在我的 LG Leon 手机上使用,但不能在其他手机上使用。 It looks like the image isn't scaled.看起来图像没有缩放。 https://mechanism.fr/misc/not_scaled.png https://mechanism.fr/misc/not_scaled.png
here's my code: declaration of the variables:这是我的代码:变量的声明:
Rect source1;
Rect dest1;
Rect arrow_box;
Rect left_box;
Rect right_box;
Rect up_box1;
Rect down_box1;
Rect up_box2;
Rect down_box2;
example of calculating one pair of the Rect dimensions:计算一对 Rect 尺寸的示例:
private void calculateDimensions(){
(...)
source1=new Rect(0,0,128,100);
arrow_box=new Rect(0,0,300,300);
left_box=new Rect((int)(0+boxxy/2),(int)(wysokosc*0.7),(int)(0+boxxy/2+boxxy),(int)(wysokosc*0.7+boxxy));
and the code responsible for drawing:以及负责绘图的代码:
Paint drawPaint2 = new Paint();
canvas.drawBitmap(arrow_left_off,arrow_box,left_box,drawPaint2);
canvas.drawBitmap(arrow_right_off,arrow_box,right_box,drawPaint2);
canvas.drawBitmap(arrow_up_off,arrow_box,up_box1,drawPaint2);
canvas.drawBitmap(arrow_down_off,arrow_box,down_box1,drawPaint2);
canvas.drawBitmap(arrow_up_off,arrow_box,up_box2,drawPaint2);
canvas.drawBitmap(arrow_down_off,arrow_box,down_box2,drawPaint2);
the dimensions are calculated after creating the canvas view:创建 canvas 视图后计算尺寸:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
kanwas=new CanvasV(this);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(kanwas);
getDimensions();
calculateDimensions();
updateScreen();
}
I don't quite understand what seems to be the problem.我不太明白似乎是什么问题。
I tried to copy the images both to "drawable" and "drawable-hdpi" folders我试图将图像复制到“drawable”和“drawable-hdpi”文件夹
Also, scale the bitmaps.此外,缩放位图。 Scaling the canvas doesn't do anything.缩放 canvas 没有任何作用。
Example of scaling bitmap缩放示例 bitmap
bitmap=Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter) bitmap=Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.