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>




















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章