Android--动画

在这里插入图片描述

程序界面:
在这里插入图片描述

public class MyViewActivity extends AppCompatActivity implements View.OnClickListener {


    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_view);

        imageView=(ImageView)findViewById(R.id.iv_main);
        iv=(ImageView) findViewById(R.id.ad_main);
        start=(Button)findViewById(R.id.start);
        stop=(Button)findViewById(R.id.stop);

        start.setOnClickListener(this);
        stop.setOnClickListener(this);
    }

    /*
        1,创建动画对象
        2,设置
        3,启动动画
     */

    //缩放
    public void setScale(View view){
        //创建动画对象,
        ScaleAnimation animation=new ScaleAnimation(0.5f,1.5f,0,1, Animation.ABSOLUTE,imageView.getWidth()/2,Animation.ABSOLUTE,0);

        //设置延时
        animation.setStartOffset(1000);
        //持续2s
        animation.setDuration(2000);
        //最终还原
        animation.setFillBefore(true);
        //启动
        imageView.startAnimation(animation);
    }

    public void setXmlScale(View view){
        //定义动画文件
        //加载动画文件,返回Animation
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale_test);
        //启动
        imageView.startAnimation(animation);
    }

    //透明度动画
    public void setAlpha(View view){
        AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);
        alphaAnimation.setDuration(5000);
        imageView.startAnimation(alphaAnimation);
    }
    public void setXmlAlpha(View view){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha_test);
        imageView.startAnimation(animation);
    }

    //旋转动画
    //角度的数值负度数是逆时针,正度数顺时针
    public void setRotate(View view){
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(3600);
        imageView.startAnimation(rotateAnimation);
    }

    public void setXmlRotate(View view){

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_test);
        imageView.startAnimation(animation);

    }

    //平移动画
    public void setTranslate(View view){
        TranslateAnimation animation= new TranslateAnimation(Animation.ABSOLUTE,0,Animation.RELATIVE_TO_SELF,1,Animation.ABSOLUTE,0,Animation.RELATIVE_TO_SELF,1);
        animation.setDuration(5000);
        imageView.startAnimation(animation);
    }


    public void setXmlTranslate(View view){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate_test);
        imageView.startAnimation(animation);
    }


    //复合动画
    public void setAnimationSet(View view){
        AlphaAnimation alphaAnimation=new AlphaAnimation(0,1);
        alphaAnimation.setDuration(3000);

        RotateAnimation animation=new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(3000);
        animation.setStartOffset(3000);


        AnimationSet set=new AnimationSet(true);
        set.addAnimation(alphaAnimation);
        set.addAnimation(animation);

        imageView.startAnimation(set);
    }


    public void setXmlAnimationSet(View view){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_set_test);
        imageView.startAnimation(animation);
    }



    public void animationListener(View view){

        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate_test);
        animation.setRepeatCount(Animation.INFINITE);
        animation.setInterpolator(new LinearInterpolator());
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e("TAG","开始");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.e("TAG","停止");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.e("TAG","重复");
            }
        });

        imageView.startAnimation(animation);

    }



    private ImageView iv;
    private AnimationDrawable ad;

    private Button start;
    private Button stop;

    @Override
    public void onClick(View v) {
        if(v==start){
            if(ad==null) {
                ad = (AnimationDrawable) iv.getBackground();
                ad.start();
            }
        }else if(v==stop){
            if(ad!=null) {
                ad.stop();
                ad=null;
            }
        }
    }
}

如果是xml形式的动画,需要在在res目录下创建xml文件。选择文件类型是animation。这时候系统会自动创建anim文件夹

下面代码是各种动画的xml文件例子:

透明度动画

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromAlpha="0"
    android:toAlpha="1"
    android:duration="5000">

</alpha>

复合动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="0"
        android:toAlpha="1"
        android:duration="3000" />
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:duration="3000"
        android:startOffset="3000"
        android:pivotY="50%"
        android:pivotX="50%"/>
</set>

旋转动画

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="360"
    android:pivotY="50%"
    android:pivotX="50%"
    android:duration="1000">

</rotate>

缩放动画

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:pivotX="100%"
    android:pivotY="100%"
    android:startOffset="1000"
    android:duration="3000"
    android:fillAfter="true">

</scale>

平移动画

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="100%"
    android:toYDelta="100%"
    android:duration="5000">

</translate>

还有一种就是图片最下面那种。

这种实现的步骤是:
在drawable目录下创建一个xml文件(普通的)。之后将类型改为:animation-list
看下这里的例子代码:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <!--上面这个是:是否播放一次的意思,如果是false就是无限循环-->

    <item
        android:drawable="@drawable/p1"
        android:duration="500"/>
    <item
        android:drawable="@drawable/p2"
        android:duration="500"/>

    <item
        android:drawable="@drawable/p3"
        android:duration="500"/>

    <item
        android:drawable="@drawable/p4"
        android:duration="500"/>

    <item
        android:drawable="@drawable/p5"
        android:duration="500"/>

    <item
        android:drawable="@drawable/p6"
        android:duration="500"/>


</animation-list>

图片是自己在网上找的。然后就是通过代码进行轮流切换这种原理

发布了117 篇原创文章 · 获赞 1 · 访问量 7042

猜你喜欢

转载自blog.csdn.net/qq_43616001/article/details/104485143