如何以编程方式为ImageView设置动画

编程入门 行业动态 更新时间:2024-10-28 12:25:47
本文介绍了如何以编程方式为ImageView设置动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我单击该图像视图时,我正在尝试为ImageView设置动画.

I am trying to animate an ImageView when the said image view is clicked.

具体来说,我希望ImageView的尺寸变大(例如,增大.20),并立即缩小到其原始尺寸.

Specifically I want the size of the ImageView gets bigger (say .20 bigger) and the immediately shrink back to its original size).

到目前为止,我一直在尝试使用此代码,但是运气不好.

So far I have been experimenting with this code with no luck.

// thumbLike is the imageView I would like to animate. button.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 2.5f, 1.0f, 2.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnim.setInterpolator(new LinearInterpolator()); scaleAnim.setDuration(1500); thumbLike.startAnimation(scaleAnim); thumbLike.setAnimation(null); } });

有人可以建议我解决问题吗?

Can anyone suggest me with a possible solution?

编辑#1

Hardik4560回答说,它正在通过XML进行工作:

It is working through XML as answered by Hardik4560:

// finally i use this code to execute the animation Animation animationScaleUp = AnimationUtils.loadAnimation(this, R.anim.scale_up); Animation animationScaleDown = AnimationUtils.loadAnimation(this, R.anim.scale_down); AnimationSet growShrink = new AnimationSet(true); growShrink.addAnimation(animationScaleUp); growShrink.addAnimation(animationScaleDown); thumbLike.startAnimation(growShrink);

和XML

SCALE_UP <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="schemas.android/apk/res/android" android:interpolator="@android:anim/linear_interpolator"> <scale android:fromXScale="1.0" android:toXScale="1.5" android:fromYScale="1.0" android:toYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:duration="1000" /> </set> SCALE_DOWN <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="schemas.android/apk/res/android" android:interpolator="@android:anim/linear_interpolator"> <scale android:fromXScale="1.5" android:toXScale="1.0" android:fromYScale="1.5" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="1000" /> </set>

ps:这很尴尬,因为我已经接受了答案.我正在尝试将@tharkbad和@ Hardik4560的答案结合起来,但是现在它的动画效果看起来并不流畅.

ps: this is awkward, since I already accepted an answer. I am trying to combine between @tharkbad and @Hardik4560 's answers yet now the way it animates does not look smooth.

在scale_up动画期间,有点像跳到"动画的结尾,然后立即开始scale_down动画.我想我得再试一下了.

during the scale_up animation, it kinda look like being "skip" to the end of animation and then immediately starting scale_down animation. I guess I have to play around with it a bit.

推荐答案

我用它来实现弹出弹出效果,

I use this to achieve popin popout effect,

看看它对您是否有帮助

弹出.

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="schemas.android/apk/res/android" android:interpolator="@android:anim/bounce_interpolator" > <scale android:pivotX="50%" android:pivotY="50%" android:fromXScale="0.5" android:fromYScale="0.5" android:toXScale="1.0" android:toYScale="1.0" android:duration="500" /> </set>

弹出

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="schemas.android/apk/res/android" android:interpolator="@android:anim/bounce_interpolator" > <scale android:pivotX="50%" android:pivotY="50%" android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="0.0" android:toYScale="0.0" android:duration="500" /> </set>

更多推荐

如何以编程方式为ImageView设置动画

本文发布于:2023-07-27 05:20:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1220575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   动画   ImageView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!