概要:
      在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;">下面是自定view的实例

  • public class flowersView extends View {
  •     /**
  •      * 构造器
  •      */
  •      public flowersView(Context context, AttributeSet attrs, int defStyle) {
  •             super(context, attrs, defStyle);
  •   }
  •      public flowersView(Context context, AttributeSet attrs) {
  •             super(context, attrs);
  •         }
  •       @Override
  •      public void onDraw(Canvas canvas) {
  •           super.onDraw(canvas);
  •      }
  • }
  • 复制代码

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

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

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

  • //花图片
  •     Bitmap bitmap_flower =null;
  •        /**
  •         * 加载天女散花的花图片到内存中
  •         *
  •         */
  •      public void LoadFlowerImage()
  •      {
  •        Resources r = this.getContext().getResources();
  •                     bitmap_flower= ((BitmapDrawable) r.getDrawable(R.drawable.flower)).getBitmap();
  •      }
  • 复制代码

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

    l   动态生成五个花朵位置

  •   //花的位置
  • private Coordinate[] flowers=new Coordinate[5];
  •     //屏幕的高度和宽度 
  •       int view_height= 0; 
  • int view_width= 0; 
  •        /** 
  •         * 设置当前窗体的实际高度和宽度
  •         */
  •      public void SetView(int height ,int width)
  •      {
  •         view_height=height-100;
  •         view_width=width-50;
  •      }
  •        /**
  •         * 随机的生成花朵的位置
  •         *
  •         */
  •      public void addRandomFlower()
  •      {
  •      flowers[0]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  •      flowers[1]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  •      flowers[2]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  •      flowers[3]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  •      flowers[4]=new Coordinate(RNG.nextInt(view_width),RNG.nextInt(view_height));
  •      } 
  • l   根据花朵的位置重新的渲染窗体
  •      for (int x = 0; x < 5; x += 1) {
  •              canvas.drawBitmap(bitmap_flower,((float)flowers[x].x),((float)flowers[x].y),mPaint); 
  • }
  • 复制代码

    <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;">Android实现《天女散花》效果--(带源码) (48.76 KB, 下载次数: 206) 

    相关文章:

    • 2022-12-23
    • 2022-12-23
    • 2021-10-20
    • 2018-03-28
    • 2022-12-23
    • 2022-12-23
    • 2022-12-23
    • 2021-12-24
    猜你喜欢
    • 2021-07-22
    • 2022-12-23
    • 2021-09-11
    • 2021-11-06
    • 2021-12-21
    • 2022-12-23
    • 2022-12-23
    相关资源
    相似解决方案