【发布时间】:2013-09-12 17:20:18
【问题描述】:
我想创建一个动画图像(就像我正在手工制作一个加号),然后在我点击它后放在我的 android 图像按钮上。如何创建这样的动画图像?并将其放在android中的按钮上?
【问题讨论】:
-
你能发布一些代码来展示你已经尝试过的东西或链接到一些展示你的研究的网站吗?
标签: android image animation button
我想创建一个动画图像(就像我正在手工制作一个加号),然后在我点击它后放在我的 android 图像按钮上。如何创建这样的动画图像?并将其放在android中的按钮上?
【问题讨论】:
标签: android image animation button
为按钮状态准备3张图片,并将其放入“resource/drawable”文件夹中。
button_normal_green.png – 默认图像按钮。
button_focused_orange.png – 当按钮 focused 时显示,例如,当手机的键盘在此按钮上移动(聚焦)时。
button_pressed_yellow.png - 当按钮按下时显示。
为不同的按钮状态添加选择器
现在,在“res/drawable/” 文件夹中创建一个新的 XML 文件,使用您想要的任何名称,在这种情况下,我们只需将名称命名为 “new_button.xml“。该文件定义了哪个按钮状态属于哪个图像。
现在,您可以通过此 ID 引用此按钮:@drawable/new_button。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="@drawable/button_normal_green" />
</selector>
添加按钮
打开“res/layout/main.xml”文件,添加普通按钮,通过“android:background="@drawable/new_button”将背景图片附加到“new_button”上面
文件:res/layout/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:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/new_button" />
</LinearLayout>
根据您需要的信息,您可以在带有Frameanimation 的按钮中设置一堆带有+号的图像和空白图像,您可以在 ImageButton 的 clickEvent 上调用它们
imgButton = (ImageButton) findViewById(R.id.imageButtonid);
imgButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
animate();
}
});
private void animate() {
imageButton = (ImageButton) findViewById(R.id.imageButtonid);
imageButton.setVisibility(ImageView.VISIBLE);
imageButton.setBackgroundResource(R.anim.simple_animation);
AnimationDrawable frameAnimation = (AnimationDrawable) imgView
.getBackground();
frameAnimation.start();
frameAnimation.setOneShot(true);
}
添加的文件:res\anim\simple_animation
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
<item android:drawable="@drawable/monkey_1" android:duration="50" />
<item android:drawable="@drawable/monkey_2" android:duration="50" />
<item android:drawable="@drawable/monkey_3" android:duration="50" />
<item android:drawable="@drawable/monkey_4" android:duration="50" />
<item android:drawable="@drawable/monkey_5" android:duration="50" />
<item android:drawable="@drawable/monkey_6" android:duration="50" />
</animation-list>
【讨论】:
FrameAnimation 为按钮设置动画。