2016-08-31 11 views
-1

私はちょうどjavaでアプリケーションを開発し始めました.Cでいくつかの経験があります。Activity.java(Androidスタジオで)のコードで、xyはすべてのデバイスで同じように動作します。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; 
} 

罰金workesが、ちょうど私の携帯電話上の(720×1280ピクセル(〜294 ppiのピクセル密度))私が自分のコードをテストした:いくつかの例を与えます。今、私は自分のアプリを公開しましたが、他のデバイスでは、アプリのレイアウトは完全に同期していません(xとyは画面ごとに異なるため、今私にとっては意味があります)。ボタンと写真は罰金workesが、私は、xとyは、他のデバイス上で壊れている使用

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

のようにオブジェクトを移動します。私は相対レイアウトを使用します。

短いストーリーです。 xとyを変更する方法があるので、それは画面に相対的になりますか?それ以外の場合は、コード全体を変更する必要があります。

答えて

0

通常、ハードコーディングされたピクセル値に基づいてプレースメントを使用するのは良い方法ではありません。これは下位互換性を損なうだけでなく、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); 
+0

を行います!しかし、私はxとyの値を変更すると、私はそれを他のデバイスとの互換性を確認しますが、私の元のデバイスは、アプリケーションを正常に実行することはできません、私はそうですか? meteorite1.setY(meteorite1.getY()+ 20)のように置き換えることはできますか?それは相対的になりますか? dpiで? –

+0

@KingRool私の答えを更新しました – CodeMonkey

関連する問題