Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動畫效果(一):任意兩點間的拋物線動畫

編輯:關於 Android 程式設計

先上效果圖:

這裡要實現的效果是:點擊上方按鈕後,讓一個 TextView 沿著拋物線軌跡隨機移動到下方某個按鈕的位置。

首先,把下方的按鈕放進一個 list 中,方便後續隨機取值:

 list = new ArrayList<Button>(); list.add(btn1); list.add(btn2); list.add(btn3); list.add(btn4);

接著就是重點——點擊按鈕後的拋物線動畫。先宣告一個整型陣列來存放被點擊按鈕的螢幕 X、Y 座標(也就是動畫的起始座標),再 new 一個用來承載拋物線的控件(這裡用 TextView,也可以換成任何 View):

 int[] start_location = new int[2]; v.getLocationInWindow(start_location); TextView te = new TextView(this); te.setText("啊");

然後把要移動的控件放進一個動畫層中:

 ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView(); LinearLayout animLayout = new LinearLayout(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); animLayout.setLayoutParams(lp); animLayout.setId(Integer.MAX_VALUE); animLayout.setBackgroundResource(android.R.color.transparent); rootView.addView(animLayout);

接著寫拋物線動畫。拋物線其實就是兩個位移動畫的組合:一個橫向移動、一個縱向移動,兩者同時執行,就產生了拋物線的效果。

 int endX = 0 - start_location[0] + list.get(ra.nextInt(list.size())).getLeft(); int endY = end_location[1] - start_location[1]; TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0); translateAnimationX.setInterpolator(new LinearInterpolator()); translateAnimationX.setRepeatCount(0); translateAnimationX.setFillAfter(true); TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY); translateAnimationY.setInterpolator(new AccelerateInterpolator()); translateAnimationY.setRepeatCount(0); AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(translateAnimationY); set.addAnimation(translateAnimationX); set.setDuration(800); view.startAnimation(set);

最後,為動畫加上監聽:動畫執行時把要移動的控件顯示出來,動畫結束後再將它隱藏。

完整代碼如下:

 package com.example.movetest;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
    private List<Button> list = null;
    private ViewGroup viewGroup;
    private Button btn1, btn2, btn3, btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn).setOnClickListener(this);
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        btn4 = (Button) findViewById(R.id.btn4);
        list = new ArrayList<Button>();
        list.add(btn1); list.add(btn2); list.add(btn3); list.add(btn4);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                int[] start_location = new int[2];
                v.getLocationInWindow(start_location);
                TextView te = new TextView(this);
                te.setText("啊");
                move(te, start_location);
                break;
        }
    }

    private void move(final View v, int[] start_location) {
        viewGroup = createAnimLayout();
        viewGroup.addView(v);
        int[] end_location = new int[2];
        Random ra = new Random();
        end_location = ...; // 取隨機目標按鈕座標
        int endX = 0 - start_location[0] + v.getLeft();
        int endY = end_location[1] - start_location[1];
        TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        AnimationSet set = new AnimationSet(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(800);
        view.startAnimation(set);
        set.setAnimationListener(new AnimationListener() {
            @Override public void onAnimationStart(Animation animation) { v.setVisibility(View.VISIBLE); }
            @Override public void onAnimationRepeat(Animation animation) { }
            @Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.GONE); }
        });
    }

    private ViewGroup createAnimLayout() {
        ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
        LinearLayout animLayout = new LinearLayout(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setId(Integer.MAX_VALUE);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }
}
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved