关闭

View Animation补间动画

54人阅读 评论(0) 收藏 举报
分类:

布局很简单,一个图片,五个监听按钮。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.viewanimation.MainActivity">

    <Button
        android:id="@+id/but"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="translate"
        android:text="位移" />

    <Button
        android:id="@+id/but2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="alpha"
        android:text="透明度" />

    <Button
        android:id="@+id/but3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="rotate"
        android:text="旋转" />

    <Button
        android:id="@+id/but4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="scale"
        android:text="收缩" />

    <Button
        android:id="@+id/but5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="set"
        android:text="全部" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"

        android:background="@mipmap/ic_launcher" />


</LinearLayout>

MainActivity类

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.image);
    }

    //位移动画
    public void translate(View v) {
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.5f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.5f);
        //执行的时间
        translateAnimation.setDuration(1000);
        //执行次数
        translateAnimation.setRepeatCount(8);
        //是否是动画结束时保存最后的状态
        translateAnimation.setFillAfter(true);
        //设置插件
        translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
        //设置动画执行的模式
        //REVERSE  反向
        //RESTART   默认
        translateAnimation.setRepeatMode(Animation.REVERSE);
        //执行动画
        imageView.setAnimation(translateAnimation);
        //开始动画
        imageView.startAnimation(translateAnimation);

    }

    // 透明度
    public void alpha(View v) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
        alphaAnimation.setDuration(3000);
        //动画执行的模式
        //alphaAnimation.setRepeatMode(Animation.REVERSE);
        imageView.setAnimation(alphaAnimation);
        imageView.startAnimation(alphaAnimation);
    }

    // 旋转
    public void rotate(View v) {
        RotateAnimation rotateAnimation = new RotateAnimation(0f, 360,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rotateAnimation.setDuration(6000);
        rotateAnimation.setRepeatCount(1);
        imageView.startAnimation(rotateAnimation);
    }

    // 收缩
    public void scale(View v) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f,
                1.0f, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(3000);
        scaleAnimation.setRepeatCount(2);
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        scaleAnimation.setFillAfter(true);
        imageView.setAnimation(scaleAnimation);
    }


    //组合动画
    public void set(View v){
        //共同使用一个指示器
        AnimationSet animationSet=new AnimationSet(true);
        //创建位移动画
        TranslateAnimation translateAnimation=new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
        //创建透明动画
        AlphaAnimation alphaAnimation=new AlphaAnimation(0.0f, 1.0f);
        //创建缩放动画
        ScaleAnimation scaleAnimation=new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //创建旋转动画
        RotateAnimation rotateAnimation=new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        //将动画添加到animationSet

        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(translateAnimation);


        //设置执行的时间
        animationSet.setDuration(5000);
        //设置执行的次数
        animationSet.setRepeatCount(1);
        //设置执行的模式  反向
    animationSet.setRepeatMode(Animation.REVERSE);
        //执行动画
        imageView.setAnimation(animationSet);

    }

}

这里写图片描述

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1981次
    • 积分:347
    • 等级:
    • 排名:千里之外
    • 原创:33篇
    • 转载:1篇
    • 译文:1篇
    • 评论:0条
    文章分类
    文章存档