组合动画
程序员文章站
2022-01-28 21:52:59
...
package com.dingtao.animotordemo;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Button button;
ImageView goodsImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
// 按钮点击事件
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 设置0到10的整数变化
final ValueAnimator valueAnimator = ValueAnimator.ofInt(60,0);
// 整个事件段是5秒
valueAnimator.setDuration(60*1000);
// 数字均匀变化,也可设置其他的变化方式,先快后慢,先慢后快等......
valueAnimator.setInterpolator(new LinearInterpolator());
// 监听每次改变时的值
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int value = (Integer) animation.getAnimatedValue();
button.setText(value+"");
}
});
valueAnimator.start();
}
});
goodsImage = findViewById(R.id.goods_image);
goodsImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator translationX = ObjectAnimator.ofFloat(goodsImage,"translationX",0,300f);
ObjectAnimator translationY = ObjectAnimator.ofFloat(goodsImage,"translationY",0,500f);
ObjectAnimator rotation = ObjectAnimator.ofFloat(goodsImage,"rotation",0,1060);
AnimatorSet animatorSet = new AnimatorSet(); //组合动画
animatorSet.play(translationX).with(rotation).before(translationY);
// animatorSet.playTogether(translationX,translationY,rotation); //设置动画
animatorSet.setDuration(3000); //设置动画时间
animatorSet.start(); //启动
}
});
}
}
动画开始
package com.dingtao.animotordemo;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
public class MusicButton extends AppCompatImageView implements View.OnClickListener {
private ObjectAnimator objectAnimator;
public static final int STATE_PLAYING =1;//正在播放
public static final int STATE_PAUSE =2;//暂停
public int state;
public MusicButton(Context context) {
super(context);
init();
}
public MusicButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MusicButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
//第一个参数:Object对象
//第二个参数:属性:旋转:rotation;平移:translationX/translationY
//缩放:scaleX/scaleY;透明:alpha
//第三个参数:起始值
//第四个参数:结束值
objectAnimator = ObjectAnimator.ofFloat(this,"rotation",0,360);
objectAnimator.setDuration(1000);
objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.start();//动画开始
state = STATE_PLAYING;
setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (state == STATE_PAUSE){
objectAnimator.resume();//动画重新开始
state = STATE_PLAYING;
}else{
objectAnimator.pause();//动画暂停
state = STATE_PAUSE;
}
}
}
布局
<com.dingtao.animotordemo.MusicButton
android:id="@+id/musicButton"
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@drawable/music" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="@+id/goods_image"
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@mipmap/ic_launcher_round" />
上一篇: Android 动画总结
下一篇: SVGA优雅实现动画
推荐阅读
-
基于CSS3自定义美化复选框Checkbox组合_html/css_WEB-ITnose
-
HTML5实践-使用css3如何完成google涂鸦动画的详解
-
Python3 加C# 并发编程!强强组合!会产生什么样的化学反应?
-
PHP将两个关联数组合并函数提高函数效率
-
一款纯css3实现的机器人看书动画效果_html/css_WEB-ITnose
-
zepto中的动画+Ajax+touch模块+zepto插件
-
nginx+php-fpm组合解决PATHINFO最佳配置
-
给定一个字符串,返回所有的可能组合
-
一款利用纯css3实现的win8加载动画的实例分析
-
由2个值组合成key的STL map排序问题