package com.swb.gamedemo.view;
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BallView extends SurfaceView implements SurfaceHolder.Callback{
public static final int V_MAX=35;
public static final int V_MIN=15;
public static final int WOOD_EDGE=60;
public static final int GROUND_LING=450;
public static final int UP_ZERO=30;
public static final int DOWN_ZERO=60;
Bitmap[]bitmapArray=new Bitmap[6];
Bitmap bmpBack;
Bitmap bmpWood;
String fps="FPS:N/A";
int ballNumber=8;
ArrayList<Movable> alMovable=new ArrayList<Movable>();
DrawThread dt;
public BallView(Context context) {
super(context);
getHolder().addCallback(this);
initBitmaps(getResources());
initMovables();
dt=new DrawThread(this, getHolder());
// TODO Auto-generated constructor stub
}
private void initBitmaps(Resources resources) {
// TODO Auto-generated method stub
bitmapArray[0]=BitmapFactory.decodeResource(resources,com.swb.gamedemo.R.drawable.redball);
bitmapArray[1]=BitmapFactory.decodeResource(resources,com.swb.gamedemo.R.drawable.redball);
bitmapArray[2]=BitmapFactory.decodeResource(resources,com.swb.gamedemo.R.drawable.redball);
bitmapArray[3]=BitmapFactory.decodeResource(resources,com.swb.gamedemo.R.drawable.redball);
bitmapArray[4]=BitmapFactory.decodeResource(resources,com.swb.gamedemo.R.drawable.redball);
bitmapArray[5]=BitmapFactory.decodeResource(resources,com.swb.gamedemo.R.drawable.redball);
bmpBack=BitmapFactory.decodeResource(resources, com.swb.gamedemo.R.drawable.bg);
bmpWood=BitmapFactory.decodeResource(resources, com.swb.gamedemo.R.drawable.p);
}
private void initMovables() {
// TODO Auto-generated method stub
Random r=new Random();
for(int i=0;i<ballNumber;i++){
int index=r.nextInt(32);
Bitmap tempBitmap=null;
if(i<ballNumber/2){
tempBitmap=bitmapArray[3+index%3];
}else{
tempBitmap=bitmapArray[index%3];
}
Movable m=new Movable(0, 70-tempBitmap.getHeight(), tempBitmap.getWidth()/2,tempBitmap);
alMovable.add(m);
}
}
public void doDraw(Canvas canvas){
canvas.drawBitmap(bmpBack, 0, 0, null);
canvas.drawBitmap(bmpWood, 0,60, null);
for( Movable m:alMovable){
m.drawSelf(canvas);
}
Paint p=new Paint();
p.setColor(Color.BLUE);
p.setTextSize(18);
p.setAntiAlias(true);
canvas.drawText(fps, 30, 30, p);
}
public BallView(Context context,AttributeSet attrs){
super(context,attrs);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
if(!dt.isAlive()){
dt.start();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
dt.flag=false;
dt=null;
}
}
- 1
- 2
- 3
前往页