码迷,mamicode.com
首页 > 移动开发 > 详细

Android动画效果(一) 任意两点间的抛物线动画

时间:2016-05-12 18:09:06      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:

先上图:

技术分享




这里要实现的是,点击上面的按钮后,将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动画效果(一) 任意两点间的抛物线动画

标签:

原文地址:http://blog.csdn.net/qq_18612815/article/details/51354495

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!