使用android中的按钮加载动画

时间:2011-08-22 13:21:10

标签: android animation button

我在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 个答案:

答案 0 :(得分:2)

您应该阅读this

制作两个布局文件夹,如下所示......

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);
      }
    }

答案 1 :(得分:0)

请尝试使用此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>

您将旋转文本而不是按钮。