属性动画

来源:互联网 发布:ubuntu 14.04与16.04 编辑:程序博客网 时间:2024/06/05 17:18

---------------------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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="com.exbawei.liteli.liteli20171014.MainActivity">  <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="渐变"      android:id="@+id/bu1"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="旋转"        android:id="@+id/bu2"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="平移"        android:id="@+id/bu3"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="缩放"        android:id="@+id/bu4"/>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="集合"        android:id="@+id/bu5"/>    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ee"             android:id="@+id/iv"        /></LinearLayout>------------------------------------------------
package com.exbawei.liteli.liteli20171014;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);     Button bu1= (Button) findViewById(R.id.bu1);     Button bu2= (Button) findViewById(R.id.bu2);     Button bu3= (Button) findViewById(R.id.bu3);     Button bu4= (Button) findViewById(R.id.bu4);     Button bu5= (Button) findViewById(R.id.bu5);       final ImageView iv= (ImageView) findViewById(R.id.iv);        bu1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0f, 1f);                animator.setDuration(5000);                animator.start();            }        });        bu2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "rotation", 0f, 360f);                animator.setDuration(5000);                animator.start();            }        });        bu3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                float curTranslationX = iv.getTranslationX();                ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "translationX", curTranslationX, -500f, curTranslationX);                animator.setDuration(5000);                animator.start();            }        });        bu4.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 5f, 1f);                animator.setDuration(5000);                animator.start();            }        });        bu5.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ObjectAnimator moveIn = ObjectAnimator.ofFloat(iv, "translationX", -500f, 0f);                ObjectAnimator rotate = ObjectAnimator.ofFloat(iv, "rotation", 0f, 360f);                ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0f, 1f);                ObjectAnimator animator = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 5f, 1f);                AnimatorSet animSet = new AnimatorSet();                animSet.play(rotate).with(fadeInOut).after(moveIn).before(animator);                animSet.setDuration(5000);                animSet.start();            }        });    }}

 

原创粉丝点击