How to make drawableLeft Android buttons keep spinning?

I need to create a button that has an icon on the left and text on the right. After clicking the button, I want to see the animation of the rotating image in the place of the left icon on the button.

I know how to rotate an image using ImageView, but this is not useful for my current requirement.

I tried using AnimationDrawable, but it didn’t work either, there is no animation, but only the first png file is shown. This is the same as using the background or leftDrawable buttons to launch AnimationDrawable. The code is as follows:

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>
+5
source share
3 answers

You can try to use AnimationDrawable.

+3

, , . . Android , start() onCreate (Bundle) , onWindowFocusChanged (boolean). :

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

Use LinearLayout (horizontal orientation). Think of it as your button, giving it a button that is convenient for selection. Inside this object place ImageView and TextView. Use OnClickListner on LinearLayout, as if it were a button. Then animate the ImageView.

0
source

All Articles