使用 TransitionDrawable 的淡入淡出不适用于 android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24591384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:27:52  来源:igfitidea点击:

Crossfading using TransitionDrawable not working on android

androidanimationtransitiondrawable

提问by Hyman Sparrow

I have two Images that I want to cross fade. Initially they both use imageview. I then use .getDrawable() to get the drawable of the images.

我有两个要淡入淡出的图像。最初他们都使用 imageview。然后我使用 .getDrawable() 来获取图像的 drawable。

This is the code I used

这是我使用的代码

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

TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
crossfader.startTransition(3000);

It only shows the image on the first array element, which it shows anyway since both images were set to visible in the XML.

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

The transition doesn't start

过渡没有开始

Any help would be appreciated :)

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

回答by Simas

Here's an example IFyou have 2 drawables and want to animate their transition in some ImageView:

这是一个示例,如果您有 2 个可绘制对象并希望在某些情况下为它们的过渡设置动画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);

    }
}

Then if you want to transition back to the original image you can call

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

// Make sure the transition occurred
crossfader.startTransition(0);
// Reverse transition
crossfader.reverseTransition(3000);

Please correct me if I misunderstood your question.

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

回答by cesards

You should use setCrossFadeEnabledpropery

你应该使用setCrossFadeEnabled财产

final TransitionDrawable briefcaseTransition = (TransitionDrawable) briefcase.getDrawable();
briefcaseTransition.setCrossFadeEnabled(true);
briefcaseTransition.startTransition(500);

回答by azuztekztorem

Maybe you like this one
- res/drawable/transition.xml:

也许你喜欢这个
- 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>

ImageView:

图像视图:

<ImageButton android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" /> 

Javacode:

代码:

ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);

If it is helpfull, you can see Google-Dev:

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

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

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

回答by aaronvargas

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

如果您使用的是 Vectors,则您的 VectorDrawables不会在 TransitionDrawable 中淡入淡出。

You'll need to convert them into BitmapDrawables first.

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

See my answer here https://stackoverflow.com/a/54583929/114549for an example

例如,在此处查看我的回答https://stackoverflow.com/a/54583929/114549

回答by matiash

A TransitionDrawableis just a special kind of drawable. It is not drawn by itself, it must be placed somewhere (e.g. on a View background, on an ImageView, &c) to be displayed.

ATransitionDrawable只是一种特殊的可绘制对象。它不是自己绘制的,它必须放置在某处(例如,在视图背景上、在 ImageView 上等)才能显示。

If you're trying to crossfade two images, you have two possible approaches:

如果您尝试对两个图像进行淡入淡出,则有两种可能的方法:

  1. Use a singleImageView with a TransitionDrawable.
  2. Use two ImageViews and crossfade them with animations.
  1. 使用单个ImageView 和TransitionDrawable.
  2. 使用两个 ImageViews 并用动画对它们进行淡入淡出