【问题标题】:make x y work the same on all devices android studio使 x y 在所有设备上都一样工作 android studio
【发布时间】:2016-08-31 19:26:35
【问题描述】:

我刚开始用 java 开发一个应用程序,我只在 C 方面有一些经验。在我的 Activity.java(在 android studio 中)的代码中,我得到了类似的东西,只是举几个例子:

    meteorite1.setX(meteoritePlacementX(meteorite1.getX()));
    meteorite1.setY(-2000);
    gnome.setX(330);
    gnome.setY(800);
    meteorite2.setX(meteoritePlacementX(meteorite2.getX()));
    meteorite2.setY(meteoritePlacementY(meteorite1.getY()));
    meteorite3.setX(meteoritePlacementX(meteorite3.getX()));
    meteorite3.setY(meteoritePlacementY(meteorite2.getY()));
    meteorite4.setX(meteoritePlacementX(meteorite4.getX()));
    meteorite4.setY(meteoritePlacementY(meteorite3.getY()));
    meteorite5.setX(meteoritePlacementX(meteorite5.getX()));
    meteorite5.setY(meteoritePlacementY(meteorite4.getY()));

    meteoritedestruction1.setX(0);
    meteoritedestruction1.setY(-2000);
    meteoritedestruction2.setX(0);
    meteoritedestruction2.setY(-2000);
    meteoritedestruction3.setX(0);
    meteoritedestruction3.setY(-2000);
    meteoritedestruction4.setX(0);
    meteoritedestruction4.setY(-2000);
    meteoritedestruction5.setX(0);
    meteoritedestruction5.setY(-2000);

    star1.setX(300);
    star2.setX(150);
    star3.setX(50);
    star4.setX(500);
    star5.setX(600);
    star6.setX(350);
    star7.setX(80);
    star8.setX(450);
    tinystar1.setX(302);
    tinystar2.setX(240);
    tinystar3.setX(57);
    tinystar4.setX(660);
    tinystar5.setX(400);

    star1.setY(300);
    star2.setY(-300);
    star3.setY(-100);
    star4.setY(100);
    star5.setY(300);
    star6.setY(500);
    star7.setY(700);
    star8.setY(900);
    tinystar1.setY(300);
    tinystar2.setY(-400);
    tinystar3.setY(-200);
    tinystar4.setY(150);
    tinystar5.setY(30);    

public float meteoritePlacementX(float X){

    float MeteoriteNewX = 0f;

    int random = (int )(Math.random() * 480 - 50);

    MeteoriteNewX = random;

    return MeteoriteNewX;
}

这工作正常,但只是在我测试我的代码的手机(720 x 1280 像素(~294 ppi 像素密度))上。现在我发布了我的应用程序,但在其他设备上,应用程序的布局完全不同步(现在这对我来说很有意义,因为每个屏幕的 x 和 y 都不同)。按钮和图片工作正常,但移动对象像

meteorite1.setY(meteorite1.getY() + 20); 

我使用 x 和 y 的地方在其他设备上已损坏。我使用相对布局。

长话短说;有没有办法改变 x 和 y,所以它变得相对于屏幕?否则我需要更改整个代码。

【问题讨论】:

    标签: java android layout coordinates


    【解决方案1】:

    一般来说,使用基于硬编码像素值的布局并不是一个好的做法。这不仅会破坏向后兼容性,而且还要考虑当 2k+ 手机问世时你必须做什么,你需要一个完整的重构。查看 this 问题和 Guillaume Perrot 的答案,您可以获得相对于用户手机的最大和最小像素值,并使用这些值而不是 480 - 50 和您的星集函数。

    为了运动做

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    int maxWidth = displayMetrics.widthPixels;
    //Make this percentage whatever you want
    float movementPercentage = 0.02
    //Will move the object 2 percent up the y axis 
    meteorite1.setY(meteorite1.getY() + maxWidth*movementPercentage); 
    

    【讨论】:

    • 是的,你完全正确!但是如果我改变 x 和 y 的值,我会使其与另一台设备兼容,但我的原始设备无法正常运行应用程序,对吗?我怎样才能替换像meteorite1.setY(meteorite1.getY() + 20);所以它变得相对?使用 dpi?
    • @KingRool 更新了我的答案
    猜你喜欢
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    相关资源
    最近更新 更多