[英]Loading images to different screen sizes and densities in game development?
The code below draws a square and circle on different screen sizes distance between images changes. 下面的代码在不同的屏幕尺寸上绘制一个正方形和一个圆形,以改变图像之间的距离。 It would be more messed up with actual bitmaps and different screen sizes and densities. 实际的位图以及不同的屏幕尺寸和密度可能会更加混乱。 Here is the code 这是代码
package com.badlogic.androidgames;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class ShapeTest extends Activity {
class RenderView extends View {
Paint paint;
public RenderView(Context context) {
super(context);
paint = new Paint();
}
protected void onDraw(Canvas canvas) {
canvas.drawRGB(255, 255, 255);
paint.setColor(Color.RED);
canvas.drawLine(0, 0, canvas.getWidth()-1, canvas.getHeight()-1, paint);
paint.setStyle(Style.STROKE);
paint.setColor(0xff00ff00);
canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, 40, paint);
paint.setStyle(Style.FILL);
paint.setColor(0x770000ff);
canvas.drawRect(100, 100, 200, 200, paint);
invalidate();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(new RenderView(this));
}
}
Here is a pic: 这是一张照片:
How should i proceed? 我应该如何进行?
The problem is that the bounds of your rectangle are not depending on the screen size. 问题在于矩形的边界不取决于屏幕尺寸。
canvas.drawRect(100, 100, 200, 200, paint);
will draw in pixels. 将以像素为单位。
What you need is an appropriate multiplier for the width, height, top and left. 您需要的是宽度,高度,顶部和左侧的适当乘数。 (float)(height / width)
or (float)(width / height)
- depending on wich is larger. (float)(height / width)
或(float)(width / height)
-取决于夹心较大。
canvas.drawRect(leftOffset* ratio, topOffset * ratio, baseWidth * ratio, baseHeight * ratio, paint);
If you use real Bitmaps, you would also need to scale it to the correct size. 如果您使用真实的位图,则还需要将其缩放到正确的大小。
I think this code gets the apropriately sized bitmap: 我认为这段代码获取了适当大小的位图:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
according to this post: Android - does decodeResource scale the bitmap for screen density? 根据这篇文章: Android-DecodeResource可缩放屏幕密度的位图吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.