首页 > 系统 > Android > 正文

Android Animation 动画(一)

2019-11-06 09:41:57
字体:
来源:转载
供稿:网友

写了一下android中关于动画的一些小demo,传上来分享一下,希望对大家有帮助。

1.xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="sc.toolPRoject.AnimationsActivity">   <LinearLayout       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:orientation="horizontal">       <Button           android:id="@+id/btn_rotate"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="旋转"/>       <Button           android:id="@+id/btn_scale"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="缩放"/>       <Button           android:id="@+id/btn_alpha"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="淡入淡出"/>       <Button           android:id="@+id/btn_translate"           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="移动"/>   </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <ImageView            android:id="@+id/animations_image"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@mipmap/ic_launcher"            android:layout_gravity="center"/>    </LinearLayout></LinearLayout>2.Activity

public class AnimationsActivity extends AppCompatActivity {    private Button btnRotate;    private Button btnScale;    private Button btnAlpha;    private Button btnTranslate;    private ImageView imageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btnRotate = (Button) findViewById(R.id.btn_rotate);        btnScale = (Button) findViewById(R.id.btn_scale);        btnAlpha = (Button) findViewById(R.id.btn_alpha);        btnTranslate = (Button) findViewById(R.id.btn_translate);        imageView = (ImageView) findViewById(R.id.animations_image);        btnRotate.setOnClickListener(new RotateListener());        btnAlpha.setOnClickListener(new AlphaListener());        btnScale.setOnClickListener(new ScaleListener());        btnTranslate.setOnClickListener(new TranslateListener());    }    class RotateListener implements View.OnClickListener{        @Override        public void onClick(View v) {            //创建一个AnimationSet对象            // 构造方法中如果使用true,表示Animation的Interpolator            //如果使用false,表示使用自身的            AnimationSet animationSet = new AnimationSet(true);            //构造方法一共包含6个参数,            //1:开始角度            //2:结束角度            //3:X轴坐标的类型,在源码注释中,我们可以看到一共有3中方式            //分别是 Animation.ABSOLUTE(绝对坐标), Animation.RELATIVE_TO_SELF(基于自身),Animation.RELATIVE_TO_PARENT(基于父控件)            //4:X轴的值,可以使用            //5:Y轴坐标的类型            //6:Y轴的值            RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);            //设置动画的持续时间            rotateAnimation.setDuration(1000);            //设置为true,则动画结束后控件停留在执行结束的状态            rotateAnimation.setFillAfter(true);            //设置动画之前的等待时间            rotateAnimation.setStartOffset(1000);            //设置动画的重复次数            rotateAnimation.setRepeatCount(3);            //设置动画的重复模式            rotateAnimation.setRepeatMode(Animation.REVERSE);            animationSet.addAnimation(rotateAnimation);            imageView.startAnimation(animationSet);        }    }    class AlphaListener implements View.OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            //创建一个AlphaAnimation对象            //构造方法中有两个参数,分别表示起始的透明度和结束时的透明度            AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);            //设置动画的执行时间            alphaAnimation.setDuration(500);            //将AlphaAnimation对象放到AnimationSet对象中            animationSet.addAnimation(alphaAnimation);            //使用ImageView执行该动画            imageView.startAnimation(animationSet);        }    }    class ScaleListener implements View.OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            //构造方法中包含6个值            //1:X轴的初始值            //2:X轴得结束值            //3:Y轴的初始值            //4:Y轴得结束值            //5:X轴坐标的类型,在源码注释中,我们可以看到一共有3中方式            //分别是 Animation.ABSOLUTE(绝对坐标), Animation.RELATIVE_TO_SELF(基于自身),Animation.RELATIVE_TO_PARENT(基于父控件)            //6:X的值            //7:Y轴坐标的类型            //8:X的值            ScaleAnimation scaleAnimation = new ScaleAnimation(0,0.1f,0,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);            scaleAnimation.setDuration(1000);            animationSet.addAnimation(scaleAnimation);            imageView.startAnimation(scaleAnimation);        }    }    class TranslateListener implements View.OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f,                                                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,0.5f);            translateAnimation.setDuration(1000);            //设置为true,则动画结束后控件停留在执行结束的状态            translateAnimation.setFillAfter(true);            animationSet.addAnimation(translateAnimation);            imageView.startAnimation(translateAnimation);        }    }}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表