Android中补间动画2----Animation的基本使用代码实现复合使用

来源:互联网 发布:付款时淘宝系统异常 编辑:程序博客网 时间:2024/06/05 14:54

效果图:


Main2Activity
package com.zhh.android;import android.animation.AnimatorSet;import android.app.Activity;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;/** * 复合动画 */public class Main2Activity extends Activity {    private Button btnScale;    private ImageView ivLauncher;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main2);        initView();        myOnclick();    }    /**     * 初始化控件     */    private void initView() {        btnScale = (Button)findViewById(R.id.btnScale);        ivLauncher = (ImageView)findViewById(R.id.ivLauncher);    }    /**     * 点击事件     */    private void myOnclick() {        btnScale.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                setRecombination();            }        });        ivLauncher.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                startActivity(new Intent(Main2Activity.this,Main3Activity.class));            }        });    }    /**     * 创建复合动画     */    private void setRecombination(){//     效果:透明度从透明到不透明,持续时间2s,接着旋转360动画,持续1s//     1创建透明动画,并设置        AlphaAnimation alphaAnimation = new AlphaAnimation(0f,1f);        alphaAnimation.setDuration(2000);//     2创建旋转动画,并设置        RotateAnimation rotateAnimation = new RotateAnimation(0,360,  Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f);        rotateAnimation.setDuration(1000);//      延时两秒        rotateAnimation.setStartOffset(2000);//     3创建复合动画对象        AnimationSet animationSet = new AnimationSet(true);//     4添加两个动画        animationSet.addAnimation(alphaAnimation);        animationSet.addAnimation(rotateAnimation);//     5启动复合动画对象        ivLauncher.startAnimation(animationSet);    }}
activity_main2.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:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.zhh.android.Main2Activity"    android:orientation="vertical"    >    <Button        android:id="@+id/btnScale"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="复合动画"        android:textSize="30dp"        />    <ImageView        android:id="@+id/ivLauncher"        android:layout_width="wrap_content"        android:layout_height="200dp"        android:src="@mipmap/cyl"        android:layout_gravity="center_horizontal"        /></LinearLayout>
参考视频:

http://www.gulixueyuan.com/course/112/task/1793/show#

源码下载:



原创粉丝点击