TransitionDrawable with vector drawables

前端 未结 2 1523
南旧
南旧 2021-01-15 20:00

I have TransitionDrawable defined in xml like this:

transition.xml


<         


        
相关标签:
2条回答
  • 2021-01-15 20:26

    Since all I wanted was to change the image with fade, I implemented a simple version of TransitionDrawable that seem to work for fixed size vector drawables.

    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;}
    }
    

    Initialize it like this:

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

    Then change the drawable with fade:

    drawable.setTarget(AppCompatResources.getDrawable(this, R.drawable.my_vector));
    drawable.startTransition(350);
    
    0 讨论(0)
  • 2021-01-15 20:26

    I know this is old, but I had the same issue... You gotta convert the Vector to a BitmapDrawable before adding to the TransitionDrawable. Here's an example

                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...
    

    Utility Methods // see 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;
    }
    
    0 讨论(0)
提交回复
热议问题