Android动画框架, 属性动画

xiaoxiao2021-02-28  156

Demo:

package com.example.administrator.youku_animi; import android.animation.Animator; import android.animation.AnimatorInflater; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.PropertyValuesHolder; import android.animation.ValueAnimator; import android.app.Activity; import android.os.Bundle; import android.renderscript.Sampler; import android.view.View; import android.view.ViewGroup; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.DecelerateInterpolator; import android.view.animation.LayoutAnimationController; import android.view.animation.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.LinearLayout; import java.security.acl.Group; /** * Created by Administrator on 2017/8/4 0004. */ public class AnimationTest extends Activity { private AnimationSet mAnimationSet; private Button button, button2, button3; //private ViewGroup mViewGrop; //属性动画 ObjectAnimator objectAnimator, objectAnimator2, objectAnimator3, objectAnimator4; PropertyValuesHolder ph1, ph2, ph3; AnimationSet as; AnimatorSet objectAnimationSet; LinearLayout viewGroup; LayoutAnimationController lac; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_animation_test); viewGroup = (LinearLayout) findViewById(R.id.group); button = (Button) findViewById(R.id.btn); // button2 = (Button) findViewById(R.id.btn2); // button3 = (Button) findViewById(R.id.btn3); // mViewGrop = (ViewGroup) findViewById(R.id.group); //布局动画 ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1); sa.setDuration(2000); lac = new LayoutAnimationController(sa, 0.5f); lac.setOrder(LayoutAnimationController.ORDER_NORMAL); viewGroup.setLayoutAnimation(lac); //动画集合 as = new AnimationSet(true); objectAnimationSet = new AnimatorSet(); //属性动画 objectAnimator = ObjectAnimator.ofFloat(button, "translationY", 0, 600, 3, 300, 800); objectAnimator.setDuration(6000); objectAnimator.setRepeatCount(-1); objectAnimator.setInterpolator(new DecelerateInterpolator()); objectAnimator.setRepeatMode(ValueAnimator.REVERSE); objectAnimator2 = ObjectAnimator.ofFloat(button, "translationX", 0, 300); objectAnimator2.setDuration(6000); objectAnimator2.setRepeatCount(-1); objectAnimator2.setInterpolator(new DecelerateInterpolator()); objectAnimator2.setRepeatMode(ValueAnimator.REVERSE); objectAnimator3 = ObjectAnimator.ofFloat(button, "alpha", 0.1f, 0.6f, 0.3f, 0.9f); objectAnimator3.setDuration(6000); objectAnimator3.setRepeatCount(-1); objectAnimator3.setInterpolator(new DecelerateInterpolator()); objectAnimator3.setRepeatMode(ValueAnimator.REVERSE); objectAnimator4 = ObjectAnimator.ofFloat(button, "scaleX", 3); objectAnimator4.setDuration(3000); as.setDuration(2000); //PropertyValues ph1 = PropertyValuesHolder.ofFloat("translationX", 300f); ph2 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.2f, 1f); ph3 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.2f, 1f); //透明度动画 AlphaAnimation aa = new AlphaAnimation(0.1f, 0.4f); aa.setDuration(4000); as.addAnimation(aa); //位移动画 TranslateAnimation ta = new TranslateAnimation(0, 100, 0, 200); ta.setDuration(4000); as.addAnimation(ta); //旋转动画 RotateAnimation ra = new RotateAnimation(0, 360, 100, 100); ra.setDuration(5000); as.addAnimation(ra); objectAnimationSet.playTogether(objectAnimator, objectAnimator2, objectAnimator3, objectAnimator4); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // button.startAnimation(as); // objectAnimationSet.start(); // ObjectAnimator.ofPropertyValuesHolder(button, ph1, ph2, ph3).setDuration(2000) // .start(); // scaleX(button); // 添加按钮 } }); } private void scaleX(View view) { Animator anim = AnimatorInflater.loadAnimator(this, R.animator.anim); anim.setTarget(button); anim.start(); } }

xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/group" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--android:animateLayoutChanges="true"--> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/app_name" /> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="startAnimation" /> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="endAnimation" /> </LinearLayout>
转载请注明原文地址: https://www.6miu.com/read-39980.html

最新回复(0)