surface实例-小球弹起事例

时间:2023-03-09 16:53:41
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);
}
}