android動畫(平移,旋轉,縮放,透明度)

介紹一下 android的基本動畫:
1創建佈局文件

<LinearLayout 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:orientation="vertical"
     >

    <Button
        android:id="@+id/button_alpha"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="透明度" />

    <Button
        android:id="@+id/button_tran"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="平移" />

    <Button
        android:id="@+id/button_rotate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="旋轉" />
    <Button
        android:id="@+id/button_scal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="縮放" />
    <Button
        android:id="@+id/button_set"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="綜合" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

2 通過xml文件添加動畫效果,要在res文件夾下創建anim文件,裏邊創建,alpha.xml,rotate.xml,scale.xml,translate.xml,set.xml
文件

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

</alpha>

這是透明度動畫,duration是動畫所持續的時長,fromAlpht是動畫開始的透明度,toAlpha是動畫結束的透明度。

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromDegrees="360"
    android:toDegrees="0" >

</rotate>

這是旋轉動畫,設置的是時間和旋轉的角度。

<scale 
    android:fromXScale="0"
    android:fromYScale="0"
    android:toXScale="1"
    android:toYScale="1"
    android:duration="2000"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="0"
    android:pivotY="50%"

    xmlns:android="http://schemas.android.com/apk/res/android">


</scale>

上述代碼是縮放動畫,fromXScale是縮放之前的x的比例,fromYScale這是y軸之前的縮放比例;toXScale是縮放後的x比例,toYScale是縮放後y的比例;interpolator是動畫進行的效果(由慢到快再到慢),pivotX是縮放位置的x軸的座標點,pivotY是縮放位置的y軸座標點;


<?xml version="1.0" encoding="utf-8"?>
<translate 
    android:duration="2000"
    android:fromXDelta="-100"
    android:fromYDelta="-100"
    android:toXDelta="200"
    android:toYDelta="200"

    xmlns:android="http://schemas.android.com/apk/res/android">


</translate>

平移動畫:設置的是時間和平移的位置這個就不多介紹了道理都一樣。

3 添加java代碼
代碼很簡單就不做過多的解釋了;

package com.example.day1501;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {
    private Button button1, button2, button3, button4,button5;
    private ImageView imageView;
    private Animation animation;
    private Animation animation1;
    private Animation animation2;
    private Animation animation3;
    private Animation animation4;

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

    private void setViews() {
        button1 = (Button) findViewById(R.id.button_alpha);
        button2 = (Button) findViewById(R.id.button_tran);
        button3 = (Button) findViewById(R.id.button_rotate);
        button4 = (Button) findViewById(R.id.button_scal);
        button5 = (Button) findViewById(R.id.button_set);
        imageView = (ImageView) findViewById(R.id.imageView1);
        button1.setOnClickListener(this);
        button4.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button5.setOnClickListener(this);
        imageView.setOnClickListener(this);

    }

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_alpha:
            animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
            imageView.setAnimation(animation);
            break;
        case R.id.button_tran:
            animation1 = AnimationUtils.loadAnimation(this, R.anim.translate);
            imageView.setAnimation(animation1);
            break;
        case R.id.button_rotate:
            animation2 = AnimationUtils.loadAnimation(this, R.anim.rotate);
            imageView.setAnimation(animation2);
            break;
        case R.id.button_scal:
            animation3 = AnimationUtils.loadAnimation(this, R.anim.scale);
            imageView.setAnimation(animation3);
            break;
        case R.id.button_set:
            animation4 = AnimationUtils.loadAnimation(this, R.anim.set);
            imageView.setAnimation(animation4);
            break;

        default:
            break;
        }

    }

}

最後添加一個在anim中添加一個set文件,
就是綜合了上述所有的動畫一起顯示,會有不一樣的效果;

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

    <alpha
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="1000"
        android:fromAlpha="0.1"
        android:toAlpha="1" >
    </alpha>

    <rotate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromDegrees="360"
        android:toDegrees="0" >
    </rotate>

    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="0"
        android:fromYScale="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="0"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" >
    </scale>

    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXDelta="-100"
        android:fromYDelta="-100"
        android:toXDelta="200"
        android:toYDelta="200" >
    </translate>

</set>

以上就是android的簡單的基本動畫。

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