android 组件动画(一)——球的进入效果

首先看看效果:

 

// 宽和高

private int width = 100;

private int height = 100;

其实获取屏幕宽高的原因是为了适应android多种屏幕的变化,虽然很多时候可以用xml里的dip定位,但是更多时候却需要动态添加一些组件,然而在代码里面设置的值都是以xp来计算的,这样做出来的软件或者游戏根本就不能很好的适应屏幕。所以,如果不通过xml布局而要动态解决,那我觉得软件界面的设计主要就是自己将界面宽高各分成十份(当然这里分的粒度越少界面控制得越精细,不过管理起来也较为麻烦),然后在根据需要给它以宽和高。我也是说说自己做这些的感想,如果大家有更好的建议,不吝请教^-^ 

//例子

private String list = "3 5 7 9 0 43";

//要给组件的动画的集合

private static final int[] INTERPOLATORS = {

           android.R.anim.accelerate_interpolator,

           android.R.anim.decelerate_interpolator,

           android.R.anim.accelerate_decelerate_interpolator,

           android.R.anim.anticipate_interpolator,

           android.R.anim.overshoot_interpolator,

           android.R.anim.anticipate_overshoot_interpolator,

           android.R.anim.bounce_interpolator };

/////////balls_item.xml

<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="fill_parent" android:layout_height="fill_parent"

    android:orientation="horizontal">

 

    <LinearLayout android:layout_width="fill_parent"

       android:layout_height="wrap_content" android:orientation="horizontal"

       android:id="@+id/layout" android:paddingRight="35dip"></LinearLayout>

 

    <ImageView android:id="@+id/delete" android:scaleType="fitXY"

       android:src="@drawable/delete" android:layout_width="30dip"

       android:layout_height="30dip" android:layout_alignParentRight="true">

    </ImageView>

</RelativeLayout>

/////////testBallSliding.java

public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

      

       setContentView(R.layout.balls_item);

 

       width = getWindowManager().getDefaultDisplay().getWidth();

       height = getWindowManager().getDefaultDisplay().getHeight();

 

       LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

 

       String[] buttons = list.split(" ");

      

      

       for (int i = 0; i < buttons.length; i++) {

           Button button = new Button(this);

           button.setPadding(2, 2, 2, 2);

           //这里就是控制动画的显示位置

           Animation animation = new TranslateAnimation(width + 50, i * 40, 0,

                  0);

//设置动画的运动效果

           animation.setInterpolator(getApplicationContext(), INTERPOLATORS[i

                  % INTERPOLATORS.length]);

           animation.setDuration(4000 - 50 * i);

           button.setAnimation(animation);

 

           button.setText(buttons[i]);

           button.setBackgroundResource(R.drawable.redball);

 

           ImageView delete = (ImageView) findViewById(R.id.delete);

           // 删除的监听

           // .........

          

           layout.addView(button,new LayoutParams(40, 40));

       }

 

    }

这里先介绍简单为后面的内容做一下铺垫。

本文为原创,如需转载,请注明作者和出处,谢谢!

代码下载

https://files.cnblogs.com/not-code/testBallSliding.zip

posted @ 2011-04-10 22:20  没有代码  阅读(3227)  评论(0编辑  收藏  举报