【发布时间】:2010-05-25 05:26:31
【问题描述】:
我正在为徽标添加发光动画效果。到目前为止,我已经设法使用 LayeredDrawable 获得了徽标背后的发光图像,但我不知道如何对其进行动画处理。我发现 AlphaAnimation 会达到预期的效果,但不幸的是我只能将它应用于 Views,而不是 Drawables。怎样才能达到这个效果?
【问题讨论】:
标签: android animation drawable
我正在为徽标添加发光动画效果。到目前为止,我已经设法使用 LayeredDrawable 获得了徽标背后的发光图像,但我不知道如何对其进行动画处理。我发现 AlphaAnimation 会达到预期的效果,但不幸的是我只能将它应用于 Views,而不是 Drawables。怎样才能达到这个效果?
【问题讨论】:
标签: android animation drawable
简单示例
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();
}
}
});
方法getAlpha()在api 19中添加。但限制不是很大,可以将状态保存在局部变量中。 ObjectAnimator 加入Android 3.0 (api 11),可能旧版Android 可以使用nineoldandroids。我没有用 Nineoldandroids 测试。
【讨论】:
Android 3.0 引入Property Animations。
不幸的是,这仅限于 Android 3.0 及更高版本,不会很快出现在手机上。
【讨论】:
谢谢@AndreyNick,它就像一个魅力! 我也将它用于 LayerDrawable 用于将一个 Drawable(一层)动画化到其中。 这是代码,也许对某人有用:
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();
我需要为操作栏创建一个可绘制对象:
我使用 Picasso 加载徽标,我喜欢在加载后对其进行动画处理(位图 onBitmapLoaded 回调)。
我希望这会有所帮助!
【讨论】:
我在显示可绘制对象的 ImageView 上使用动画。我认为这在你的情况下也应该是可能的。
【讨论】: