如何使Android Button的drawableLeft保持旋转?

时间:2011-11-12 11:16:10

标签: android button rotation

我需要创建一个按钮,左边有一个图标,右边有一个文本。按下按钮后,我想在按钮左侧图标的位置看到旋转图像的动画。

我知道如何使用ImageView旋转图像,但它对我当前的要求没有帮助。

我尝试使用AnimationDrawable,但它也没有用,没有动画但只显示了第一个png文件。无论我使用什么背景或leftDrawable按钮来运行AnimationDrawable都是一样的。代码如下:

package com.example.layout;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Button;

public class TestLinearlayoutActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public void onStart() {
        super.onStart();

        Button locationTitleButton = (Button) findViewById(R.id.LocationTitleButton);
        //locationTitleButton.setBackgroundResource(R.drawable.loading);

        locationTitleButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.loading, 0, 0, 0);

        Drawable[] locationTitleButtonDrawables = locationTitleButton.getCompoundDrawables();
        AnimationDrawable animDrawable = (AnimationDrawable) locationTitleButtonDrawables[0];
        //AnimationDrawable animDrawable = (AnimationDrawable) locationTitleButton.getBackground();
        animDrawable.start();
    }
}

//loading.xml:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <item android:drawable="@drawable/loc1" android:duration="200" />
    <item android:drawable="@drawable/loc2" android:duration="200" />
    <item android:drawable="@drawable/loc3" android:duration="200" />
    <item android:drawable="@drawable/loc4" android:duration="200" />

</animation-list>

// layout file, main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="horizontal" android:padding="0dp"
    android:layout_height="wrap_content" android:gravity="fill_horizontal" android:layout_margin="0dp">

    <Button android:id="@+id/LocationTitleButton"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_marginRight="0dip"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:ellipsize="end" 
        android:scrollHorizontally="true" 
        android:singleLine="true"
        android:text="Add location" 
        android:textStyle="bold" />

    <Button android:textColor="#FF000000" 
        android:layout_weight="0" 
        android:id="@+id/AddLocationButton" 
        android:text="Search" 
        android:gravity="center_vertical"
        android:layout_gravity="center_vertical" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_marginLeft="-8dp" />

</LinearLayout>

3 个答案:

答案 0 :(得分:4)

您可以尝试使用AnimationDrawable

答案 1 :(得分:1)

我也面临这个问题而且我已经尝试了所有上述解决方案,但没有人为我工作过。我找到了解决方案here。 Android文件说不要在onCreate(Bundle)方法中调用start(),在onWindowFocusChanged(boolean)函数中调用它。所以我这样做:

@Override
public void onWindowFocusChanged(boolean hasFocus){
    if(hasFocus){
        final AnimationDrawable d=(AnimationDrawable) mBtnView.getCompoundDrawables()[0];
        d.start();
    }
}

答案 2 :(得分:0)

使用LinearLayout(水平方向)。把它作为你的按钮,给它一个按钮可绘选择器,因为它的背景是可绘制的。在里面,放一个ImageView和一个TextView。在LinearLayout上使用OnClickListner,就像它是一个按钮一样。然后为ImageView设置动画。