Property Animation(属性动画)
效果图
布局文件
<?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="bw.com.Unit2.PropertyActivity">
<Button
android:id="@+id/alpha_btn_property"
android:text="alpha动画(透明)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/scale_btn_property"
android:text="rotate动画(拉伸)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/rotate_btn_property"
android:text="rotate动画(旋转)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/translate_btn_property"
android:text="translate动画(位移)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/color_btn_property"
android:text="color动画(改变颜色)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/set_btn_property"
android:text="set动画(集合)"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/property_tv"
android:text="1702C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
/>
</LinearLayout>
Java代码
package bw.com.Unit2;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateInterpolator;
import android.widget.TextView;
import android.widget.Toast;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import bw.com.Unit1.R;
@ContentView(R.layout.activity_property)
public class PropertyActivity extends AppCompatActivity {
@ViewInject(R.id.property_tv)
private TextView textView;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
}
@Event({R.id.alpha_btn_property,R.id.scale_btn_property,R.id.rotate_btn_property,R.id.translate_btn_property,
R.id.property_tv,R.id.color_btn_property,R.id.set_btn_property})
private void click(View view){
switch (view.getId()){
case R.id.alpha_btn_property:
alphaProperty();
break;
case R.id.scale_btn_property:
scaleProperty();
break;
case R.id.rotate_btn_property:
rotateProperty();
break;
case R.id.translate_btn_property:
transcaleProperty();
break;
case R.id.property_tv:
Toast.makeText(this, "我被点击了!", Toast.LENGTH_SHORT).show();
break;
case R.id.color_btn_property:
colorProperty();
break;
case R.id.set_btn_property:
setProperty();
break;
}
}
private void setProperty() {
PropertyValuesHolder propertyValuesHolderC = PropertyValuesHolder.ofInt("backgroundColor", Color.BLUE,Color.RED);
PropertyValuesHolder propertyValuesHolderT = PropertyValuesHolder.ofFloat("translationY", 0,200);
PropertyValuesHolder propertyValuesHolderS = PropertyValuesHolder.ofFloat("scaleX", 1f,3f);
PropertyValuesHolder propertyValuesHolderR = PropertyValuesHolder.ofFloat("rotation", 0,360);
PropertyValuesHolder propertyValuesHolderA = PropertyValuesHolder.ofFloat("alpha", 1,0);
ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(textView,propertyValuesHolderC,propertyValuesHolderT
,propertyValuesHolderS,propertyValuesHolderR,propertyValuesHolderA
);
objectAnimator.setDuration(5000);
objectAnimator.start();
}
private void colorProperty() {
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(textView,"backgroundColor", Color.BLUE,Color.RED);
objectAnimator.setDuration(5000);
objectAnimator.start();
}
private void transcaleProperty() {
float y = textView.getTranslationY();
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"translationY",y,y+100);
objectAnimator.setDuration(3000);
objectAnimator.start();
}
private void scaleProperty() {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"scaleY",1f,3f);
objectAnimator.setDuration(3000);
objectAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"scaleX",3f,1f);
objectAnimator.setDuration(3000);
objectAnimator.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
objectAnimator.start();
}
private void rotateProperty() {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"rotation",0,360);
objectAnimator.setDuration(3000);
objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
objectAnimator.setInterpolator(new AnticipateInterpolator());
objectAnimator.start();
}
private void alphaProperty() {
ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(PropertyActivity.this,R.animator.alpha);
valueAnimator.setTarget(textView);
valueAnimator.setDuration(3000);
valueAnimator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Toast.makeText(PropertyActivity.this, "开始", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animator animation) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"alpha",0,1);
objectAnimator.setDuration(1000);
objectAnimator.start();
Toast.makeText(PropertyActivity.this, "结束", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
count++;
Toast.makeText(PropertyActivity.this, "执行第"+count+"次", Toast.LENGTH_SHORT).show();
}
});
valueAnimator.start();
}
}
R.animator.alpha文件
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="Alpha"
android:valueFrom="1"
android:valueTo="0"
android:fillBefore="true"
>
</objectAnimator>