android动画_属性动画

来源:互联网 发布:微信领取淘宝优惠券 编辑:程序博客网 时间:2024/06/06 16:54

属性动画会改变控件真实的坐标 

一、项目目录结构

二、activity_main.xml界面

三、activity_main.xml代码

<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=".MainActivity" >    <LinearLayout        android:id="@+id/ll"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="translate"            android:text="平移" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="scale"            android:text="缩放" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="alpha"            android:text="透明" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="rotate"            android:text="旋转" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="fly"            android:text="set" />    </LinearLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/ll"        android:onClick="playxml"        android:text="播放xml定义的属性动画" />    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@drawable/ic_launcher" /></RelativeLayout>
四、在res下创建animator文件夹(animator固定写法),然后添加xml动画文件
oanimator.xml代码
<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=".MainActivity" >    <LinearLayout        android:id="@+id/ll"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="translate"            android:text="平移" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="scale"            android:text="缩放" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="alpha"            android:text="透明" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="rotate"            android:text="旋转" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="fly"            android:text="set" />    </LinearLayout>    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@id/ll"        android:onClick="playxml"        android:text="播放xml定义的属性动画" />    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:src="@drawable/ic_launcher" /></RelativeLayout>
五、MainActivity.java代码
package com.zgs.PropertyAnimator;import android.animation.AnimatorInflater;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {private ImageView iv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//找到iv 执行动画iv = (ImageView) findViewById(R.id.iv);//设置了一个点击事件iv.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "你点不到我", 0).show();}});}//位移动画public void translate(View v){//创建属性动画/** * target 执行的目标   * propertyName 属性名字  The name of the property being animated. * float... values 可变参数  */ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 50,20,100);oa.setDuration(2000);oa.start(); //开始动画}//缩放动画public void scale(View v){ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 2);oa.setDuration(2000);oa.start();}//实现透明的效果 public void alpha(View v){ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1,0,1);oa.setDuration(2000);oa.start();}//实现旋转的效果public void rotate(View v){//ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationX", 0, 180, 90, 360);oa.setDuration(2000);oa.start();}//一起执行public void fly(View v){AnimatorSet as = new AnimatorSet();ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 50, 20, 100);ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 2);ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1);ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotationY", 0, 180, 90, 360);as.setDuration(2000);//执行动画时长as.setTarget(iv);//iv执行动画//往集合中添加动画//挨个执行as.playSequentially(oa, oa2, oa3, oa4);//一起执行//as.playTogether(oa, oa2, oa3, oa4);as.start();}//使用xml的方式创建属性动画public void playxml(View v){ObjectAnimator oa = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.oanimator);//设置执行目标oa.setTarget(iv);oa.start();//开始执行}}
六、效果演示

0 0
原创粉丝点击