我正在为徽标添加发光动画效果。到目前为止,我已经设法使用 LayeredDrawable 获得了徽标背后的发光图像,但我不知道如何对其进行动画处理。我发现 AlphaAnimation 会达到预期的效果,但不幸的是我只能将它应用于 Views,而不是 Drawables。我怎样才能达到这个效果?
问问题
14174 次
4 回答
9
简单的例子
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()
add in api 19。不过限制不大,可以将状态保存在局部变量中。ObjectAnimator
添加 Android 3.0 (api 11),也许旧版本的 Android 你可以使用Nineoldandroids。我没有用 Nineoldandroids 进行测试。
于 2014-11-11T14:13:01.790 回答
3
Android 3.0 引入了属性动画。
不幸的是,这仅限于 Android 3.0 及更高版本,不会很快出现在手机上。
于 2011-05-22T00:16:14.033 回答
1
谢谢@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();
我需要为操作栏创建一个可绘制对象:
- 一个图层 (0),它是背景颜色和
- 中间带有徽标的图层 (1)(带有淡入淡出动画)
我使用 Picasso 加载徽标,并且我喜欢在加载时对其进行动画处理(位图 onBitmapLoaded 回调)。
我希望这会有所帮助!
于 2018-04-17T08:06:51.003 回答
-2
我在显示可绘制对象的 ImageView 上使用动画。我认为这在你的情况下也应该是可能的。
于 2010-05-25T08:11:57.217 回答