Android TransitionDrawable有多个项目 [英] Android TransitionDrawable with multiple items

查看:123
本文介绍了Android TransitionDrawable有多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android中创建一个带有文本和背景图片的按钮.背景图像应该每X次淡入淡出.

I want to create a Button in Android with a text, and a background image. The background image should crossfade every X time.

我可以使用带有2张图像的TransitionDrawable进行工作.

I have this working using a TransitionDrawable with 2 images.

但是我不能让它用于2张以上的图像.

But I can't get this to work with more than 2 images.

我有什么:

在Java代码中,我创建一个Button并设置一个背景(这是用XML定义的TransitionDrawable).然后我开始过渡.

In Java code I create a Button and set a background (which is a TransitionDrawable defined in XML). And I start the transition.

final Button b = new Button(getApplicationContext());
b.setTextColor(getResources().getColor(R.color.white));
b.setText("Some text");
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.tile));
StateListDrawable background = (StateListDrawable) b.getBackground();
TransitionDrawable td = (TransitionDrawable) background.getCurrent();
td.startTransition(2000);

在XML中,我在tile.xml中定义

In XML I define in tile.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="#449def" />
        </shape>
    </item>
    <item android:drawable="@drawable/transition">
        <shape>
            <solid
                android:color="#0000ff" />
        </shape>
    </item>
</selector> 

最后是一个transition.xml

And finally a transition.xml

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android"     android:oneshot="false">
  <item android:drawable="@drawable/desert"/> 
  <item android:drawable="@drawable/hydrangeas" /> 
  <item android:drawable="@drawable/jellyfish" /> 
</transition> 

现在的效果是,当我启动该应用程序时,将显示沙漠图像.该图像应根据需要交叉淡入绣球图像.但是,从未显示出水母图像.

Now the effect is that when I start the app the desert image is shown. This image crossfades to the hydrangeas image as it should. But the jellyfish image is never shown.

在TransitionDrawables的文档中,您可以指定两个以上的drawable,但是我无法使用它.

In the doc for TransitionDrawables it is stated that you can specify more than 2 drawables but I can't get this to work.

我也尝试过在没有任何XML的情况下使用纯JAVA进行此操作,但这却带来了完全相同的问题:-(

I also tried this without any XML but in pure JAVA but this gave exactly the same problem :-(

推荐答案

您可以通过使用处理程序来实现

You can do it by using a handler

mAnimateImage is your button

int DrawableImage[] = {R.drawable.back_red, R.drawable.back_green, R.drawable.back_purple};

final Handler handler = new Handler();
    final int[] i = {0};
    final int[] j = {1};
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Resources res = getApplicationContext().getResources();
                    TransitionDrawable out = new TransitionDrawable(new Drawable[]{res.getDrawable(DrawableImage[i[0]]), res.getDrawable(DrawableImage[j[0]])});
                    out.setCrossFadeEnabled(true);
                    mAnimateImage.setBackgroundDrawable(out);
                    out.startTransition(4000);
                    i[0]++;
                    j[0]++;
                    if (j[0] == DrawableImage.length) {
                        j[0] = 0;
                    }
                    if (i[0] == DrawableImage.length) {
                        i[0] = 0;
                    }
                    handler.postDelayed(this, 8000);
                }
            });
        }
    }, 0);

这篇关于Android TransitionDrawable有多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆