Android: установка x и y pos - PullRequest
1 голос
/ 15 марта 2012

Я новичок в Android.Я начинаю пытаться построить библиотеку методов.Я гораздо удобнее в Java, и, поскольку все во время выполнения, кажется, происходит в Java, я пытаюсь создать методы, связанные с GUI в Java.У меня пока немногое, и я ищу, чтобы определить, в каких точках х и у рисовать объекты.Это то, что у меня есть:

//method to get width or Xpos that is a one size fits all
public int widthRatio(double ratioIn){
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenWidth = dm.widthPixels;      //gets screen height
    double ratio = screenWidth/100;           //gets the ratio in terms of %
    int displayWidth = (int)(ratio*ratioIn);  //multiplies ratio desired % of screen 
    return displayWidth;
}

//method to get height or Ypos that is a one size fits all
public int heightByRatio(double ratioIn){
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenHeight = dm.heightPixels;    //gets screen height
    double ratio = screenHeight/100;          //gets the ratio in terms of %
    int newHeight = (int)(ratio*ratioIn);     //multiplies ratio by desired % of screen
    return newHeight;
}

//sets size of any view (button, text, ...) to a one size fits all screens
public void setSizeByRatio(View object, int width, int height){
    ViewGroup.LayoutParams params = object.getLayoutParams(); // gets params of view
    params.width = widthRatio(width);                         // sets width by portion of screen
    params.height = heightByRatio(height);                    // sets height by portion of screen
}

Итак, если у меня есть кнопка с именем Button, и я говорю setSizeByRatio (button, 25, 50);он устанавливает высоту кнопки на 25% процентов экрана и ее высоту на 50% любого экрана.

Мой главный вопрос - как установить положение x и y, чтобы вы начали рисовать так же, как вы?будет в рег Ява?Я наткнулся на макет (l, t, r, b);но это только устанавливает x и y относительно родителя.

Следующие вопросы - что еще, насколько методы GUI я должен изучить?и я знаю, что это убьет много людей, но как мне комментировать в XML?Я новичок в XML, как и на Android.

1 Ответ

1 голос
/ 15 марта 2012

Это на самом деле не актуально, но оно очищает одну строку кода (может использоваться как справка для очистки большего количества строк кода, если у вас есть больше подобных вещей).

Таким образом,DisplayMetrics применяется к ним обоим, а не вводить их каждый раз, когда вы получаете другую ось.

//method to get width or Xpos that is a one size fits all
DisplayMetrics dm = new DisplayMetrics() {    // This line now applies to both int and gets screen properties
public int widthRatio(double ratioIn){
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenWidth = dm.widthPixels;      //gets screen height
    double ratio = screenWidth/100;           //gets the ratio in terms of %
    int displayWidth = (int)(ratio*ratioIn);  //multiplies ratio desired % of screen 
    return displayWidth;
}

//method to get height or Ypos that is a one size fits all
public int heightByRatio(double ratioIn){
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    double screenHeight = dm.heightPixels;    //gets screen height
    double ratio = screenHeight/100;          //gets the ratio in terms of %
    int newHeight = (int)(ratio*ratioIn);     //multiplies ratio by desired % of screen
    return newHeight;
    }
}
...