0
我有一個ImageButton,我想突出顯示 - 例如。下載完成後閃爍。轉換爲ImageButton背景
我想要的背景設置爲TransitionDrawable
和重置回當過渡完成,但我得到怪異的結果:
- 按鈕的填充消失,一旦過渡激活和ISN恢復原始繪圖時,恢復原狀。
- 過渡僅發生在按鈕的筆觸(邊框)上,而不是填充背景區域的漸變。
這是Java代碼,我使用激活過渡,扭轉這種局面,恢復原有繪製背景:
downloadButton.setBackgroundResource(R.drawable.animate_background);
Drawable background = downloadButton.getBackground().getCurrent();
if (background instanceof TransitionDrawable) {
TransitionDrawable transition = (TransitionDrawable) background;
transition.startTransition(300);
downloadButton.postDelayed(new Runnable() {
@Override
public void run() {
// reverse the transition after it completes
Drawable background = downloadButton.getBackground().getCurrent();
if (background instanceof TransitionDrawable) {
TransitionDrawable transition = (TransitionDrawable) background;
transition.reverseTransition(200);
downloadButton.postDelayed(new Runnable() {
@Override
public void run() {
// restore original background
downloadButton.setBackgroundResource(R.drawable.custom_button);
}
}, 200); // after reverse transition
}
}
}, 300); // after transition
}
這是在佈局XML按鈕:
<ImageButton
android:id="@id/download_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_button"
android:padding="14dp"
android:src="@drawable/download_icon" />
這是custom_button drawable的xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid android:color="#343434" />
<stroke android:width="1dp" android:color="#171737" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape>
<gradient android:startColor="#343434" android:endColor="#171737" android:angle="270" />
<stroke android:width="1dp" android:color="#171717" />
<corners android:radius="5dp" />
</shape>
</item>
過渡 「animate_background」 XML:
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/animate_start" />
<item android:drawable="@drawable/animate_end" />
</transition>
動畫開始XML(動畫端很相似,只是不同的顏色梯度和中風)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient android:startColor="#34F434" android:endColor="#174717" android:angle="270" />
<stroke android:width="2dp" android:color="#17F717" />
<corners android:radius="5dp" />
</shape>