属性动画 效果如下:
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
//动画 三种 补间动画 帧动画 属性动画3.0之后
private ImageView img,img2;
private Button rotateX;
private Button rotateY;
private Button alpha;
private Button scaleX;
private Button scaleY;
private Button total;
private Button down;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
setListener();
}
private void setListener() {
rotateX.setOnClickListener(this);
rotateY.setOnClickListener(this);
alpha.setOnClickListener(this);
scaleX.setOnClickListener(this);
scaleY.setOnClickListener(this);
total.setOnClickListener(this);
down.setOnClickListener(this);
}
private void initView() {
img = ((ImageView) findViewById(R.id.img));
img2 = ((ImageView) findViewById(R.id.img2));
rotateX = ((Button) findViewById(R.id.rotateX));
rotateY = ((Button) findViewById(R.id.rotateY));
alpha = ((Button) findViewById(R.id.alpha));
scaleX = ((Button) findViewById(R.id.scaleX));
scaleY = ((Button) findViewById(R.id.scaleY));
total = ((Button) findViewById(R.id.total));
down = ((Button) findViewById(R.id.down));
}
@Override
public void onClick(View view) {
final View v=view;
switch (view.getId()){
//ObjectAnimator
case R.id.rotateX://沿着x轴旋转 0-360度 -
ObjectAnimator.ofFloat(img,"rotationX",0.0f,360f,180f).setDuration(1000).start();
break;
case R.id.rotateY://沿着y轴旋转 0-360度
ObjectAnimator.ofFloat(img,"rotationY",0.0f,360f).setDuration(1000).start();
break;
case R.id.alpha://渐变 即透明度的改变
ObjectAnimator.ofFloat(img,"alpha",1.0f,0.0f,0.8f).setDuration(1000).start();
break;
case R.id.scaleX://x轴缩放
ObjectAnimator.ofFloat(img,"scaleX",1.0f,0.0f,1.0f).setDuration(1000).start();
break;
case R.id.scaleY://y轴缩放
ObjectAnimator.ofFloat(img,"scaleY",1.0f,0.0f,1.0f).setDuration(1000).start();
break;
case R.id.total://组合动画
PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("alpha",1.0f,0.0f,1.0f);
PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("scaleX",1.0f,0.0f,1.0f);
PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("scaleY",1.0f,0.0f,1.0f);
ObjectAnimator.ofPropertyValuesHolder(img,p1,p2,p3).setDuration(1000).start();
break;
//ValueAnimator 自由落体
case R.id.down:
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
ValueAnimator va=ValueAnimator.ofFloat(view.getY(),dm.heightPixels).setDuration(10000);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
v.setTranslationY((Float) valueAnimator.getAnimatedValue());
}
});
va.start();
break;
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/jia"
/>
<ImageView
android:id="@+id/img2"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/jia"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/rotateX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotateX"
/>
<Button
android:id="@+id/rotateY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="rotateY"
/>
<Button
android:id="@+id/alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="透明度"
/>
<Button
android:id="@+id/scaleX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="x轴缩放"
/>
<Button
android:id="@+id/scaleY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Y轴缩放"
/>
<Button
android:id="@+id/total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="组合"
/>
<Button
android:id="@+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="自由落体"
/>
</LinearLayout>
</LinearLayout>