2013-09-01 2 views
0

У меня есть приложение, в котором вы нажимаете кнопку, и это переведёт в другую точку на экране.Как активировать кнопку в диапазоне экрана

Вот что я использую для перевода.

findViewById(R.id.clickButton).animate().translationX((float) (Math.random() * (100 - 400))); 
findViewById(R.id.clickButton).animate().translationY((float) (Math.random() * (100 - 400))); 

Хорошо, однако кнопка может быть частично переведена из экрана, а иногда и поверх других виджетов. Есть ли способ избежать этого?

ответ

0

Чтобы получить значение math.random() в диапазоне, который вы хотите, вам нужно будет добавить это -:

bttn1.animate().translationX((float) (randomFromInterval(100,800))); 
bttn2.animate().translationY((float) (randomFromInterval(200,500))); 

public int randomFromInterval(int from,int to) 
{ 
    return (int) Math.floor(Math.random()*(to-from+1)+from); 
} 

И если вы получаете ваши кнопки в верхней части других виджетов, то вы будете иметь разместить кнопки в конце вашего относительного расположения, поскольку он управляет видимостью вид г-ось в соответствии с виджетами, которые вы написали в XML-макете, например -:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Button1" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:text="Button2" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button2" 
     android:layout_below="@+id/button2" 
     android:text="Button3" /> 

</RelativeLayout> 

в выше макете Button3 является toppest вида г -axis, если вам нужно сделать button1 верхний вид из оси z, тогда вы будет перестроен ваш код следующим образом:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 


    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:text="Button2" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button2" 
     android:layout_below="@+id/button2" 
     android:text="Button3" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:text="Button1" /> 


</RelativeLayout> 

Надеюсь, что это объяснение очистит ваши сомнения.

Смежные вопросы