使用TransitionDrawable的交叉淡化不适用于android

Jac*_*row 27 animation android transitiondrawable

我有两张想要褪色的图像.最初他们都使用imageview.然后我使用.getDrawable()来获取图像的drawable.

这是我使用的代码

Drawable backgrounds[] = new Drawable[2];
backgrounds[0] = BackgroundImage.getDrawable();
backgrounds[1] = BackgroundImageBlurred.getDrawable();

TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
crossfader.startTransition(3000);
Run Code Online (Sandbox Code Playgroud)

它只显示第一个数组元素上的图像,无论如何它都显示,因为两个图像都设置为在XML中可见.

转型没有开始

任何帮助,将不胜感激 :)

Sim*_*mas 36

这是一个例子,如果你有2个drawable并且想要在某些中设置过渡动画ImageView:

package com.example.app;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.widget.ImageView;

 class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Drawable backgrounds[] = new Drawable[2];
        Resources res = getResources();
        backgrounds[0] = res.getDrawable(android.R.drawable.btn_star_big_on);
        backgrounds[1] = res.getDrawable(android.R.drawable.btn_star_big_off);

        TransitionDrawable crossfader = new TransitionDrawable(backgrounds);

        ImageView image = (ImageView)findViewById(R.id.image);
        image.setImageDrawable(crossfader);

        crossfader.startTransition(3000);

    }
}
Run Code Online (Sandbox Code Playgroud)

然后,如果要转换回原始图像,可以调用

// Make sure the transition occurred
crossfader.startTransition(0);
// Reverse transition
crossfader.reverseTransition(3000);
Run Code Online (Sandbox Code Playgroud)

如果我误解了你的问题,请纠正我.

  • 应该注意的是,具有半透明背景的交叉渐变不起作用,这将使两个可绘制的末端重叠. (5认同)
  • 你忘了添加`crossfader.setCrossFadeEnabled(true);` (3认同)

ces*_*rds 13

你应该使用setCrossFadeEnabledpropery

final TransitionDrawable briefcaseTransition = (TransitionDrawable) briefcase.getDrawable();
briefcaseTransition.setCrossFadeEnabled(true);
briefcaseTransition.startTransition(500);
Run Code Online (Sandbox Code Playgroud)


小智 6

也许你喜欢这个
- res/drawable/transition.xml:

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>
Run Code Online (Sandbox Code Playgroud)

图像视图:

<ImageButton android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" /> 
Run Code Online (Sandbox Code Playgroud)

代码:

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);
Run Code Online (Sandbox Code Playgroud)

如果有帮助,您可以查看 Google-Dev:

http://developer.android.com/guide/topics/resources/drawable-resource.html


aar*_*gas 5

If you are using Vectors, your VectorDrawables won't be cross-faded in the TransitionDrawable.

您需要先将它们转换为BitmapDrawables。

见我的答案在这里/sf/answers/3820875061/为例