Property (属性动画)
来源:互联网 发布:java程序员月薪 编辑:程序博客网 时间:2024/06/07 17:50
XML加载动画 需要在创建animator文件夹,
res/animator 文件中 (注意 不是 res/anim,两者不一样)
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"> <objectAnimator android:duration="3000" android:propertyName="alpha" android:valueFrom="1.0" android:valueTo="0.0"/></set>
布局文件中
<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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.moliying.a57_property_animation.MainActivity"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxWidth="200dp" android:maxHeight="200dp" android:adjustViewBounds="true" android:onClick="imageClick" android:id="@+id/imageView" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:src="@mipmap/jj"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView2" android:onClick="musicClick" android:src="@mipmap/music_stack" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/></RelativeLayout>
package com.moliying.a57_property_animation;import android.animation.Animator;import android.animation.AnimatorInflater;import android.animation.AnimatorListenerAdapter;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.animation.PropertyValuesHolder;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.view.animation.AccelerateInterpolator;import android.view.animation.BounceInterpolator;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void musicClick(View view){ final View v = view;// DisplayMetrics dm = new DisplayMetrics();// getWindowManager().getDefaultDisplay().getMetrics(dm);// //定义一个动画// ValueAnimator va = ValueAnimator.ofFloat(v.getY(),dm.heightPixels,v.getY()).setDuration(1000);// //监听动画的动作变化// va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {// @Override// public void onAnimationUpdate(ValueAnimator animation) {// v.setTranslationY((Float) animation.getAnimatedValue());// }// });// va.start(); //组合多个动画效果 ObjectAnimator a1 = ObjectAnimator.ofFloat(view,"translationX",0f,200f); a1.setInterpolator(new AccelerateInterpolator()); //添加插值器 ObjectAnimator a2 = ObjectAnimator.ofFloat(view,"translationY",0f,200f); a2.setInterpolator(new AccelerateInterpolator());//添加插值器 ObjectAnimator a3 = ObjectAnimator.ofFloat(view,"rotation",0f,360f); a3.setInterpolator(new BounceInterpolator());//添加插值器 AnimatorSet set = new AnimatorSet(); set.setDuration(1000);// set.playTogether(a1,a2,a3);//同时执行// set.setStartDelay(1000);//延迟多少时间执行// set.playSequentially(a1,a2,a3); //顺序执行 set.play(a1).with(a2);//先a1,a2一起执行,之后再执行a3 set.play(a3).after(a1); set.start(); } public void imageClick(View view){// ObjectAnimator.ofFloat(view,"rotationX",0f,360f).setDuration(500).start();// propertyValuesHolder(view);// alphaValuesAnimator(view); //通过加载XML动画文件 Animator a = AnimatorInflater.loadAnimator(this,R.animator.anim_alpha); a.setTarget(view); a.start(); } /** * 为动画添加监听事件, 来删除组件 * @param view */ public void alphaValuesAnimator(View view){ final View v = view; ObjectAnimator oa = ObjectAnimator.ofFloat(v,"alpha",1f,0f).setDuration(1000);// oa.addListener(new Animator.AnimatorListener() {// @Override// public void onAnimationStart(Animator animation) {// //动画开始// }//// @Override// public void onAnimationEnd(Animator animation) {// //动画结束// ViewGroup viewGroup = (ViewGroup) v.getParent();// if(viewGroup!=null){// viewGroup.removeView(v);// System.out.println("removed.");// }// }//// @Override// public void onAnimationCancel(Animator animation) {// //动画取消// }//// @Override// public void onAnimationRepeat(Animator animation) {// //重复// }// }); oa.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { //动画结束 ViewGroup viewGroup = (ViewGroup) v.getParent(); if(viewGroup!=null){ viewGroup.removeView(v); System.out.println("removed."); } } }); oa.start(); } /** * 组合多个属性动画 * @param view */ public void propertyValuesHolder(View view){ PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("alpha",1f,0f,1f); PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("scaleX",1f,0f,1f); PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("scaleY",1f,0f,1f); ObjectAnimator.ofPropertyValuesHolder(view,p1,p2,p3).setDuration(3000).start(); }}
0 0
- Property Animation(属性动画)
- 属性动画(Property Animation)
- 属性动画(Property Animation)
- android属性动画Property
- property Animaton属性动画
- Property 属性动画入门
- 属性动画(property)
- Property Animation(属性动画)
- 【Property Animator 属性动画】
- Property (属性动画)
- Property Animation 属性动画
- Property 属性动画
- 属性动画 Property Animation
- 属性(Property)动画
- 属性动画(Property Animation)
- 属性动画(Property Animation)
- android属性动画-Property Animations
- Android Property Animation 属性动画
- c++设计模式之模板模式
- 板子测试SPI
- oracle的SQL语言
- [编写高质量iOS代码的52个有效方法](九)块(block)
- 7月份学习内容
- Property (属性动画)
- Python是什么类型的语言?
- 2016/07/27 选择
- 实现上拉加载更多的SwipeRefreshLayout
- 【链表】单链表——单循环链表
- LintCode之爬楼梯
- 传统线程通信技术
- CodeForces 448D Multiplication Table
- 安卓aidl夸进程通信的小例子,