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

來源:互聯網
上載者:User

標籤:

先:




這裡要實現的是,點擊上面的按鈕後,將TextView隨機移動到底部按鈕的位置


首先,將底部按鈕放入list中,方便後面隨機取值

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

然後就是點擊按鈕後的拋物線動畫了

點擊按鈕後,先寫一個數組用來儲存點擊按鈕的X、Y座標,然後new一個用來展示拋物線的控制項,樓主這裡用的TextView,也可以換成其他任何控制項

int[] start_location = new int[2];// 一個整型數組,用來儲存按鈕的在螢幕的X、Y座標v.getLocationInWindow(start_location);// 這是擷取當前點擊的按鈕在螢幕的X、Y座標(這也是動畫開始的座標)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 x = location[0];int y = location[1];LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);lp.leftMargin = x;lp.topMargin = y;view.setLayoutParams(lp);

緊接著,就是寫拋物線的動畫了。

拋物線其實就是兩個位移動畫,一個橫向移動,一個豎向移動,兩個動畫同時執行,就有了拋物線的效果

<span style="color:#000000;">int endX = 0 - start_location[0] + list.get(ra.nextInt(list.size())).getLeft();// 動畫位移的X座標int endY = end_location[1] - start_location[1];// 動畫位移的y座標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);// 動畫重複執行的次數translateAnimationX.setFillAfter(true);AnimationSet set = new AnimationSet(false);set.setFillAfter(false);set.addAnimation(translateAnimationY);set.addAnimation(translateAnimationX);set.setDuration(800);// 動畫的執行時間view.startAnimation(set);</span>


最後,監聽動畫,在動畫執行的時候將要移動的控制項顯示出來,動畫結束了將之隱藏


完整代碼如下:

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;/*** *  * @author 帽簷遮不住陽光 *  * @date 2016/5/9 * */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) {  // TODO Auto-generated method stub  switch (v.getId()) {  case R.id.btn:   int[] start_location = new int[2];// 一個整型數組,用來儲存按鈕的在螢幕的X、Y座標   v.getLocationInWindow(start_location);// 這是擷取當前點擊的按鈕在螢幕的X、Y座標(這也是動畫開始的座標)   TextView te = new TextView(this);   te.setText("啊");   move(te, start_location);   break;  } } private void move(final View v, int[] start_location) {  viewGroup = null;  viewGroup = createAnimLayout();  viewGroup.addView(v);// 把要移動的控制項添加到動畫層  final View view = addViewToAnimLayout(viewGroup, v, start_location);  int[] end_location = new int[2];// 這是用來儲存動畫結束位置的X、Y座標  Random ra = new Random();  for (int i = 0; i < list.size(); i++) {   list.get(ra.nextInt(list.size())).getLocationInWindow(end_location);   // 計算位移   int endX = 0 - start_location[0]     + list.get(ra.nextInt(list.size())).getLeft();// 動畫位移的X座標   int endY = end_location[1] - start_location[1];// 動畫位移的y座標   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);// 動畫重複執行的次數   translateAnimationX.setFillAfter(true);   AnimationSet set = new AnimationSet(false);   set.setFillAfter(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) {     // TODO Auto-generated method stub    }    // 動畫的結束    @Override    public void onAnimationEnd(Animation animation) {     v.setVisibility(View.GONE);    }   });   break;  } } /**  * 建立動畫層  *   * @return  */ 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; } private View addViewToAnimLayout(final ViewGroup vg, final View view,   int[] location) {  int x = location[0];  int y = location[1];  LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(    LinearLayout.LayoutParams.WRAP_CONTENT,    LinearLayout.LayoutParams.WRAP_CONTENT);  lp.leftMargin = x;  lp.topMargin = y;  view.setLayoutParams(lp);  return view; }}

:http://download.csdn.net/detail/qq_18612815/9514682







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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.