Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ImageButton的背景转换_Android_Imagebutton_Transitiondrawable - Fatal编程技术网

Android ImageButton的背景转换

Android ImageButton的背景转换,android,imagebutton,transitiondrawable,Android,Imagebutton,Transitiondrawable,我有一个ImageButton,我想突出显示-例如,下载完成后闪烁 我试图将背景设置为一个TransitionDrawable,并在转换完成后将其重置,但我得到了奇怪的结果: 一旦转换被激活,按钮的填充就会消失,当我在完成时恢复原始可绘制图形时,按钮的填充不会恢复 过渡仅发生在按钮的笔划(边框)上,而不是填充背景区域的渐变 这是我用来激活转换、反转转换和恢复原始可绘制背景的Java代码: downloadButton.setBackgroundResource(R.drawable.anim

我有一个ImageButton,我想突出显示-例如,下载完成后闪烁

我试图将背景设置为一个
TransitionDrawable
,并在转换完成后将其重置,但我得到了奇怪的结果:

  • 一旦转换被激活,按钮的填充就会消失,当我在完成时恢复原始可绘制图形时,按钮的填充不会恢复
  • 过渡仅发生在按钮的笔划(边框)上,而不是填充背景区域的渐变
这是我用来激活转换、反转转换和恢复原始可绘制背景的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" />

这是自定义按钮可绘制的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>

转换“动画背景”xml:


动画开始xml(动画结束非常相似,只是渐变和笔划的颜色不同)


在将背景设置为transition之后,通过手动设置填充来解决问题,我必须使用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 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>
downloadButton.setBackgroundResource(R.drawable.animate_background);
downloadButton.setPadding(28, 28, 28, 28);