`
lukey
  • 浏览: 10988 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Libgdx关于载入图片规格的问题

阅读更多

对于libgdx来说,对载入的图片要求是:宽高都必须是2的N次幂的图片才行,否则会提示:texture width and height must be powers of two。

好了,我们来查看一下源码:

		if(Gdx.gl20 == null && (!MathUtils.isPowerOfTwo(width) || !MathUtils.isPowerOfTwo(height)))
			throw new GdxRuntimeException("texture width and height must be powers of two");

 我们再看他这个判断宽高的方法是怎么写的:

	static public boolean isPowerOfTwo (int value) {
		return value != 0 && (value & value - 1) == 0;
	}

 很显然,只有参数为2的N次方时才返回真。那么问题是不是解决了呢?我把代码修改成为了:

	static public boolean isPowerOfTwo (int value) {
		return value != 0 /*&& (value & value - 1) == 0*/;
	}

 这样不就是可以载入任意图了么,哈哈。先别得意太早,测测看。我选了一张64X64的图,50X50的图来测试

 

下面试在模拟器上测试的结果:

这是在模拟器测试的结果

哈哈,真成功了。不错,还是别得意,一切信真机.

 

下面是我用HTC g10测试的结果:

下面试在模拟器上测试的结果:

 

看来真是成功了,不错。不过还是在不同型号的机子上测测才放心

 

 

下面是在三星i9023,i9020上测试出来的结果:

 

 

悲催了,看来不行,这是什么原因呢。

 

这有很大的可能说明是GLSurfaceView的问题,对于GLSurfaceView来说,对于某些机器的硬件来说,必须使用宽高均为2的N次方的图片才能有效的渲染。说明三星的这两款机器的硬件确实不如HTC。口说无凭,看源码:

package com.badlogic.gdx.backends.android.surfaceview;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

public class DefaultGLSurfaceView extends GLSurfaceView{


    final ResolutionStrategy resolutionStrategy;

    public DefaultGLSurfaceView(Context context, ResolutionStrategy resolutionStrategy) {
        super(context);
        this.resolutionStrategy = resolutionStrategy;
    }
 
     。。。。。。

}
 

果然Libgdx是从GLSurfaceView这里继承过来的。当然在这之前,我看挨个打印了框架中图形渲染的部分,发觉确实没有问题。

 

对于这问问题看来,只能抽时间自己再完善一下,要不只能载入2的N次方的图片,这太郁闷了。

 

 

另外测试用的那两个图是我用libgdx写的一个游戏中的2张图,估计这月月底就要上线了了吧。到时候再讨论。

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics