ball.java

package com.example.sufacedemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics; /**
* 球类
* @author zcl
*
*/
public class Ball { /**
* 球的高
*/
public static final int HEIGHT = 93;
/**
* 球的宽
*/
public static final int WIDTH = 93;
private static final int STEPLENGTH = 10;//每次运动的间距
private static final float REDUCEPERCENTAGE = 0.35F;//递减系数
private int stepReduce ;//每次反向运动的缩短的距离 private float runX ;//球的位置
private float runY ;//球的位置
private BallSurfaceView bsv ;
private boolean upDirection = false;//if true,up direction,or is down direction
private float maxHeight ;//当前运动最高的高度
private Paint paint ; Bitmap ballBitmap ;//球的图片
SportActivity sa ;
public Ball(float initX , float initY , BallSurfaceView bsv){
this.runX = initX;
this.runY = initY ;
maxHeight = initY;
this.bsv = bsv;
ballBitmap = BitmapFactory.decodeResource(bsv.getResources(), R.drawable.ball);//加载图片
paint = new Paint();
sa = bsv.sportActivity;
} public void onDraw(Canvas canvas) {
int c = paint.getColor();//保存颜色,之后还原为之前颜色
boundaryTest();
if(canvas != null) canvas.drawBitmap(ballBitmap,runX,runY,paint);
paint.setColor(c);
move();
}
/**
* 运动
*/
private void move() {
if(maxHeight >= (sa.screenHeight - HEIGHT)) {
return;
}
if(upDirection){//向上
runY = runY + STEPLENGTH ;
}else{
runY = runY - STEPLENGTH ;
}
} /**
* 边界检测,使球不会飞出边界
*/
private void boundaryTest(){ if(runY > sa.screenHeight - HEIGHT){//向下运动到头
upDirection = !upDirection;//方向置反
runY = sa.screenHeight - HEIGHT;
stepReduce = (int) (maxHeight * REDUCEPERCENTAGE);
maxHeight = maxHeight + stepReduce ;//最大高度递减 }
if(runY < maxHeight ){//向上运动到头
upDirection = !upDirection;//方向置反
if(maxHeight >= (sa.screenHeight - HEIGHT)) return;
runY = maxHeight ; }
}
}

  

BallSurfaceView

package com.example.sufacedemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView; public class BallSurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
SportActivity sportActivity;// 调用该SurfaceView的上下文引用
private Ball ball;// 小球
SurfaceHolder holder; public BallSurfaceView(Context context) {
super(context);
this.sportActivity = (SportActivity) context;
ball = new Ball(100, 100, this);
holder = this.getHolder();
holder.addCallback(this);
} @SuppressLint("WrongCall")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); if (canvas == null)
canvas = holder.lockCanvas();// 锁定画布
Paint p = new Paint();
int c = p.getColor();
p.setColor(Color.WHITE);// 设置背景白色
if (canvas != null)
canvas.drawRect(0, 0, sportActivity.screenWidth,
sportActivity.screenHeight, p);
p.setColor(c);
ball.onDraw(canvas);
holder.unlockCanvasAndPost(canvas);// 释放锁
} @Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) { } @Override
public void surfaceCreated(SurfaceHolder holder) {
new RefreshThread().start();
} @Override
public void surfaceDestroyed(SurfaceHolder holder) { } private class RefreshThread extends Thread { @SuppressLint("WrongCall")
@Override
public void run() { while (true) {
Canvas canvas = null;
try {
onDraw(canvas);
} catch (Exception e) {
e.printStackTrace();
} try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} } }

  

SportActivity.java

package com.example.sufacedemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager; public class SportActivity extends Activity { public int screenWidth ;
public int screenHeight ;
BallSurfaceView bsv ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bsv = new BallSurfaceView(this);
//获得屏幕尺寸
DisplayMetrics dm = new DisplayMetrics();
dm = this.getApplicationContext().getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
//下两句为设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN ,
WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(bsv);
}
}

  

surface实例-小球弹起事例的更多相关文章

  1. c语言编程实例——小球跳动

    1.预备知识 1.1 相关头文件 "#include"是c语言中用以申明所需调用的库函数或自定义函数的头文件路径及文件名.#include ""和#includ ...

  2. promise实例小球运动

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. pygame 入门实例

    本文基于win7(64) + py3.5(64)环境. 本文是这里的一篇学习笔记.加入了自己的理解. 本文最终目的是实现一个飞机躲避导弹的游戏. 1.核心概念 pygame 的核心概念有: Surfa ...

  4. android 动画具体解释(二)

    以下就開始学习属性动画的基本使用方法,我们来看属性动画的继承关系,例如以下如所看到的: 显然关注的焦点应该是ValueAnimator,ObjectAnimator这两个类啦,ObjectAnimat ...

  5. 深入理解 Android 之 View 的绘制流程

    概述 本篇文章会从源码(基于Android 6.0)角度分析Android中View的绘制流程,侧重于对整体流程的分析,对一些难以理解的点加以重点阐述,目的是把View绘制的整个流程把握好,而对于特定 ...

  6. 如何在springMVC 中对REST服务使用mockmvc 做测试

    如何在springMVC 中对REST服务使用mockmvc 做测试 博客分类: java 基础 springMVCmockMVC单元测试  spring 集成测试中对mock 的集成实在是太棒了!但 ...

  7. Android:onNewIntent()触发机制及注意事项

    一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestro ...

  8. Activtiy完全解析(三、View的显示过程measure、layout、draw)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52840065 本文出自:[openXu的博客]   在Activity完全解析的第一篇文章A ...

  9. 【view绘制流程】理解

    一.概述 View的绘制是从上往下一层层迭代下来的.DecorView-->ViewGroup(--->ViewGroup)-->View ,按照这个流程从上往下,依次measure ...

随机推荐

  1. JQuery 层次选择器

    <!DOCTYPE HTML> <html> <head> <title> 使用jQuery层次选择器 </title> <scrip ...

  2. Ajax中dataType数据类型

    今天项目中使用Ajax向后台保存数据,其中dataType为'json';当请求成功后,没有走success回调,反而走了error:数据库已经成功保存数据了. 后来搞半天才知道原来dataType指 ...

  3. JAVA基础知识之NIO.2——Path,Paths,Files

    NIO.2 JDK7对NIO进行了重大改进,主要包含以下两方面 新增Path接口,Paths工具类,Files工具类. 这些接口和工具类对NIO中的功能进行了高度封装,大大简化了文件系统的IO编程. ...

  4. webapi 解决ajax跨域请求问题

    webapi在配置文件中加入这几句就可以解决ajax(同源策略是JavaScript里面的限制,其他的编程语言,比如在C#,Java或者iOS等其他语言中是可以调用外部的WebService,也就是 ...

  5. ios html5 网页取消默认样式

    ios的的默认样式修改成扁平化的样式 重要的一句css  -webkit-appearance: none;  将样式清除 单数会出现将raido的选择按钮也会消失 所以需要对radio的样式进行重新 ...

  6. html5 svg动画

    http://www.zhangxinxu.com/sp/svg/ 以上是svg的一个线上编辑器,也可以adobe Illustrator制作生成. 我们通过以上编辑器可以获得以下代码. 例: < ...

  7. 查看SQL SERVER数据库运行参数和连接数

    ---查看当前数据库系统所有请求情况.我只列出了我认为比较重要有助于我解决问题的字段. SELECT ds.session_id, ds.status, Db_name(dr.database_id) ...

  8. wifipineapple使用教程

    1.把开关拨到右边 如果有灯亮说明有电 4个灯全亮说明电量是满的 以此类推 如果一个也不亮说明没电了需要用充电器充电 2.把开关拨到左边打开wifi的开关  会开启一个wifi大概一分钟左右会有wif ...

  9. 大分享-hibernate,springmvc,easyui简要介绍

    近期公司一直在做项目,主要用到了springMVC,eseayui,hibernate几大框架.近一个月的时间,个人就目前自我知识给予分享. 很多公司使用mybatis产品,综合所述其最大优点是全SQ ...

  10. Hibernate各种主键生成策略与配置详解

    出自:http://www.cnblogs.com/kakafra/archive/2012/09/16/2687569.html 1.assigned 主键由外部程序负责生成,在 save() 之前 ...