到目前为止,我了解了动画的工作原理以及如何根据here描述的状态设置按钮的背景。
好吧,我定义了一个动画:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="50"
android:repeatMode="reverse"
android:repeatCount="6"/>
我在onClick(View v)方法中启动动画。现在的问题是,在动画结束之前处理实际的点击动作。我知道我可以使用AnimationListener,但这对我来说不太好看,因为我必须在AnimationListener中调用实际的点击过程。
有人知道一个更有技巧的方法让按钮在点击后闪烁吗?
答案 0 :(得分:1)
您可以使用Sectors标签,例如Follows:Create a New a xml file并将其放在drawable文件夹中,并将其命名为shadow_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/ask_footer"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/askfooter_hover" />
<item android:drawable="@drawable/ask_footer" />
</selector>
然后转到声明Button的xml:并在Button
中写一个属性 android:background="@drawable/shadow_color"
你完成了。
如果您觉得答案有用,请标记答案。
答案 1 :(得分:0)
onclick
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick (View v){ calcular(1,v); }
}
);
这使得XML中定义的图像在彼此之间交替。
public void calcular(final int p,final View v){
MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
mp.start();
//v.setBackgroundResource(R.drawable.dia1btn_stl2);
final TransitionDrawable transition1 =
(TransitionDrawable) v.getBackground();
Handler blinkHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
transition1.startTransition(70);
break;
case 1:
transition1.resetTransition();
break;
}
super.handleMessage(msg);
}
};
for (int i=0; i<6; i++)
{
Message msg = new Message();
if(i % 2 == 0){
msg.what = 0;
}
else{
msg.what=1;
}
blinkHandler.sendMessageDelayed(msg, i*100);
}
/*mCurrentSeries.clear();
if(calcularctrl == 0){
calcularctrl = 1;
dtdodo = new DownloadImageTask(this , p , codacaovalue);
dtdodo.execute("wwwkjhdijdh");
}*/
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
public void run() {
//v.setBackgroundResource(R.drawable.dia1btn_stl2);
mCurrentSeries.clear();
if(calcularctrl == 0){
calcularctrl = 1;
dtdodo = new DownloadImageTask(outer() , p , codacaovalue);
dtdodo.execute("wwwkjhdijdh");
}
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public acoesdetalhes outer(){
return acoesdetalhes.this;
}
}, 1000);
}
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="@drawable/mes1btn_stl2" />
<item android:drawable="@drawable/mes1btn_prssd2" />
</transition>
此代码部分由用户Alin提供。