surface实例-小球弹起事例
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实例-小球弹起事例的更多相关文章
- c语言编程实例——小球跳动
1.预备知识 1.1 相关头文件 "#include"是c语言中用以申明所需调用的库函数或自定义函数的头文件路径及文件名.#include ""和#includ ...
- promise实例小球运动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- pygame 入门实例
本文基于win7(64) + py3.5(64)环境. 本文是这里的一篇学习笔记.加入了自己的理解. 本文最终目的是实现一个飞机躲避导弹的游戏. 1.核心概念 pygame 的核心概念有: Surfa ...
- android 动画具体解释(二)
以下就開始学习属性动画的基本使用方法,我们来看属性动画的继承关系,例如以下如所看到的: 显然关注的焦点应该是ValueAnimator,ObjectAnimator这两个类啦,ObjectAnimat ...
- 深入理解 Android 之 View 的绘制流程
概述 本篇文章会从源码(基于Android 6.0)角度分析Android中View的绘制流程,侧重于对整体流程的分析,对一些难以理解的点加以重点阐述,目的是把View绘制的整个流程把握好,而对于特定 ...
- 如何在springMVC 中对REST服务使用mockmvc 做测试
如何在springMVC 中对REST服务使用mockmvc 做测试 博客分类: java 基础 springMVCmockMVC单元测试 spring 集成测试中对mock 的集成实在是太棒了!但 ...
- Android:onNewIntent()触发机制及注意事项
一.onNewIntent() 在IntentActivity中重写下列方法:onCreate onStart onRestart onResume onPause onStop onDestro ...
- Activtiy完全解析(三、View的显示过程measure、layout、draw)
转载请标明出处: http://blog.csdn.net/xmxkf/article/details/52840065 本文出自:[openXu的博客] 在Activity完全解析的第一篇文章A ...
- 【view绘制流程】理解
一.概述 View的绘制是从上往下一层层迭代下来的.DecorView-->ViewGroup(--->ViewGroup)-->View ,按照这个流程从上往下,依次measure ...
随机推荐
- Dynamics AX 2012 R2 安装 AIF IIS上的Web服务
1.为什么使用IIS上的WEB服务 组件? 如果你要在Dynamics AX Service中使用HTTP Adapter,那么你就要安装IIS上的WEB服务 组件.HTTP Adapter会在IIS ...
- Date类型,Boolean类型,Number类型
1.Date类型: 1)创建Date对象: var date=new Date(); //Date对象会把当前日期和时间保存为其初始值 2)Date对象的方法: --这些得到的都是数字 ...
- Linux 打通ssh无密码登录
像hadoop和spark这类的集群,因为master节点要控制slave节点,以及各节点之间要交互信息,所以需要各节点之间能够互相无密码登录. 通过RSA保存密码, 基本操作如下: Step 1: ...
- 用webdriver+phantomjs实现无浏览器的自动化过程
环境准备 1. 安装python: 2. 安装pip: 3. 通过pip安装selenium: 4. 下载phantomJS的包并解压缩: 1. 若在Windows系统中,将下载的phantomjs文 ...
- java并发编程参考资料
1.java编程思想 2.java并发编程实战 3.java并发编程的艺术 4.http://www.infoq.com/cn/author/%E7%A8%8B%E6%99%93%E6%98%8E#文 ...
- centos 基本操作(快捷键开户终端,复制,粘贴,yum命令)
centos 开启终端默认时不禁止的,所以得手动开启(可自定义键盘)菜单:System->Preferences->Keyboard Shortcuts在Desktop分类下找到“Run ...
- Trasformation中的公式报错误,错误数据行定位
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 《BI那点儿事》双变量的相关分析——相关系数
例如,“三国人物是否智力越高,政治就越高”,或是“是否武力越高,统率也越高:准备数据分析环境: SELECT * FROM FactSanguo11 WHERE 姓名 IN ( N'荀彧', N'荀攸 ...
- C#中调用Matlab人工神经网络算法实现手写数字识别
手写数字识别实现 设计技术参数:通过由数字构成的图像,自动实现几个不同数字的识别,设计识别方法,有较高的识别率 关键字:二值化 投影 矩阵 目标定位 Matlab 手写数字图像识别简介: 手写 ...
- VPN推荐
最近ZF加强了对谷歌的屏蔽,推荐一些VPN azuressh.com ¥10/m http://www.archsocks.com/ ¥12/年 豆荚VPN,免费800M/月,偶尔连不上,可付费