Android 如何更改和查看按钮背景图像几毫秒?
当我点击一个我想改变的按钮时,在500毫秒内,该按钮将返回图像并做一个简短的动画。我已经完成了一个代码,但是我看不到重新结果,动画还可以,但是单击的图像按钮没有出现 defalut按钮图像为“boutton_a.png” 单击的按钮图像是“button\u a\u ok.png” 以下是onclick按钮代码:Android 如何更改和查看按钮背景图像几毫秒?,android,android-layout,Android,Android Layout,当我点击一个我想改变的按钮时,在500毫秒内,该按钮将返回图像并做一个简短的动画。我已经完成了一个代码,但是我看不到重新结果,动画还可以,但是单击的图像按钮没有出现 defalut按钮图像为“boutton_a.png” 单击的按钮图像是“button\u a\u ok.png” 以下是onclick按钮代码: if (Button04.getText().equals(Reponse)){ Button01.setBackgrou
if (Button04.getText().equals(Reponse)){
Button01.setBackgroundResource(R.drawable.boutton_a_ok);
AnimationSet set=new AnimationSet(true);
Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,10,Animation.RELATIVE_TO_SELF,10);
animation.setDuration(100);
set.addAnimation(animation);
Button01.startAnimation(set);
}else{
Button01.setBackgroundResource(R.drawable.boutton_a_nok);
AnimationSet set=new AnimationSet(true);
Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF,10,Animation.RELATIVE_TO_SELF,10);
animation.setDuration(100);
set.addAnimation(animation);
Button04.startAnimation(set);
}
我该怎么办
编辑:我已经发布了所有代码,因为有两个背景资源不同您可以通过从xml中为不同的按钮状态创建一个可绘制的按钮来更改按钮单击时的按钮背景。下面是一个例子
<selector
<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>
将此文件作为“rotate.xml”放在您的资源文件夹中
res/anim/rotate.xml
并使用该类设置动画
public class DownloadButton extends Button
{
private String state = "Normal";
public DownloadButton( Context context )
{
super(context);
}
public DownloadButton( Context context, AttributeSet attrs )
{
super(context, attrs);
}
public DownloadButton( Context context, AttributeSet attrs, int defStyle )
{
super(context, attrs, defStyle);
}
public void setState(String state)
{
this.state = state;
if (this.state.equals("Downloading"))
{
ButtonAnimate();
}
else if(this.state.equals("Waiting"))
{
setToWaiting();
}
else
{
clearAnimation();
}
}
private void setToWaiting()
{
setBackgroundResource(R.drawable.waiting);
}
public void ButtonAnimate()
{
this.startAnimation(AnimationUtils.loadAnimation(MediaSingleton.getInstance().getContext(), R.anim.rotate));
}
public String getState()
{
return state;
}
}
在布局中使用此按钮,如下所示
<com.yourpackage.DownloadButton
android:id="@+id/Downloadbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_gravity="center_vertical"
android:background="@drawable/download"
/>
Button button=(Button) findViewById(R.id.yourbtnid);
button.setBackgroundResource(R.drawable.downloading);
button.setState("Downloading");
你已经完成了你的动画祝你好运添加计时器并运行方法设置背景图像我已经发布了所有代码。事实上,我想根据字符串“response”更改backgroundressource并制作动画。
Button button=(Button) findViewById(R.id.yourbtnid);
button.setBackgroundResource(R.drawable.downloading);
button.setState("Downloading");