这是项目的开始界面,由于没有背景图片,所以....
我们用到了剪切图动画,他的原理是先定义一块矩形区域,然后不断改变图片的位置来实现的。
注意:刚进入时执行顺序是:StartView构造方法——>surfaceCreated——>surfaceChanged——>surfaceDestroyed,楼主曾把线程的开启操作t = new Thread(this)放到
了构造方法中,运行项目的时候按Home键退出后再进入游戏会导致黑屏,为什么会这样呢?
这是因为按Home键退出后再进入游戏方法的执行顺序是:surfaceCreated——>surfaceChanged——>surfaceDestroyed,他不会运行构造方法,,而产生一个异常,小伙伴
们注意啰。
最后要注意的是提交画布的时候也可能产生异常,所以我们要if (canvas != null)进行判断后再进行提交操作,还有screen_width = this.getWidth()和screen_height =
this.getHeight()这两行代码不要写在构造方法中,因为只有进入到surfaceCreated方法中才能得到屏幕的宽和高,大家养成良好的代码编写习惯哦。
package com.example.qgns;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
public class StartView extends BasicView {
private Bitmap logo;//logo图片
private Bitmap fly;//动画图片
private Bitmap button;//按钮图片
private float logo_x; //他们的x,y坐标,大家随便设置
private float logo_y;
private float fly_x;
private float fly_y;
private float button_x;
private float button_y;
private MainActivity mainActivity;
private SoundPlay sound;//音效
private String name = "作者:情过南山";//绘制的文字
public StartView(Context context) {
super(context);
this.mainActivity = (MainActivity) context;
sound=new SoundPlay(mainActivity);
sound.initSound();//初始化音效
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {//点击按钮进入到游戏主界面
if (event.getAction() == MotionEvent.ACTION_DOWN
&& event.getPointerCount() == 1) {
float x = event.getX();
float y = event.getY();
if (x > button_x && x < button_x + button.getWidth()//按钮的一个矩形区域
&& y > button_y && y < button_y + button.getHeight()) {
sound.play(1, 0);
mainActivity.toMyView();
}
}
return false;
}
@Override
public void initBitmap() {//图片的初始化操作
logo = BitmapFactory.decodeResource(getResources(),
R.drawable.shoot_copyright);
fly = BitmapFactory.decodeResource(getResources(), R.drawable.fly);
button = BitmapFactory.decodeResource(getResources(),
R.drawable.game_start);
logo_x = screen_width / 2 - logo.getWidth() / 2;
logo_y = screen_height / 5;
fly_x = screen_width / 2 - fly.getWidth() / 2;
fly_y = logo_y + logo.getHeight() + fly.getWidth() / 3;
button_x = screen_width / 2 - button.getWidth() / 2;
button_y = screen_height - 3 * button.getHeight();
}
@Override
public void myDraw() {
try {
canvas = sur.lockCanvas();//锁定画布
if (canvas != null) {
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(logo, logo_x, logo_y, paint);//绘制logo
canvas.drawBitmap(button, button_x, button_y, paint);//绘制按钮
canvas.save();//剪切图动画
canvas.clipRect(fly_x, fly_y, fly_x + fly.getWidth(), fly_y
+ fly.getHeight() / 3);
canvas.drawBitmap(fly, fly_x,
fly_y - currentFrome * fly.getHeight() / 3, paint);
currentFrome++;//不断的改变帧
if (currentFrome >= 3) {
currentFrome = 0;
}
canvas.restore();
paint.setTextSize(30);//画笔大小
paint.setAntiAlias(true);//抗锯齿
float textLong = paint.measureText(name, 0, name.length());//标签长度
canvas.drawText(name, screen_width / 2 - textLong / 2, 60,//绘制标签
paint);
}
} catch (Exception e) {
} finally {
if (canvas != null) {
sur.unlockCanvasAndPost(canvas);//解锁画布
}
}
}
@Override
public void run() {
while (threadFlag) {//通过threadFlag标志位的改变控制线程的开始与暂停
long startTime = System.currentTimeMillis();//得到当前时间
myDraw();
long endTime = System.currentTimeMillis();
try {
if ((endTime - startTime) < 500) {
Thread.sleep(500 - (endTime - startTime));//保证每次的刷新时间相同
}
} catch (Exception e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
screen_width = this.getWidth();//屏宽
screen_height = this.getHeight();//屏高
initBitmap();
if (!threadFlag) {
threadFlag = true;//改变标志位
t = new Thread(this);//开启线程
t.start();//开始线程的绘制
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
release();
threadFlag = false;//视图销毁改变标志位
}
@Override
public void release() {//垃圾回收操作
if (!logo.isRecycled()) {
logo.recycle();
}
if (!fly.isRecycled()) {
fly.recycle();
}
if (!button.isRecycled()) {
button.recycle();
}
}
}