public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button button1;
private Button button2;
private Button button3;
private Button button4,button5;
private ImageButton img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
intByid();
}
//获取的id控件
private void intByid() {
button1 = findViewById( R.id.but1 );
button2 = findViewById( R.id.but2 );
button3 = findViewById( R.id.but3 );
button4 = findViewById( R.id.but4 );
button5 = findViewById( R.id.but5 );
img = findViewById( R.id.img );
button1.setOnClickListener( this );
button2.setOnClickListener( this );
button3.setOnClickListener( this );
button4.setOnClickListener( this );
button5.setOnClickListener( this );
img.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText( MainActivity.this, "点击我", Toast.LENGTH_SHORT ).show();
}
} );
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.but1://透明
ObjectAnimator alpha = ObjectAnimator.ofFloat( img, "alpha", new float[]{0.0f, 0.2f, 0.5f, 0.9f, 1.0f, 1.5f} );
alpha.setDuration( 2000 );
alpha.setRepeatCount( ObjectAnimator.RESTART );
alpha.setRepeatCount( 1 );
alpha.start();
break;
case R.id.but4://旋转
ObjectAnimator rotationX = ObjectAnimator.ofFloat( img, "rotationX", new float[]{90f, 180f, 270f, 360f} );
rotationX.setDuration( 2000 );
rotationX.setRepeatCount( ObjectAnimator.RESTART );
rotationX.setRepeatCount( 1 );
rotationX.start();
break;
case R.id.but2://平移
ObjectAnimator translationX = ObjectAnimator.ofFloat( img, "translationX", new float[]{10f, 20f, 30f, 50f, 70f, 85f} );
translationX.setDuration( 2000 );
translationX.setRepeatCount( ObjectAnimator.RESTART );
translationX.setRepeatCount( 1 );
translationX.start();
break;
case R.id.but3://缩放
ObjectAnimator scalex = ObjectAnimator.ofFloat( img, "scaleX", new float[]{1f, 2f, 3f, 5f, 10f, 15f,1f} );
scalex.setDuration( 2000 );
scalex.setRepeatCount( ObjectAnimator.RESTART );
scalex.setRepeatCount( 1 );
scalex.start();
break;
case R.id.but5://组合
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator rotationx = ObjectAnimator.ofFloat( img, "rotationX", new float[]{90f, 180f, 270f, 360f} );
rotationx.setDuration( 2000 );
ObjectAnimator translationx = ObjectAnimator.ofFloat( img, "translationX", new float[]{10f, 20f, 30f, 50f, 70f, 85f} );
translationx.setDuration( 2000 );
animatorSet.playTogether( rotationx,translationx );
animatorSet.start();
break;
}
}
、、、、、、、、、、、、、、、
已xml的形式的动画效果
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
Animator animator = AnimatorInflater.loadAnimator( this, R.animator.objectanimator );
animator.setTarget( img );
animator.start();
}xml文件(创建animator文件)
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="rotationX"
android:duration="3000"
android:repeatCount="1"
android:repeatMode="restart"
android:startOffset="0"
android:valueFrom="360.0"
>
</objectAnimator>