补间动画的实现

来源:互联网 发布:国语版港剧软件 编辑:程序博客网 时间:2024/04/30 10:53

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">    <Button        android:id="@+id/btn1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="透明"        android:layout_weight="1"/>    <Button        android:id="@+id/btn2"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="缩放"        android:layout_weight="1"/>    <Button        android:id="@+id/btn3"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="旋转"        android:layout_weight="1"/>    <Button        android:id="@+id/btn4"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="平移"        android:layout_weight="1"/>    <Button        android:id="@+id/btn5"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="综合"        android:layout_weight="1"/>    </LinearLayout>    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"/></LinearLayout>
2.MainActivity.java

package com.cwj.anim2;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.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private ImageView iv;    private Button btn1,btn2,btn3,btn4,btn5;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        iv = (ImageView) findViewById(R.id.iv);        btn1 = (Button) findViewById(R.id.btn1);        btn2 = (Button) findViewById(R.id.btn2);        btn3 = (Button) findViewById(R.id.btn3);        btn4 = (Button) findViewById(R.id.btn4);        btn5 = (Button) findViewById(R.id.btn5);        btn1.setOnClickListener(this);        btn2.setOnClickListener(this);        btn3.setOnClickListener(this);        btn4.setOnClickListener(this);        btn5.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.btn1:                //透明                AlphaAnimation aAnim = new AlphaAnimation(0f,1f);                aAnim.setDuration(2 * 1000);                //动画完成后,是否保持                aAnim.setFillAfter(true);                iv.startAnimation(aAnim);                break;            case R.id.btn2:                //缩放                ScaleAnimation sAnim = new ScaleAnimation(1f,2f,1f,2f,                        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);                sAnim.setDuration(2 * 1000);                //动画完成后,是否保持                sAnim.setFillAfter(false);                iv.startAnimation(sAnim);                break;            case R.id.btn3:                //旋转                RotateAnimation rAnim = new RotateAnimation(0,360,                        Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);                rAnim.setDuration(2 * 1000);                //动画完成后,是否保持                rAnim.setFillAfter(true);                iv.startAnimation(rAnim);                break;            case R.id.btn4:                //平移                TranslateAnimation tAnim = new TranslateAnimation(                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,5f,                        Animation.RELATIVE_TO_SELF,0f,Animation.RELATIVE_TO_SELF,5f                );                tAnim.setDuration(2 * 1000);                //动画完成后,是否保持                tAnim.setFillAfter(true);                iv.startAnimation(tAnim);                break;            case R.id.btn5:                //综合                //缩放                sAnim = new ScaleAnimation(1f, 2f, 1f, 2f,                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);                sAnim.setDuration(2 * 1000);                //旋转                rAnim = new RotateAnimation(0, 360,                        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);                rAnim.setDuration(2 * 1000);                //透明                aAnim = new AlphaAnimation(0f, 1f);                aAnim.setDuration(2 * 1000);                AnimationSet animset = new AnimationSet(true);                animset.addAnimation(sAnim);                animset.addAnimation(rAnim);                animset.addAnimation(aAnim);                iv.startAnimation(animset);                break;            default:                break;        }    }}


原创粉丝点击