Android动画应用总结1

本人其实比较懒,不太喜欢写文字,喜欢写代码比较多,不过今天有点时间还是写一点.

Android里面的基本动画看起来还是比较简单的


手机的坐标系



知道坐标系很重要,这样容易设置起始点和结束点 ,中心点就是(0,0) 手机左上角的点


动画配置都放在res/anim目录下,直接写代码也是OK

首先看渐变,就是设置物体从透明到完全显示的一个渐变效果

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <alpha
        android:duration="800"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

duration就是动画时间,单位为毫秒

fromAlpha其实透明度

toAlpha动画结束透明度


旋转动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <rotate
        android:duration="800"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="+270" />

</set>

fromDegrees开始角度,UI组件的顶点也是(0,0)开始和手机一样

toDegrees结束角度

pivotX 相对于物体宽度比例位置 50%就是一半,同理pivotY也是一样的意思


缩放动画

<?xml version="1.0" encoding="utf-8"?>   
<set xmlns:android="http://schemas.android.com/apk/res/android">   
<scale     
    android:fromXScale="0.0"     
    android:toXScale="1.0"     
    android:fromYScale="0.0"     
    android:toYScale="1.0"     
    android:pivotX="50%"     
    android:pivotY="50%"     
    android:startOffset="0"     
    android:duration="800"    
    />   
</set>   

fromXScale 

fromYScale 

从X,Y开始的缩放比例

toXScale

toYScale

结束时的X,Y缩放比例


水平移动动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate android:fromXDelta="-100%p" android:toXDelta="0"
		android:duration="800" />
</set>

fromXDelta

fromYDelta 

从X或者Y开始移动的点 ,fromXDelta 为 -100%p 就是从 屏幕的左边开始移动


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate android:fromXDelta="100%p" android:toXDelta="0"
		android:duration="800" />

</set>


测试的代码.

public class MainActivity extends Activity {

	private View controlView;

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

		controlView = findViewById(R.id.controlView);
		findViewById(R.id.show).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				show();
			}
		});

		findViewById(R.id.close).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				close();
			}
		});
		
		findViewById(R.id.left).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				left();
			}
		});
		
		findViewById(R.id.right).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				right();
			}
		});
		
		
		findViewById(R.id.rotate).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				rotate();
			}
		});
	}
	
	private void rotate() {
		Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);//R.anim.rotate 等
		controlView.setVisibility(View.VISIBLE);
		controlView.startAnimation(a);
		
		
	}

	protected void show() {
		Animation a = AnimationUtils.loadAnimation(this, R.anim.up_from_bottom);
		controlView.setVisibility(View.VISIBLE);
		controlView.startAnimation(a);
	}

	protected void close() {
		Animation a = AnimationUtils.loadAnimation(this, R.anim.down_from_up);
		controlView.setVisibility(View.INVISIBLE);
		controlView.startAnimation(a);
	}

	protected void left() {
		Animation a = AnimationUtils.loadAnimation(this, R.anim.left_in);
		controlView.setVisibility(View.VISIBLE);
		controlView.startAnimation(a);
	}

	protected void right() {
		Animation a = AnimationUtils.loadAnimation(this, R.anim.right_in);
		controlView.setVisibility(View.VISIBLE);
		controlView.startAnimation(a);
	}

	@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;
	}

}


main.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:background="#336699" >
    
        <Button
        android:id="@+id/rotate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="rotate" />

    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="show" />

    <Button
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:text="left" />

    <Button
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="right" />

    <LinearLayout
        android:id="@+id/controlView"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true"
        android:background="#339988"
        android:orientation="vertical"
        android:visibility="gone" >

        <Button
            android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:text="close" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="100dp"
            android:text="I want control"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />
    </LinearLayout>

</RelativeLayout>




















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值