您的位置:首页 > 移动开发 > Android开发

Android 属性动画使用总结

2018-02-28 15:19 405 查看
把四大常用效果加动画组合的代码整理了一下
下面是一个演示图:



简单强调一下:

获取ObjectAnimator

ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);
第三个参数是 ...无限多的形式 因此可以根据自己的需求传递参数
最后上一下代码
Acitivity中点击按钮开始动画:public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button start_btn;
private TextView alpha_tv;
private TextView rotation_tv;
private TextView translationX_tv;
private TextView scaleY_tv;
private TextView animset_tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}

private void initView() {
start_btn = (Button) findViewById(R.id.start_btn);
alpha_tv = (TextView) findViewById(R.id.alpha_tv);
rotation_tv = (TextView) findViewById(R.id.rotation_tv);
translationX_tv = (TextView) findViewById(R.id.translationX_tv);
scaleY_tv = (TextView) findViewById(R.id.scaleY_tv);
animset_tv = (TextView) findViewById(R.id.animset_tv);

start_btn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_btn:
//淡入淡出属性动画
ObjectAnimator alpha = ObjectAnimator.ofFloat(alpha_tv, "alpha", 1f, 0.1f, 1f);
alpha.setDuration(5000);
alpha.start();

//旋转
ObjectAnimator rotation = ObjectAnimator.ofFloat(rotation_tv, "rotation", 0f, 360f);
rotation.setDuration(5000);
rotation.start();

//平移
float curTranslationX = translationX_tv.getTranslationX();
ObjectAnimator translationX = ObjectAnimator.ofFloat(translationX_tv, "translationX", curTranslationX, -500f, curTranslationX);
translationX.setDuration(5000);
translationX.start();

//缩放
ObjectAnimator scaleY = ObjectAnimator.ofFloat(scaleY_tv, "scaleY", 1f, 3f, 1f);
scaleY.setDuration(3000);
scaleY.start();

//组合
ObjectAnimator moveIn = ObjectAnimator.ofFloat(animset_tv, "translationX", -500f, 0f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(animset_tv, "rotation", 0f, 360f);
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(animset_tv, "alpha", 1f, 0f, 1f);
ObjectAnimator end = ObjectAnimator.ofFloat(animset_tv, "scaleY", 1f, 3f, 1f);
AnimatorSet animSet = new AnimatorSet();
//after(Animator anim) 将现有动画插入到传入的动画之后执行
//after(long delay) 将现有动画延迟指定毫秒后执行
//before(Animator anim) 将现有动画插入到传入的动画之前执行
//with(Animator anim) 将现有动画和传入的动画同时执行
animSet.play(rotate).with(fadeInOut).after(moveIn).before(end);
animSet.setDuration(5000);
animSet.start();

break;
}
}
}
Activity的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lyl.objectanimator.MainActivity">

<Button
androi
4000
d:id="@+id/start_btn"
android:text="开始动画"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/alpha_tv"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个字符串,淡入淡出" />

<TextView
android:id="@+id/rotation_tv"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个字符串,旋转" />

<TextView
android:id="@+id/translationX_tv"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个字符串,平移" />

<TextView
android:id="@+id/scaleY_tv"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个字符串,缩放" />

<TextView
android:id="@+id/animset_tv"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是一个字符串,组合" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: