Загрузка анимации с помощью кнопок в Android - PullRequest
1 голос
/ 22 августа 2011

У меня есть активность в Android, которую я установил кнопку под SurfaceView. И результат выглядит так:

И я доволен этим, за исключением одной вещи: надпись на кнопке неверна и должна быть правильно настроена. Для этого я использовал анимацию, подобную этой:

takePhoto = (Button) findViewById(R.id.btnPhoto);
takePhoto.setText(t);

RotateAnimation ranim = (RotateAnimation)AnimationUtils.loadAnimation(this, R.anim.myanim);
ranim.setFillAfter(true);
takePhoto.setAnimation(ranim);

ГДЕ res/anim/myanim выглядит так:

<?xml version="1.0" encoding="utf-8"?>
<rotate  xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromDegrees="0" 
       android:toDegrees="-90"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="0" />

Но результат немного разочаровывает, потому что все выглядит так:

enter image description here

Текст в порядке, но размеры уменьшены.

То, что я хочу, это чтобы текст был правильно сохранен и имел начальный размер. Как я могу это сделать ??? Это не имеет ничего общего со свойствами кнопки в XML-файле, потому что я не менял их при загрузке анимации .... Идеи? Спасибо ...

РЕДАКТИРОВАТЬ: xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>

<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="@+id/surface_camera" />

<RelativeLayout android:layout_width="fill_parent"
android:layout_toRightOf="@id/surface_camera"
android:layout_height="fill_parent"
 >

<Button android:layout_width="680dip"
android:layout_height="680dip" 
android:id="@+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo" 
android:layout_gravity="bottom|right" />

</RelativeLayout>

</RelativeLayout>





IMPORTANT:

In the cases described below...my activity is in `LANDSCAPE` mode and the position of the phone is portrait.Another issue is that when I turn my phone in landscape mode(and here I mean the position of the phone, cause the activity is still landscape) the button doesn't go at the bottom of the phone is stays there.No matter how I turn the phone the button is in the same position!!!!

Ответы [ 2 ]

2 голосов
/ 23 августа 2011

Вы должны прочитать это

Создайте две папки макета, как показано ...

enter image description here

В обеих папках ваш файл макетаимя должно быть таким же.

В папке layout-port ваш файл макета должен выглядеть следующим образом ...

android:layout_toRightOf="@id/surface_camera"

заменен на ...

android:layout_Below="@id/surface_camera"

какследует ...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
>

<SurfaceView android:layout_width="450dip"
android:layout_height="fill_parent" android:id="@+id/surface_camera" />

<RelativeLayout android:layout_width="fill_parent"
android:layout_toBelow="@id/surface_camera" 
android:layout_height="fill_parent"
 >

<Button android:layout_width="680dip"
android:layout_height="680dip" 
android:id="@+id/btnPhoto"
android:layout_alignParentBottom="true"
android:text="Take Photo" 
android:layout_gravity="bottom|right" />

</RelativeLayout>

</RelativeLayout>

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

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //setContentView(R.layout.main);

      int orientation = getResources().getConfiguration().orientation;

      if(orientation == Configuration.ORIENTATION_PORTRAIT) {
         setContentView(R.layout.main1);

      }
      else {

         setContentView(R.layout.main2);
      }
   }

Также Если вы делаете это не нужночтобы создать две папки макета, но для этого нужны два файла макета main1 и main2 в одной папке

Еще один вариант ...

В манифесте ...

<activity android:name=".ActivityName" android:configChanges="keyboardHidden|orientation">

+

В вашей деятельности ...

 @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);

      int orientation = newConfig.orientation;
      if(orientation == Configuration.ORIENTATION_PORTRAIT) {

        setContentView(R.layout.main2);
      }
      else {

        setContentView(R.layout.main);
      }
    }
0 голосов
/ 23 августа 2011

Попробуйте вместо этого XML:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="fill_parent"
                    android:orientation="horizontal"
                    android:layout_height="fill_parent">

    <SurfaceView    android:id="@+id/surface_camera" 
                    android:layout_width="450dip"
                    android:layout_height="fill_parent" />

    <FrameLayout    android:layout_width="fill_parent"
                    android:layout_toRightOf="@id/surface_camera"
                    android:layout_height="fill_parent">
        <Button     android:layout_width="match_parent"
                    android:layout_height="match_parent"/>
        <TextView   android:id="@+id/btnPhoto"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" 
                    android:text="Take Photo" />
    </FrameLayout>

    </RelativeLayout>

Вы будете вращать текст, а не кнопку.

...