ObjectAnimator的简单使用
1.在activity_main.xml文件中
<?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" android:background="@android:color/white" tools:context=".MainActivity">
<TextView android:id="@+id/main_tv" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" />
<Button android:id="@+id/main_btn_top" android:layout_width="match_parent" android:layout_height="150dp" android:background="@android:color/white" />
<Button android:id="@+id/main_btn_bottom" android:layout_width="match_parent" android:layout_height="150dp" android:layout_alignParentBottom="true" android:background="@android:color/white" />
</RelativeLayout>
2.在res/anim/property_animator_large.xml文件中,ObjectAnimator的xml放大动画。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:ordering="together">
<objectAnimator android:interpolator="@android:anim/overshoot_interpolator" android:propertyName="scaleX" android:valueFrom="0.1f" android:valueTo="1.0f" android:valueType="floatType" />
<objectAnimator android:interpolator="@android:anim/overshoot_interpolator" android:propertyName="scaleY" android:valueFrom="0.1f" android:valueTo="1.0f" android:valueType="floatType" />
<objectAnimator android:propertyName="backgroundColor" android:valueFrom="@android:color/holo_red_dark" android:valueTo="@android:color/holo_blue_bright" android:valueType="colorType" />
</set>
3.在res/anim/property_animator_small.xml文件中,ObjectAnimator的xml缩小动画。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="1000" android:ordering="together">
<objectAnimator android:interpolator="@android:anim/overshoot_interpolator" android:propertyName="scaleX" android:valueFrom="1.0f" android:valueTo="0.1f" android:valueType="floatType" />
<objectAnimator android:interpolator="@android:anim/overshoot_interpolator" android:propertyName="scaleY" android:valueFrom="1.0f" android:valueTo="0.1f" android:valueType="floatType" />
<objectAnimator android:propertyName="backgroundColor" android:valueFrom="@android:color/holo_blue_bright" android:valueTo="@android:color/holo_red_dark" android:valueType="colorType" />
</set>
4.在MainActivity中
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView mTextView;
private Button mButtonTop;
private Button mButtonBottom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initListener();
}
private void initView() {
mTextView = (TextView) findViewById(R.id.main_tv);
mButtonTop = (Button) findViewById(R.id.main_btn_top);
mButtonBottom = (Button) findViewById(R.id.main_btn_bottom);
}
private void initListener() {
mButtonTop.setOnClickListener(this);
mButtonBottom.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_btn_top:
//大变小
// largeToSmallAnim(mTextView);
//xml实现
xmlSmallAnim(mTextView);
break;
case R.id.main_btn_bottom:
//小变大
// smallToLarge(mTextView);
//xml实现
xmlLargeAnim(mTextView);
break;
}
}
private void xmlSmallAnim(View view) {
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.property_animator_small);
animator.setTarget(view);
animator.start();
}
private void xmlLargeAnim(View view) {
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.property_animator_large);
animator.setTarget(view);
animator.start();
}
private void largeToSmallAnim(View view) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 0.1f,1.0f);
scaleX.setInterpolator(new OvershootInterpolator());
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 0.1f,1.0f);
scaleY.setInterpolator(new OvershootInterpolator());
ObjectAnimator color = ObjectAnimator.ofArgb(view, "backgroundColor", 0xFFFF0000, 0xFF0000FF);
AnimatorSet set = new AnimatorSet();
set.playTogether(scaleX, scaleY, color);
set.setDuration(1000).start();
}
private void smallToLarge(View view) {
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0.1f, 1.0f,0.5f);
scaleX.setInterpolator(new OvershootInterpolator());
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0.1f, 1.0f,0.5f);
scaleY.setInterpolator(new OvershootInterpolator());
ObjectAnimator color = ObjectAnimator.ofArgb(view, "backgroundColor", 0xFF0000FF, 0xFFFF0000);
AnimatorSet set = new AnimatorSet();
set.playTogether(scaleX, scaleY, color);
set.setDuration(1000).start();
}
}
在代码中实现动画更加灵活,可以设置更多的变量,在xml只能设置2个属性值
ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 0.1f,1.0f);
android:valueFrom="1.0f"
android:valueTo="0.1f"