Android一个小球弹跳的例子,希望对大家有用
来源:互联网 发布:淘宝抢购前100名怎么抢 编辑:程序博客网 时间:2024/04/29 13:14
1.小球类
package com.android.ballmove;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
public class Ball {
/**
18. * 球的高
19. */
public static final int HEIGHT = 93;
/**
22. * 球的宽
23. */
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();
}
/**
56. * 运动
57. */
private void move() {
if(maxHeight >= (sa.screenHeight - HEIGHT)) {
return;
}
if(upDirection){//向上
runY = runY + STEPLENGTH ;
}else{
runY = runY - STEPLENGTH ;
}
}
/**
70. * 边界检测,使球不会飞出边界
71. */
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 ;
}
}
}
2.小球弹跳的过程
package com.android.ballmove;
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);
}
@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.GRAY);//设置背景白色
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{
@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();
}
}
}
}
}
3.Activity
package com.android.ballmove;
import android.app.Activity;
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);
}
}
- Android一个小球弹跳的例子,希望对大家有用
- Android一个小球弹跳的例子,希望对大家有用
- 指针-----希望对大家有用
- 今天给大家分享一个购物车的例子,希望对大家有所帮助
- 【Android】弹跳的小球——SurfaceView
- Android(安卓)面试中常见的一些面试题和答案,希望对大家有用
- canvas例子一弹跳小球
- sql的一些基础,希望对大家有用!
- template的用法(转载)---希望对大家有用
- 总结一下SHFileOperation的用法,希望对大家有用
- 关于objective-c的内存管理(希望对大家有用)
- 触发器,网上搜到的 希望对大家有用
- 自己总结的一套JavaScript,希望对大家有用
- 弹跳的小球
- 杀毒软件下载,希望对大家有用
- RTSP协议中文版,希望对大家有用!
- 可能对大家有用的工具一个
- 可能对大家有用的工具一个
- 地底爬虫人访谈
- 这里的break语句以前我没用到过。
- 测试项目三大核心管控方向
- hadoop+hbase 伪分布式安装配置
- 创建进程时指定其父进程
- Android一个小球弹跳的例子,希望对大家有用
- jdk与jre的区别
- Android中进入下一个Activity时在上一个Activity主动调用onDestroy()方法不会真正将上一个Activity从栈中删除
- Android 数据库创建字段时的数据类型
- 关于我自己
- 无线上网的方式及速率总结
- 船长的航海日志--2011年10月21日 航向
- 一些收益的帖子
- 美国民主扩张后的伊拉克