转载请注明出处:
http://blog.csdn.net/iwanghang/article/details/53738561
绝对博文有用,请点赞,请留言,谢谢!~
直接看GIF效果和代码:
MainActivity.java:
转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53738561
绝对博文有用,请点赞,请留言,谢谢!~
绝对博文有用,请点赞,请留言,谢谢!~
直接看GIF效果和代码:
MainActivity.java:
package com.iwanghang.propertyanimation;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ImageView iv_animation;
private TextView tv_animation_msg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_animation = (ImageView) findViewById(R.id.iv_animation);
iv_animation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了图片", Toast.LENGTH_SHORT).show();
}
});
}
/**
* 补间(视图)动画
*/
public void testTweenAnimation(View v) {
// 由左上向右下移动
TranslateAnimation animation = new TranslateAnimation(0, iv_animation.getWidth(), 0, iv_animation.getHeight());
// 由右下向左上移动
//TranslateAnimation animation = new TranslateAnimation(iv_animation.getWidth(), 0, iv_animation.getHeight(), 0);
animation.setDuration(3000); // 设置持续时间
animation.setFillAfter(true); // 界面会停留在动画播放完时的界面
iv_animation.startAnimation(animation); // 运行动画
}
/**
* 属性动画
*/
public void testPropertyAnimation(View v) {
ObjectAnimator animator3 = ObjectAnimator.ofFloat(iv_animation,"translationX",0,iv_animation.getWidth());
ObjectAnimator animator4 = ObjectAnimator.ofFloat(iv_animation,"translationY",0,iv_animation.getHeight());
/**
* TranslateAnimation 补间动画 一个动画
* AnimatorSet + playTogether 属性动画 两个动画一起播放
*/
AnimatorSet set = new AnimatorSet();
set.playTogether(animator3,animator4);
set.setDuration(2000);
set.start();
// ObjectAnimator animator = ObjectAnimator.ofFloat(iv_animation, "translationX", 0,iv_animation.getWidth());
// ObjectAnimator animator2 = ObjectAnimator.ofFloat(iv_animation, "translationY", 0,iv_animation.getHeight());
// AnimatorSet animatorSet = new AnimatorSet();
// animatorSet.setDuration(2000);
// animatorSet.setInterpolator(new BounceInterpolator());
// //两个动画一起播放
// animatorSet.playTogether(animator, animator2);
// //开始播放
// animatorSet.start();
// //另外一种写法
// iv_animation.animate()
// .translationXBy(iv_animation.getWidth())
// .translationYBy(iv_animation.getWidth())
// .setDuration(2000)
// .setInterpolator(new BounceInterpolator())
// .start();
}
public void reset(View v) {
iv_animation.clearAnimation();
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.iwanghang.propertyanimation.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="testTweenAnimation"
android:text="测试补间(视图)动画" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="testPropertyAnimation"
android:text="测试属性动画" />
<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000" />
<TextView
android:textColor="#000000"
android:text="补间动画可以被clearAnimation,属性动画不可以
因为属性动画没有setFillAfter属性"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="reset"
android:text="重置补间动画" />
<ImageView
android:id="@+id/iv_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@drawable/logo" />
<!--我是分割-->
<ImageView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000" />
</LinearLayout>
转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53738561
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
绝对博文有用,请点赞,请留言,谢谢!~