android 动画之---位移抛物线

今天给大家说说android里面的动画,如何实现抛物线移动

 欢迎大家进群:574605026   开启我们的开发之旅

首先简单给大家科普下android之中的四种动画
1、AlphaAnimation 透明度动画 
2、ScaleAnimation 缩放动画 
3、TranslateAnimation 位移动画 
4、RotateAnimation 旋转动画

在这里呢,我简单给大家说说位移动画

TranslateAnimation是移动的动画效果。它有三个构造函数,分别是:

  1.public  TranslateAnimation(Context context,AttributeSet attrs)   略过

  2.public  TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

  这个是我们最常用的一个构造方法,

  float fromXDelta:这个参数表示动画开始的点离当前View X坐标上的差值;

  float toXDelta, 这个参数表示动画结束的点离当前View X坐标上的差值;

  float fromYDelta, 这个参数表示动画开始的点离当前View Y坐标上的差值;

  float toYDelta)这个参数表示动画开始的点离当前View Y坐标上的差值;

  如果view在A(x,y)点 那么动画就是从B点(x+fromXDelta, y+fromYDelta)点移动到C 点(x+toXDelta,y+toYDelta)点.

 

  3.public  TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

  fromXType:第一个参数是x轴方向的值的参照(Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF,or Animation.RELATIVE_TO_PARENT);

  fromXValue:第二个参数是第一个参数类型的起始值;

  toXType,toXValue:第三个参数与第四个参数是x轴方向的终点参照与对应值;

  后面四个参数就不用解释了。如果全部选择Animation.ABSOLUTE,其实就是第二个构造函数。

      以x轴为例介绍参照与对应值的关系:

      如果选择参照为Animation.ABSOLUTE,那么对应的值应该是具体的坐标值,比如100到300,指绝对的屏幕像素单位

      如果选择参照为Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT指的是相对于自身或父控件,对应值应该理解为相对于自身或者父控件的几倍或百分之多少。多试参数就明白了。


大家先看图,我们到底实现的是一个什么功能
如果让一个小球从A已抛物线的方式位移到B。
把A点和B点都当作是一个view
1、第一步
获取到A点和B的坐标
     int[] startLocation = new int[2];// 一个整型数组,用来存储按钮的在屏幕的XY坐标
     av.getLocationInWindow(startLocation);// 这是获取A的在屏幕的XY坐标(这也是动画开始的坐标)

    int[] endLocation = new int[2];// 一个整型数组,用来存储按钮的在屏幕的XY坐标
     bv.getLocationInWindow(startLocation);// 这是获取B的在屏幕的XY坐标(这也是动画结束的坐标)

2、第二步

自定义一个Imageview
   ImageView ball = new ImageView(mContext);// ball动画的图片,我的是一个小球
   ball.setImageResource(R.mipmap.icon_fansnum_like_on);// 设置ballImg的图片
       
把ball 动画图片添加到动画层,假设存在
    setAnim(ball);

3、第三步
       在第二步,有一个setAnim,设置动画方法,现在就来补全
public void setAnim(final View v) {
    anim_mask_layout = null;
    anim_mask_layout = createAnimLayout();
    anim_mask_layout.addView(v);//把动画小球添加到动画层
    // 计算位移
    int endX = 0 - startLocation[0] + 20;// 动画位移的X坐标
    int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
    System.out.println("=====x==="+endX);
    System.out.println("=====y==="+endY);
    TranslateAnimation translateAnimationX = new TranslateAnimation(0,
            endX, 0, 0);
    translateAnimationX.setInterpolator(new LinearInterpolator()); //让动画已均匀的速度改变
    translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
    translateAnimationX.setFillAfter(true); //执行完毕,利用视图setLayoutParams来重新定位

    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 Animation.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);
            addCart();
        }
    });

}
 哎 anim_mask_layout是什么,问的话,他就是小球需要滚动的那个动画层
	
/**
 * 创建动画层
 * @return
 */
private ViewGroup createAnimLayout() {
//这里大家应该都能看懂了,我就不过多的写注释了
    ViewGroup rootView = (ViewGroup) getActivity().getWindow().getDecorView();
    LinearLayout animLayout = new LinearLayout(getActivity());
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);
    animLayout.setLayoutParams(lp);
    animLayout.setId(Integer.MAX_VALUE - 1);
    animLayout.setBackgroundResource(android.R.color.transparent);
    rootView.addView(animLayout);
    return animLayout;
}

private View addViewToAnimLayout(final ViewGroup parent, final View view,
                                 int[] location) {
//定义小球在动画层的位置,A坐标的位置,及距离左边的距离为X轴坐标,距离高的距离为Y轴坐标
    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;
}
这就是一个简单的位移动画,其实没有大家想的那么难,主要是需要熟悉android developer api
大家可以多试试,开发就是在不断的尝试,不断的分享,让大家共同进步。


  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值