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 ...
随机推荐
- IBM DB2 For Linux安装指南(转)
一.安装前准备工作: 1.对于Linux系统,需要安装以下软件包: 2.Linux内核设置: 编辑/etc/sysctl.conf文件,加入如下内容: 3.创建相应用户以及组: 官方文档给出必须创建三 ...
- Unix/Linux编程实践教程(三:代码、测试)
测试logfilec.c的时候,有个sendto(sock,msg,strlen(msg),0,&addr,addrlen),编译时提示: logfilec.c:30: warning: pa ...
- popUpWindow 动画无法超出窗体的解决方案
popupWindow 做动画时,当要求有一个放大动画时,动画无法超出窗体,给人的感觉是只有内容在放大,窗体不动. 这是由于窗口大小固定的原因,解决方案是加大popUpwindow的 大小. 一个比较 ...
- 【转】malloc与free的底层实现
本文转自:http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201210975312473/ 如何查看进程发生缺页中断的次数? 用ps ...
- SQL Server T-SQL高级查询
name like 'ja%'; select * from student where name not like '%[j,n]%'; select * from student where na ...
- ArcGIS Javascript地图上添加json数据格式的点
/** * 显示地图点. * json的格式[{"name":"name1","x":"x1","y" ...
- R----stringr包介绍学习
1. stringr介绍 stringr包被定义为一致的.简单易用的字符串工具集.所有的函数和参数定义都具有一致性,比如,用相同的方法进行NA处理和0长度的向量处理. 字符串处理虽然不是R语言中最主要 ...
- Android 坐标系统
屏幕的左上角是坐标系统原点(0,0),原点向右延伸是X轴正方向,原点向下延伸是Y轴正方向. 一.View的坐标 需要注意view的坐标是相对父容器而言的,包括:getTop().getBott ...
- jquery总结05-常用事件02-表单事件
表单事件 .focus()元素获得焦点时 阻止冒泡 子元素不可以 .blur() 元素失去焦点时 阻止冒泡 子元素不可以 .change() input.select.textarea值发生改变时 i ...
- Hibernate <一级缓存>
Hibernate缓存分为三级: 一级缓存:基于事务级别(内存)的缓存,也可以成为session级别缓存 二级缓存:依赖于第三方,当请求一个对象时,先在缓存里面查找,如果没有就执行查询语句 查询缓存: ...