使用andengine开发游戏,屏幕适配出现的问题(二)。
转载请注明出处http://blog.csdn.net/qq_33536981/article/details/78197713
通过上一节,我们就可以让我们的图片在所有分辨率的手机上,保证视觉上的一致!但是,由此又会有新的问题出现。先来看一段代码
int x = 300;
int y = 300;
Line h = new Line(scaleWidth*x,0,scaleWidth*x,CAMERA_HEIGHT, getVertexBufferObjectManager());
h.setColor(1,0,0);
scene.attachChild(h);//画一根竖线做参照系
Line s = new Line(0,scaleHeight*y,CAMERA_WIDTH,scaleHeight*y,getVertexBufferObjectManager());
s.setColor(1,0,0);
scene.attachChild(s);//画一根横线做参照系
Sprite sprite = new Sprite(scaleWidth*x,scaleHeight*y,textureRegion,getVertexBufferObjectManager());
sprite.setScale(scaleWidth,scaleHeight);
scene.attachChild(sprite);
下面是在不同分辨率的手机上索展现的不同的效果。
可以看到,图片大小在视觉上是一样大的,但是位置却不一样。其实,这也是andengine的一个问题吧,我们在设定坐标的时候,是对图片的左上角设置坐标,但是旋转,缩放,这些操作是针对图片的中心点所做的,这就造成了这种种的矛盾。
接下来我们看看该怎么解决.
我们设定原图坐标为(x,y),我们需要求出缩放后的图片坐标,这样就将问题转化成了一个简单的数学问题.
缩放后坐标为(X*Sw-W*(1-Sw)/2,Y*Sh-H*(1-Sh)/2)
最后我们测试一下!
这样我们就得到在全屏幕都能适配坐标和屏幕的图片!
最后我写了个屏幕适配的帮助类,大家有需要就根据情况使用,我没有写注解的习惯,大家谅解一下!
public class ScaleHelper {
private final int defaultWidth = 1920;
private final int defaultHeight = 1080;
private static ScaleHelper scaleHelper = new ScaleHelper();
private int CAMERA_HEIGHT;
private int CAMERA_WIDTH;
private float scaleWidth = 1;
private float scaleHeight = 1;
private ScaleHelper() {
}
public static ScaleHelper getInstance() {
return scaleHelper;
}
public void init(Activity activity) {
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
CAMERA_HEIGHT = dm.heightPixels;
CAMERA_WIDTH = dm.widthPixels;
scaleWidth = (float) CAMERA_WIDTH / defaultWidth;
scaleHeight = (float) CAMERA_HEIGHT / defaultHeight;
}
public int getAppWidth() {
return CAMERA_WIDTH;
}
public int getAppHeight() {
return CAMERA_HEIGHT;
}
public float getXLocation(float x, float w) {
return x * scaleWidth - (w - w * scaleWidth) / 2;
}
public float getRealX(float x, float w) {
return x / scaleWidth + w * (1 - scaleWidth) / (2 * scaleWidth);
}
public float getRealY(float y, float h) {
return y / scaleHeight + h * (1 - scaleHeight) / (2 * scaleHeight);
}
public float getYLocation(float y, float h) {
return y * scaleHeight - (h - h * scaleHeight) / 2;
}
public float getHeightScale() {
return scaleHeight;
}
public float getWidthScale() {
return scaleWidth;
}
public float getCenterXLoction(float x, float width) {
return x * scaleWidth - width / 2;
}
public float getRealCenterXLocation(float x, float width) {
return x - width * (1 - scaleWidth) / 2;
}
public float getRealCenterYLocation(float y, float height) {
return y - height * (1 - scaleHeight) / 2;
}
public float getRealCenterX(float x, float width) {
return x - width / 2;
}
public float getRealCenterY(float y, float height) {
return y - height / 2;
}
public float getCenterYLoction(float y, float height) {
return y * scaleHeight - height / 2;
}
public float getNoRealCenterX(float x, float width) {
return x*scaleWidth - width / 2;
}
public float getNoRealCenterY(float y, float height) {
return y*scaleHeight - height / 2;
}
}