TransitionDrawable与矢量drawables

时间:2018-04-11 14:09:06

标签: android android-animation android-vectordrawable

我在xml中定义了TransitionDrawable,如下所示:

transition.xml

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/ic_disabled" />
    <item android:drawable="@drawable/ic_enabled" />
</transition>

我用它来设置复选框状态更改的动画:

val stateDrawable = ContextCompat.getDrawable(this, R.drawable.transition) as TransitionDrawable
checkbox.buttonDrawable = stateDrawable
checkbox.setOnCheckedChangeListener { icon, checked -> 
    if (checked) {
        stateDrawable.startTransition(300) 
    } else {
        stateDrawable.reverseTransition(300)
    }
}

如果@drawable/ic_disabled@drawable/ic_enabledpng个文件,那么一切正常。但如果它们是矢量绘图,那么转换就不起作用了。我错过了什么? TransitionDrawable不支持vector drawables吗?

2 个答案:

答案 0 :(得分:1)

由于我只想通过淡入淡出来更改图像,因此我实现了TransitionDrawable的简单版本,该版本似乎适用于固定大小的矢量可绘制对象。

public class SimpleTransitionDrawable extends Drawable {
    private static final int TRANSITION_STARTING = 0;
    private static final int TRANSITION_RUNNING = 1;
    private static final int TRANSITION_NONE = 2;

    private int state = TRANSITION_NONE;

    private long startTimeMillis;
    private int duration;

    private @Nullable Drawable source;
    private Drawable target;

    public void setTarget(Drawable target) {
        this.source = this.target;
        this.target = target;
        target.setBounds(0, 0, target.getIntrinsicWidth(), target.getIntrinsicHeight());
    }

    public void startTransition(int durationMillis) {
        duration = durationMillis;
        state = TRANSITION_STARTING;
        invalidateSelf();
    }

    @Override
    public void draw(Canvas canvas) {
        int alpha;

        switch (state) {
            case TRANSITION_STARTING:
                startTimeMillis = SystemClock.uptimeMillis();
                alpha = 0;
                state = TRANSITION_RUNNING;
                break;

            case TRANSITION_RUNNING:
                float normalized = (float) (SystemClock.uptimeMillis() - startTimeMillis) / duration;
                alpha = (int) (0xff * Math.min(normalized, 1.0f));
                break;

            default:
                if (target == null) return;
                alpha = 0xff;
                break;
        }

        if (source != null && alpha < 0xff) {
            source.setAlpha(0xff - alpha);
            source.draw(canvas);
        }

        if (alpha > 0) {
            target.setAlpha(alpha);
            target.draw(canvas);
        }

        if (alpha == 0xff) {
            state = TRANSITION_NONE;
            return;
        }

        invalidateSelf();
    }

    @Override public void setAlpha(int alpha) {}

    @Override public void setColorFilter(ColorFilter colorFilter) {}

    @Override public int getOpacity() {return PixelFormat.TRANSLUCENT;}
}

像这样初始化它:

ImageView view = findViewById(R.id.image_view);
SimpleTransitionDrawable drawable = new SimpleTransitionDrawable();
view.setImageDrawable(drawable);

然后使用淡入淡出更改可绘制对象:

drawable.setTarget(AppCompatResources.getDrawable(this, R.drawable.my_vector));
drawable.startTransition(350);

答案 1 :(得分:0)

我知道这很旧,但是我遇到了同样的问题...您必须先将Vector转换为BitmapDrawable,然后再添加到TransitionDrawable中。这是一个例子

            TransitionDrawable td = new TransitionDrawable(new Drawable[]{
                    getBitmapDrawableFromVectorDrawable(this, R.drawable.vector1),
                    getBitmapDrawableFromVectorDrawable(this, R.drawable.vector2)
            });
            td.setCrossFadeEnabled(true); // probably want this

            // set as checkbox button drawable...

实用程序方法//请参见https://stackoverflow.com/a/38244327/114549

public static BitmapDrawable getBitmapDrawableFromVectorDrawable(Context context, int drawableId) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return (BitmapDrawable) ContextCompat.getDrawable(context, drawableId);
    }
    return new BitmapDrawable(context.getResources(), getBitmapFromVectorDrawable(context, drawableId));
}

public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
    Drawable drawable = ContextCompat.getDrawable(context, drawableId);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        drawable = (DrawableCompat.wrap(drawable)).mutate();
    }

    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
            drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}