第一部分 Activity部分
yipublic class MainActivity extends AppCompatActivity implements View.OnClickListener { public Button mBtnFirst, mBtnSecond, mBtnThird, mBtnForth; public ImageView mIv; /* *Tween动画通用方法 * setDuration(long durationMillis) 设置动画持续时间单位为毫秒 * setFillAfter(boolean fillAfter) 如果是true动画执行后控件将停留在执行后的状态 * setStartOffset(long startOffset)设置动画执行前的等待时间 * setRepeatCount(int repeatCount)设置动画重复次数 * setRepeatMode(int repeatMode)设置动画重复模式RESTART:重新从头开始执行REVERSE:反方向执行 */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBtnFirst = (Button) findViewById(R.id.btn_first); mBtnFirst.setOnClickListener(this); mBtnSecond = (Button) findViewById(R.id.btn_second); mBtnSecond.setOnClickListener(this); mBtnThird = (Button) findViewById(R.id.btn_third); mBtnThird.setOnClickListener(this); mBtnForth = (Button) findViewById(R.id.btn_forth); mBtnForth.setOnClickListener(this); mIv = (ImageView) findViewById(R.id.img); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_first: //TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f, // Animation.RELATIVE_TO_SELF,100f,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0f); // Animation.RELATIVE_TO_SELF表示相对于自身当前位置的XY坐标与下方写法效果相同 TranslateAnimation translateAnimation = new TranslateAnimation(0f, 0f, 100f, -1000f);//X轴起点,X轴终点,Y轴起点,Y轴终点 translateAnimation.setDuration(2000); mIv.startAnimation(translateAnimation); break; case R.id.btn_second: //参数,图片起始度数,图片目标度数,圆心X坐标,圆心Y坐标 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5F); rotateAnimation.setDuration(3000); mIv.startAnimation(rotateAnimation); break; case R.id.btn_third: //参数起始图片X轴比例,结束X轴比例,起始Y轴比例,结束Y轴比例,确定X轴坐标类型,0.5表明控件自身一般为X周,确定Y轴坐标类型,0.5表明控件自身一般为Y轴后四项不填默认放大缩小至控件左上角 ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.0f, 1f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); scaleAnimation.setDuration(5000); mIv.startAnimation(scaleAnimation); break; case R.id.btn_forth: //动画开始时透明度,动画结束时透明度 AlphaAnimation alphaAnimation = new AlphaAnimation(1,0f); alphaAnimation.setDuration(3000); mIv.startAnimation(alphaAnimation); break; } } }
第二部分 layout部分
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_first" android:layout_width="match_parent" android:layout_height="45dp" android:text="平移动画" /> <Button android:id="@+id/btn_second" android:layout_width="match_parent" android:layout_height="45dp" android:text="旋转动画" /> <Button android:id="@+id/btn_third" android:layout_width="match_parent" android:layout_height="45dp" android:text="放大缩小动画" /> <Button android:id="@+id/btn_forth" android:layout_width="match_parent" android:layout_height="45dp" android:text="透明度动画" /> <ImageView android:id="@+id/img" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="100dp" android:background="@drawable/ic_launcher" /> </LinearLayout>