Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android游戲開發 補間動畫 透明度漸變 旋轉 縮放 平移

android游戲開發 補間動畫 透明度漸變 旋轉 縮放 平移

編輯:關於Android編程

首先在res文件下新建anim文件夾
新建anim_alpha.xml代碼如下:

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

    <alpha android:duration="2000" android:fillafter="true" android:fromalpha="1" android:repeatcount="1" android:repeatmode="reverse" android:toalpha="0">

</alpha></set></code>

新建anim_translate.xml

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

<translate android:duration="2000" android:fillafter="true" android:fromxdelta="0" android:fromydelta="0" android:repeatcount="1" android:repeatmode="reverse" android:toxdelta="860" android:toydelta="0">

</translate></set></code>

新建anim_rotate.xml

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


    <rotate android:duration="2000" android:fromdegrees="0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotx="50%" android:pivoty="50%" android:todegrees="720">
    <rotate android:duration="2000" android:fromdegrees="360" android:interpolator="@android:anim/accelerate_interpolator" android:pivotx="50%" android:pivoty="50%" android:startoffset="2000" android:todegrees="0">

</rotate></rotate></set></code>

新建anim_scale.xml

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

    <scale android:duration="2000" android:fillafter="true" android:fromxscale="1" android:fromyscale="1" android:interpolator="@android:anim/accelerate_interpolator" android:pivotx="50%" android:pivoty="50%" android:repeatcount="1" android:repeatmode="reverse" android:toxscale="2.0" android:toyscale="2.0">

</scale></set></code>

然後修改main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">

    <imageview android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/cat">

    <linearlayout android:id="@+id/myli2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"><button android:id="@+id/alpha" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="透明"></button><button android:id="@+id/translate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋轉"></button><button android:id="@+id/scale" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="縮放"></button></linearlayout></imageview></linearlayout></code><button android:id="@+id/rotate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="平移"><code class=" hljs xml">
    

</code></button>

MainActivity.java

package com.mz.demo;

import android.app.Activity;
import android.os.Bundle;
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 {
    private ImageView img;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Animation animation_alpha = AnimationUtils.loadAnimation(this,
                R.anim.anim_alpha);
        final Animation animation_translate = AnimationUtils.loadAnimation(
                this, R.anim.anim_translate);
        final Animation animation_scale = AnimationUtils.loadAnimation(this,
                R.anim.anim_scale);
        final Animation animation_rotate = AnimationUtils.loadAnimation(this,
                R.anim.anim_rotate);
        img = (ImageView) findViewById(R.id.img);
        btn1 = (Button) findViewById(R.id.alpha);
        btn2 = (Button) findViewById(R.id.translate);
        btn3 = (Button) findViewById(R.id.scale);
        btn4 = (Button) findViewById(R.id.rotate);
        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_alpha);
            }
        });
        btn2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_translate);
            }
        });
        btn3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_scale);
            }
        });
        btn4.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_rotate);
            }
        });
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved