帧
程序员文章站
2022-02-05 19:29:35
...
package com.example.animation_cc;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.AnimatorSet;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView img;
private Button scale;
private Button translate;
private Button rotate;
private Button alpla;
private Button mix;
private Button animD;
private TranslateAnimation translateAnimatio;
private ScaleAnimation scaleAnimation;
private RotateAnimation rotateAnimation;
private AlphaAnimation alphaAnimation;
private AnimationSet animationSet;
AnimationDrawable animationDrawable;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scale = (Button)findViewById(R.id.scale);
rotate = (Button)findViewById(R.id.rotate);
translate = (Button)findViewById(R.id.move);
alpla = (Button)findViewById(R.id.alphe);
animD = (Button)findViewById(R.id.anmi_drawble);
mix = (Button)findViewById(R.id.mix);
mix.setOnClickListener(this);
img = (ImageView)findViewById(R.id.image);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "xxx", Toast.LENGTH_SHORT).show();
}
});
scale.setOnClickListener(this);
rotate.setOnClickListener(this);
alpla.setOnClickListener(this);
translate.setOnClickListener(this);
animD.setOnClickListener(this);
/**
* 补间动画,java写法
*/
/**
* 位移动画
*fromXDelta 起始点X轴坐标,可以是数值、百分数、百分数p 三种样式,同scale
*fromYDelta 起始点Y轴从标,可以是数值、百分数、百分数p 三种样式
*toXDelta 结束点X轴坐标
*toYDelta 结束点Y轴坐标
*/
translateAnimatio= new TranslateAnimation(0,200,0,200);
translateAnimatio.setDuration(2000);
//translateAnimatio.setFillAfter(true);//保持最后的状态
// translate.startAnimation(translateAnimatio);
/**
* 缩放动画ScaleAnimation
* fromXScale 起始的X方向上相对自身的缩放比例,浮点值,比如1.0代表自身无变化,0.5代表起始时缩小一倍,2.0代表放大一倍;
* toXScale 结尾的X方向上相对自身的缩放比例,浮点值;
* fromYScale 起始的Y方向上相对自身的缩放比例,浮点值
* toYScale 结尾的Y方向上相对自身的缩放比例,浮点值
*/
scaleAnimation = new ScaleAnimation(0f,2f,0f,2f);
//起点加上100,100,不在从0开始缩放
// scaleAnimation = new ScaleAnimation(0f,2f,0f,2f,100,100);
//如果是50%p(代码0.5),那么就是表示在当前的左上角加上父控件宽度的50%做为起始点x轴坐标。
scaleAnimation.setDuration(2000);
scaleAnimation.setRepeatCount(2);//3次
// scale.startAnimation(scaleAnimation);
/**
* 旋转动画
* 1.fromDegrees 为开始旋转的角度,toDegress
* 正值代表顺时针方向度数,负值代码逆时针方向度数,toDegrees为结束时旋转角度,取值同fromDegrees
*/
//rotateAnimation = new RotateAnimation(0,360,0,0);//以控件左上角的0,0起点开始
// rotateAnimation = new RotateAnimation(0,-720,RotateAnimation.RELATIVE_TO_SELF,0.5f
// ,RotateAnimation.RELATIVE_TO_SELF,0.5f);//旋转中心以控件自身中心开始
rotateAnimation = new RotateAnimation(0,360,RotateAnimation.RELATIVE_TO_PARENT,0.5f,
RotateAnimation.RELATIVE_TO_PARENT,0.5f);//以整个父布局的中心开始
rotateAnimation.setDuration(2000);
// rotate.startAnimation(rotateAnimation);
/**
* 透明动画
*fromAlpha 动画开始的透明度,从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
* toAlpha 动画结束时的透明度,也是从0.0 --1.0 ,0.0表示全透明,1.0表示完全不透明
*/
alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(2000);
// alpla.startAnimation(alphaAnimation);
/**
* 组合动画 AnimationSet 类 参数true代表所有的animation都有效
*
*/
// animationSet = new AnimationSet(true);
// animationSet.addAnimation(alphaAnimation);
// animationSet.addAnimation(translateAnimatio);
// animationSet.addAnimation(scaleAnimation);
// animationSet.addAnimation(rotateAnimation);
// animationSet.setDuration(4000);
// animationSet.setFillAfter(true);
// img.startAnimation(animationSet);
/**
* 帧动画java实现 舍弃,还不如xml方便需要把引用的img的src去掉
*/
//创建一个AnimationDrawable
animationDrawable = new AnimationDrawable();
//资源图片
int []ids = {R.drawable.action1,R.drawable.action2,R.drawable.action3,R.drawable.action4};
for(int i=0; i<4;i++){
//for 循环添加每一帧动画
Drawable frame = getResources().getDrawable(ids[i]);
//设定时长
animationDrawable.addFrame(frame,200);
}
//是否循环播放 true代表只播放一次
animationDrawable.setOneShot(false);
img.setBackground(animationDrawable);
animationDrawable.start();
}
@Override
public void onClick(View v) {
switch (v.getId()){
/**
* 补间动画xml写法
*/
case R.id.move:
Animation translate_anmi = AnimationUtils.loadAnimation(MainActivity.this,R.anim.translate);
translate_anmi.setRepeatCount(-1);
img.startAnimation(translate_anmi);
break;
case R.id.rotate:
Animation rotate_anmi = AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate);
img.startAnimation(rotate_anmi);
break;
case R.id.scale:
Animation scale_anmi = AnimationUtils.loadAnimation(MainActivity.this,R.anim.scale);
img.startAnimation(scale_anmi);
break;
case R.id.alphe:
Animation alphe_anmi = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
img.startAnimation(alphe_anmi);
break;
case R.id.mix:
Animation anmi = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
img.startAnimation(anmi);
break;
/**
* 帧动画xml写法
*/
case R.id.anmi_drawble:
// AnimationDrawable anmi_drawble = (AnimationDrawable)img.getDrawable();
// anmi_drawble.setOneShot(false);
// if (anmi_drawble.isRunning())
// anmi_drawble.stop();
// else
// anmi_drawble.start();
break;
default:
break;
}
}
}
上一篇: css3的animate动画属性小记