Android 简单动画

public class MainActivity extends AppCompatActivity {

private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	//获取图片控件
	iv = (ImageView) findViewById(R.id.iv);
}

/**
 * @param v
 * 透明动画
 */
public void alpha(View v) {
	// 创建透明度动画, 1.0:完全不透明,,  0.0:完全透明
	AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
	//设置时长
	alphaAnimation.setDuration(3000);
	//启动 动画
	iv.startAnimation(alphaAnimation);


}

/**
 *Translate:平移动画
 * @param v
 */
public void trans(View v) {
	//前两个参数是控制X轴, 后两个参数是控制Y轴;
	TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 100.0f, 0.0f, 100.0f);
	translateAnimation.setDuration(3000);
	iv.startAnimation(translateAnimation);


}

/**
 *rotate :旋转动画
 *
 //参数1:从哪个旋转角度开始

 //参数2:转到什么角度

 //-------(后4个参数用于设置围绕着旋转的圆的圆心在哪里)

 //参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标

 //参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴

 //参数5:确定y轴坐标的类型

 //参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴

 */

public void rotate(View v) {
	RotateAnimation rotateAnimation = new RotateAnimation(
			0, 360,
			Animation.RELATIVE_TO_SELF, 0.5f,
			Animation.RELATIVE_TO_SELF, 1.0f);
	rotateAnimation.setDuration(3000);
	iv.startAnimation(rotateAnimation);



}


/**
 * scale:缩放动画
 * @param v
 */
public void scale(View v) {
	// X轴从哪到哪,
	//Y 轴从哪到哪
	ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f);


	RotateAnimation rotateAnimation = new RotateAnimation(
			0, 360,
			Animation.RELATIVE_TO_SELF, 0.5f,
			Animation.RELATIVE_TO_SELF, 0.5f);

	//动画集合
	AnimationSet animationSet = new AnimationSet(true);

	animationSet.setDuration(3000);
	animationSet.addAnimation(scaleAnimation);
	animationSet.addAnimation(rotateAnimation);


	iv.startAnimation(animationSet);

	// 动画监听
	animationSet.setAnimationListener(new Animation.AnimationListener() {
		@Override
		public void onAnimationStart(Animation animation) {
			
		}

		//动画结束的时候回调
		@Override
		public void onAnimationEnd(Animation animation) {
			Toast.makeText(MainActivity.this, "动画播放结束", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}
	});



}

/**
 * 使用布局来实现动画
 * @param view
 */
public void goLayout(View view) {
	startActivity(new Intent(MainActivity.this,Main2Activity.class));

}

}

布局文件

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:onClick="alpha"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="透明" />

    <Button
         android:onClick="trans"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="位移" />

    <Button
        android:onClick="rotate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="旋转" />

    <Button
        android:onClick="scale"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="缩放" />
</LinearLayout>

<ImageView 
    android:id="@+id/iv"
    android:layout_width="wrap_content"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="100dp"
    android:layout_height="wrap_content"
    android:src="@drawable/aa"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"

    android:layout_alignParentTop="true"
    android:layout_marginTop="61dp"
    android:onClick="goLayout"
    android:text="跳转到布局实现" />

猜你喜欢

转载自blog.csdn.net/qq_43567217/article/details/83626661