Apply an Animation on a Drawable in Android

前端 未结 4 1947
慢半拍i
慢半拍i 2020-12-19 05:06

I am adding a glow animation effect to a logo. So far, I have managed to get the glow image behind the logo, using a LayeredDrawable, but I can\'t figure out how to animate

相关标签:
4条回答
  • 2020-12-19 05:31

    Simple example

    final ImageView imageView = (ImageView) findViewById(R.id.animatedImage);
    final Button animated = (Button) findViewById(R.id.animated);
    animated.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Drawable drawable = imageView.getDrawable();
            if (drawable.getAlpha() == 0) {
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(drawable, PropertyValuesHolder.ofInt("alpha", 255));
                animator.setTarget(drawable);
                animator.setDuration(2000);
                animator.start();
            } else {
                ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(drawable, PropertyValuesHolder.ofInt("alpha", 0));
                animator.setTarget(drawable);
                animator.setDuration(2000);
                animator.start();
            }
        }
    });
    

    Method getAlpha() add in api 19. But it's not a big restriction, you can save the status in a local variable. ObjectAnimator add in Android 3.0 (api 11),maybe old version Android you can use nineoldandroids. I didn't test with nineoldandroids.

    0 讨论(0)
  • 2020-12-19 05:39

    I'm using an Animation on the ImageView displaying the drawable. I think this should be possible in your case too.

    0 讨论(0)
  • 2020-12-19 05:40

    Android 3.0 introduced Property Animations.

    Unfortunately, this is limited to Android 3.0 and up which won't get on phones any time soon.

    0 讨论(0)
  • 2020-12-19 05:44

    Thank you @AndreyNick, it works like a charm! I've used it also for a LayerDrawable for animating just one Drawable (a layer) into it. This is the code, maybe could be useful for someone:

    Drawable[] layers = new Drawable[2];
    layers[0] = new ColorDrawable(Color.RED);
    BitmapDrawable bd = new BitmapDrawable(activity.getResources(), bitmap);
    bd.setGravity(Gravity.CENTER);
    Drawable drawLogo = bd;
    layers[1] = drawLogo;
    LayerDrawable layerDrawable = new LayerDrawable(layers);
    
    layers[1].setAlpha(0);
    
    ((AppCompatActivity) activity).getSupportActionBar().setBackgroundDrawable(layerDrawable);
    
    ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(layers[1], PropertyValuesHolder.ofInt("alpha", 255));
    animator.setTarget(layers[1]);
    animator.setDuration(2000);
    animator.start();
    

    I needed to create a drawable for the Action Bar with:

    • a layer (0) which is a background color and
    • a layer (1) with the logo in the middle of it (with fade animation)

    I load the logo with Picasso and I like to animate it when has been loaded (bitmap onBitmapLoaded callback).

    I hope this could help!

    0 讨论(0)
提交回复
热议问题