首先看看效果:

// 宽和高

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));

}

}

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

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

代码下载

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

更多相关文章

  1. 仿MIUI的Toast动画效果实现
  2. Android(安卓)中使用OpenGL ES进行2D开发(绘制第一个三角形番外篇
  3. android 的布局单位 dip dp sp px总结
  4. Android(安卓)环境配置,和一系列问题的解决方案
  5. AssetManager读取assets下多张图片资源输出到ImageView动画
  6. Android中进程与线程及如何在子线程中操作UI线程
  7. Android之自定义控件
  8. android动手写控件系列——老猪叫你写相机
  9. Android(安卓)总结4种线程中操作UI界面的方法

随机推荐

  1. Android自动化测试之MonkeyRunner-之getP
  2. Android(安卓)强制停止RecyclerView滑动
  3. Android应用自启动
  4. Android外置存储器(SD卡)工具类
  5. Gradle DSL method not found: 'android(
  6. Intent Server Environment
  7. android 网络
  8. android渲染网页时onPageFinished不触发
  9. Android中声音处理流程
  10. android 字符串加解密算法