|  | 
 
| 
概要: 在Android实现应用屏幕一定时间间隔下,随机出现多片花朵的效果,实现的原理和贪吃蛇的原理是互通的。在实现这样的效果中,关键几个技术点。
 自定义View,加载图片到内存,动态绘制窗体内容,
 
  
 
 1 技术点解析
 
 1.1自定义View
 自定义view主要是处理界面需要动态处理的情况,自定义view 主要继承与android.view.View类
 
 下面是自定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);
 
 }
 
  PerformSan.rar (48.76 KB, 下载次数: 9) | 
 |