1. 程式人生 > >屬性動畫(旋轉,平移,縮放,透明集合)

屬性動畫(旋轉,平移,縮放,透明集合)

MainActivity

package com.example.animator;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import
android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button tran; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super
.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tran =(Button)findViewById(R.id.translate); img =(ImageView)findViewById(R.id.img); img.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this
,"hello",Toast.LENGTH_SHORT).show(); } }); tran.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //平移屬性動畫 ObjectAnimator translationX = ObjectAnimator.ofFloat(img, "translationX", 0, 300); //建立透明度動畫 ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", 1.0f, 0f); //縮放 ObjectAnimator scaleY = ObjectAnimator.ofFloat(img, "scaleY", 1f, 3f); //旋轉 ObjectAnimator rotation = ObjectAnimator.ofFloat(img, "rotation", 0f, 300f); //動畫集合 AnimatorSet set = new AnimatorSet(); //新增動畫 set.play(rotation).with(alpha).after(translationX); //設定動畫時間 set.setDuration(3000); set.start(); //動畫監聽 set.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); img.setVisibility(View.GONE); } @Override public void onAnimationRepeat(Animator animation) { super.onAnimationRepeat(animation); } @Override public void onAnimationStart(Animator animation) { super.onAnimationStart(animation); } }); } }); } }

activity_main

<?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.animator.MainActivity">
<Button
    android:id="@+id/translate"
    android:text="動畫集合"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/img"
        android:src="@mipmap/v2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>