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

Geo*_*_BJ 6 android rotation button

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

我知道如何使用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>
Run Code Online (Sandbox Code Playgroud)

Com*_*are 4

您可以尝试使用AnimationDrawable.

  • 抱歉,有什么解决方案可以在按钮的可绘制左侧旋转单个位图吗? (2认同)