Android实现《天女散花》效果--(带源码)

概要:
      在Android 实现应用屏幕一定时间间隔下,随机出现多片花朵的效果,实现的原理和贪吃蛇的原理是互通的。在实现这样的效果中,关键几个技术点。
自定义View,加载图片到内存,动态绘制窗体内容,
<ignore_js_op style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 13.63636302947998px; line-height: 19.09090805053711px;">104524iuqec2squiqiy2s5.png 

1 技术点解析

1.1自定义View
   自定义view主要是处理界面需要动态处理的情况,自定义view 主要继承与Android.view.View类

下面是自定view的实例

  1. public class flowersView extends View {
  2.     /**
  3.      * 构造器
  4.      */
  5.      public flowersView(Context context, AttributeSet attrs, int defStyle) {
  6.             super(context, attrs, defStyle);
  7.   }
  8.      public flowersView(Context context, AttributeSet attrs) {
  9.             super(context, attrs);
  10.         }
  11.       @Override
  12.      public void onDraw(Canvas canvas) {
  13.           super.onDraw(canvas);
  14.      }
  15. }
复制代码

补充说明:
自定义view 在继承View类,主要关注于两块 1 构造器 2窗体重绘

1.2 加载图片到内存
在这个小应用中,会重复的出现多个花朵的图片,为节省内存,直接在应用开始时,直接将图片转化成内存的对象,在其后页面渲染时,直接用内存的对象

下面是加载图片到内存的实例

  1. //花图片
  2.     Bitmap bitmap_flower =null;
  3.        /**
  4.         * 加载天女散花的花图片到内存中
  5.         *
  6.         */
  7.      public void LoadFlowerImage()
  8.      {
  9.        Resources r = this.getContext().getResources();
  10.                     bitmap_flower= ((BitmapDrawable) r.getDrawable(R.drawable.flower)).getBitmap();
  11.      }
复制代码

1.3动态绘制窗体内容
   动态绘制窗体内容 分两块

l   动态生成五个花朵位置

  1.   //花的位置
  2. private Coordinate[] flowers=new Coordinate[5];
  3.     //屏幕的高度和宽度 
  4.       int view_height= 0; 
  5. int view_width= 0; 
  6.        /** 
  7.         * 设置当前窗体的实际高度和宽度
  8.         */
  9.      public void SetView(int height ,int width)
  10.      {
  11.         view_height=height-100;
  12.         view_width=width-50;
  13.      }
  14.        /**
  15.         * 随机的生成花朵的位置
  16.         *
  17.         */
  18.      public void addRandomFlower()
  19.      {
  20.      flowers[0]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  21.      flowers[1]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  22.      flowers[2]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  23.      flowers[3]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  24.      flowers[4]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  25.      } 
  26. l   根据花朵的位置重新的渲染窗体
  27.      for (int x = 0; x < 5; x += 1) {
  28.              canvas.drawBitmap(bitmap_flower,((float)flowers[x].x),((float)flowers[x].y),mPaint); 
  29. }
复制代码

<ignore_js_op style="word-wrap: break-word; color: rgb(68, 68, 68); font-family: Tahoma, 'Microsoft Yahei', Simsun; font-size: 13.63636302947998px; line-height: 19.09090805053711px;"> PerformSan.rar (48.76 KB, 下载次数: 206) 

posted on 2013-01-18 17:32  Code大蛇丸  阅读(882)  评论(0编辑  收藏  举报