简体   繁体   English

在游戏开发中将图像加载到不同的屏幕尺寸和密度吗?

[英]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.

相关问题 在Android上重复使用不同屏幕尺寸和密度的可绘制图像 - Reuse drawable images for different screen sizes and densities on Android 在布局方面支持不同的屏幕尺寸和密度 - Supporting different screen sizes and densities in regards to layout 如何针对不同的屏幕密度确定字体大小? - How to target font sizes for different screen densities? 支持不同android屏幕尺寸和密度的技术 - Techniques in supporting different android screen sizes and densities Android:如何创建不同大小和密度的图像? - Android : How to create images for different sizes and densities? Android屏幕大小和密度 - Android Screen Sizes and Densities 使用不同的屏幕尺寸和不同密度的资源可绘制对象 - Working with different screen sizes and different densities for resource drawables 如何为多种屏幕尺寸和密度创建背景图像 - How to create background images for multiple screen sizes and densities android-如何为具有不同密度的不同屏幕尺寸创建avd - android - how to create Avd's for different screen sizes with different densities 针对不同的Android屏幕尺寸/密度扩展Phonegap应用? - Scaling a Phonegap app for different Android screen sizes/densities?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM