如何让Android按钮的drawableLeft保持旋转?
我需要创建一个按钮,左侧有一个图标,右侧有一个文本。按下按钮后,我想看到按钮左侧图标处有一个旋转图像的动画。
我知道如何使用 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>
I need to create a button where there is a icon in the left and a text in the right. After pressing the button, I want to see there is an animation of rotating image at the place of the left icon in the button.
I know how to rotate a image with ImageView, but it is not helpful to my current requirement.
I tried to use AnimationDrawable, but it did not work either, there is no animation but only the first png file shown. It is then same whatever I use the background or leftDrawable of the button to run the AnimationDrawable. The code is as below:
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用
AnimationDrawable
。You can try using an
AnimationDrawable
.我也遇到过这个问题,我已经尝试了上述所有解决方案,但没有人为我工作。我在此处找到了解决方案。 Android文档说不要在activity的onCreate(Bundle)方法中调用start(),而是在onWindowFocusChanged(boolean)函数中调用它。所以我这样做:
I have also face this issue and I have tried all above solution but no one have worked for me. I have found solution here. Android document says that don't call start() in the onCreate(Bundle) method of activity call it in onWindowFocusChanged(boolean) function. So I do it like this :