播放动画

public class MainActivity extends Activity implements OnClickListener{

private ImageButton image=null;
private Button btnAlpha=null;
private Button btnScale=null;
private Button btnRotate=null;
private Button btnTranslate=null;

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

image=(ImageButton) findViewById(R.id.image);
btnAlpha = (Button) findViewById(R.id.btn_alpha);
btnScale = (Button) findViewById(R.id.btn_scale);
btnRotate= (Button) findViewById(R.id.btn_rotate);
btnTranslate = (Button) findViewById(R.id.btn_translate);

btnAlpha.setOnClickListener(this);
btnScale.setOnClickListener(this);
btnRotate.setOnClickListener(this);
btnTranslate.setOnClickListener(this);
}

public void onClick(View view){
switch(view.getId()){
case R.id.btn_alpha:
//代码实现
/*AnimationSet animationSet01 = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(3000);
animationSet01.addAnimation(alphaAnimation);
image.startAnimation(animationSet01);*/

//xml文件实现
Animation alpha = AnimationUtils.loadAnimation(this, R.anim.alpha);
image.startAnimation(alpha);
break;
case R.id.btn_scale:
//代码实现
/*AnimationSet animationSet02 = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1,0.1f,1,0.1f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//设置动作执行所用的时长
scaleAnimation.setDuration(3000);
//当等待指定的时间过后,才能执行此动作
scaleAnimation.setStartOffset(3000);
//设置动画重复执行的次数
scaleAnimation.setRepeatCount(3);
animationSet02.addAnimation(scaleAnimation);
//设置状态,若设为true则动作执行之后的动作为最终状态
animationSet02.setFillAfter(true);
// //设置状态,若设为true则动作执行完毕后,回到最初状态,反之,则
animationSet02.setFillBefore(true);
image.startAnimation(animationSet02);*/

//xml文件实现
Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale);
image.startAnimation(scale);
break;
case R.id.btn_rotate:
//代码实现
/*AnimationSet animationSet03 = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(3000);
animationSet03.addAnimation(rotateAnimation);
image.startAnimation(animationSet03);*/

//xml文件实现
Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
image.startAnimation(rotate);
break;
case R.id.btn_translate:
//代码实现
/*AnimationSet animationSet04 = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,2);
translateAnimation.setDuration(3000);
animationSet04.addAnimation(translateAnimation);
image.startAnimation(animationSet04);*/

//xml文件实现,多种动作同时执行
/*Animation translate = AnimationUtils.loadAnimation(this, R.anim.alpha_rotate_scale);
translate.setFillAfter(true);
image.startAnimation(translate);*/

//代码实现 多种动作同时执行
//如果构造AnimationSet对象时,穿进去的参数为true,则AnimationSet对象内所有的Animation对象共享此对象使用setInterpolator()方法设置的内容,否则穿进去FALSE的话,则每个Animation对象根据需求自行设置
AnimationSet set = new AnimationSet(true);
AlphaAnimation alpha02 = new AlphaAnimation(1.0f,0);
ScaleAnimation scale02 = new ScaleAnimation(1,0,1,0,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
RotateAnimation rotate02 = new RotateAnimation(0, 1080, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
set.addAnimation(rotate02);
set.addAnimation(scale02);
set.addAnimation(alpha02);
set.setDuration(3000);
set.setFillAfter(true);
//设置动作运行时的速度
set.setInterpolator(new AccelerateDecelerateInterpolator());
image.startAnimation(set);
break;
case R.id.image:
Log.e(">>>>>>>>>>>djkf","您点击了图片");
Animation translate2 = AnimationUtils.loadAnimation(this, R.anim.rotate);
image.startAnimation(translate2);
Toast.makeText(this,"您点击了图片",Toast.LENGTH_LONG).show();
break;
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

XML文件

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:descendantFocusability="blocksDescendants">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btn_alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="alpha"
/>
<Button
android:id="@+id/btn_scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="sclae"
/>
<Button
android:id="@+id/btn_rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="rotate"
/>
<Button
android:id="@+id/btn_translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="alpha_rotate_scale"
/>

</LinearLayout>
<ImageButton
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/image2"
/>
</RelativeLayout>

奋斗和第三方
原文地址:https://www.cnblogs.com/Smart-Du/p/4301972.html