package wyf.ytl;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class WelcomeView extends SurfaceView implements SurfaceHolder.Callback {
PlaneActivity activity;//activity的引用
private TutorialThread thread;//更新視框的執行緒
private WelcomeViewThread welcomeThread;//繪製執行緒
int status = 1;//當前的狀態值
int k = 0;//狀態為2時用到的切換圖片
int alpha = 255;//透明度
SoundPool soundPool;//聲音
HashMap<Integer, Integer> soundPoolMap;
Bitmap welcomeborder;//背景外框圖片
Bitmap background;//背景圖片
Bitmap background2;//背景圖片2
Bitmap image1,image2,image3,image4,image5,image6,image7;//動畫視框
Bitmap startGame;//開始遊戲功能表
Bitmap help;//?明功能表
Bitmap openSound;//打開聲音功能表
Bitmap closeSound;//關閉聲音功能表
Bitmap exit;//退出功能表
int backgroundY = -200;//背景的座標
int background2Y = 40;
Paint paint;//用於改變透明度
Paint paint2;//正常繪製
public WelcomeView(PlaneActivity activity) {//建構式
super(activity);
this.activity = activity;//得到activity的引用
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
getHolder().addCallback(this);
this.thread = new TutorialThread(getHolder(), this);
this.welcomeThread = new WelcomeViewThread(this);
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
initSounds();//初始化聲音
initBitmap();//初始化圖片資源
playSound(1);
}
public void initSounds(){//初始化聲音的方法
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);//初始化SoundPool
soundPoolMap = new HashMap<Integer, Integer>();//初始化HashMap
soundPoolMap.put(1, soundPool.load(getContext(), R.raw.welcome1, 1));
}
public void playSound(int sound) {//播放聲音的方法
AudioManager mgr = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);//設定最大音量
float volume = streamVolumeCurrent/streamVolumeMax; //裝置的音量
soundPool.play(soundPoolMap.get(sound), volume, volume, 1, 0, 1f);//播放
}
public void initBitmap(){//初始化圖片資源的方法
paint = new Paint();
paint2 = new Paint();
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
welcomeborder = BitmapFactory.decodeResource(getResources(), R.drawable.welcomeborder);
background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
image1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
image2 = BitmapFactory.decodeResource(getResources(), R.drawable.image2);//初始化動畫視框
image3 = BitmapFactory.decodeResource(getResources(), R.drawable.image3);//初始化動畫視框
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
image4 = BitmapFactory.decodeResource(getResources(), R.drawable.image4);//初始化動畫視框
image5 = BitmapFactory.decodeResource(getResources(), R.drawable.image5);//初始化動畫視框
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
image6 = BitmapFactory.decodeResource(getResources(), R.drawable.image6);//初始化動畫視框
image7 = BitmapFactory.decodeResource(getResources(), R.drawable.image7);//初始化動畫視框
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
background2 = BitmapFactory.decodeResource(getResources(), R.drawable.background2);//初始化背景圖片
startGame = BitmapFactory.decodeResource(getResources(), R.drawable.startgame);//初始化開始遊戲
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
help = BitmapFactory.decodeResource(getResources(), R.drawable.help);//幫助
openSound = BitmapFactory.decodeResource(getResources(), R.drawable.opensound);//打開聲音
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
closeSound = BitmapFactory.decodeResource(getResources(), R.drawable.closesound);//關閉聲音
exit = BitmapFactory.decodeResource(getResources(), R.drawable.exit);//退出遊戲
if(activity.processView != null){//走載入介面進度
activity.processView.process += 10;
}
}
public void onDraw(Canvas canvas){//自己寫的繪製方法
//畫的內容是z軸的,後畫的會覆蓋前面畫的
canvas.drawColor(Color.WHITE);//背景色
if(this.status == 1 || this.status == 2){
canvas.drawBitmap(background, 0, backgroundY, paint);//繪製背景
if(k == 0){
canvas.drawBitmap(image1, 0, 48, paint);
}else if(k > 0 && k<=1){
canvas.drawBitmap(image2, 0, 48, paint);
}else if(k>1 && k<=2){
canvas.drawBitmap(image3, 0, 48, paint);
}else if(k>2 && k<=3){
canvas.drawBitmap(image4, 0, 48, paint);
}else if(k>3 && k<=4){
canvas.drawBitmap(image5, 0, 48, paint);
}else if(k>4 && k<=5){
canvas.drawBitmap(image6, 0, 48, paint);
}else if(k>5 && k<=6){
canvas.drawBitmap(image7, 0, 48, paint);
}else if(k>6 && k<=7){
canvas.drawBitmap(image4, 0, 48, paint);
}else if(k>7 && k<=8){
canvas.drawBitmap(image3, 0, 48, paint);
}else if(k>8 && k<=10){
canvas.drawBitmap(image2, 0, 48, paint);
}
canvas.drawRect(0, 0, 480, 48, paint);//繪製上下的黑框
canvas.drawRect(0, 270, 480, 320, paint);//繪製矩形
canvas.drawBitmap(welcomeborder, -14, 10, paint);//繪製外框
}
else if(status == 3){
paint.setAlpha(alpha);//設定透明度
canvas.drawBitmap(background2, 0, background2Y, paint);//繪製背景圖
canvas.drawRect(0, 0, 480, 48, paint2);//繪製上下的黑框
canvas.drawRect(0, 270, 480, 320, paint2);//繪製矩形框
}
else if(status == 4){//功能表狀態
canvas.drawBitmap(background2, 0, background2Y, paint);//繪製背景圖
canvas.drawBitmap(startGame, 10, 70, paint2);//繪製開始遊戲按鈕
canvas.drawBitmap(help, 390, 60, paint2);//繪製說明按鈕
canvas.drawBitmap(exit, 380, 230, paint2);//繪製退出按鈕
if(activity.isSound){
canvas.drawBitmap(closeSound, 10, 230, paint2);//繪製關閉聲音功能表
}else{
canvas.drawBitmap(openSound, 10, 230, paint2);//繪製打開聲音
}
canvas.drawRect(0, 0, 480, 48, paint2);//繪製上下的黑框
canvas.drawRect(0, 270, 480, 320, paint2);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
public void surfaceCreated(SurfaceHolder holder) {//新建時被呼叫
this.thread.setFlag(true);//設定迴圈標記位元
this.thread.start();//啟動繪製執行緒
this.welcomeThread.setFlag(true);//設定迴圈標記位元
this.welcomeThread.start(); //啟動動畫執行緒
}
public void surfaceDestroyed(SurfaceHolder holder) {//摧毀時被呼叫
boolean retry = true;//迴圈標記
thread.setFlag(false);
while (retry) {
try {
thread.join();//等待中的執行緒的結束
retry = false;//設定迴圈標記停止迴圈
}
catch (InterruptedException e) {}//不斷地迴圈,直到更新視框執行緒結束
}
}
public boolean onTouchEvent(MotionEvent event) {//螢幕監聽
if(event.getAction() == MotionEvent.ACTION_DOWN){//螢幕被按下
if(this.status != 4){//當不是功能表狀態時返回
return false;
}
double x = event.getX();//得到X座標
double y = event.getY();//得到Y座標
if(x>10 && x<10 + openSound.getWidth()
&& y>70 && y<70 + openSound.getHeight()){//點擊了開始有些按鈕
activity.myHandler.sendEmptyMessage(2);//發送訊息
}
else if(x>390 && x<390 + help.getWidth()
&& y>60 && y<60 + help.getHeight()){//點擊了說明按鈕
activity.myHandler.sendEmptyMessage(3);//發送訊息
}
else if(x>10 && x<10 + openSound.getWidth()
&& y>230 && y<230 + openSound.getHeight()){//點擊了聲音按鈕
activity.isSound = !activity.isSound;//將聲音旗標位置反
}
else if(x>380 && x<380 + exit.getWidth()
&& y>230 && y<230 + exit.getHeight()){//點擊了退出按鈕
System.exit(0);//退出遊戲
}
}
return super.onTouchEvent(event);//呼叫基類的方法
}
class TutorialThread extends Thread{//更新視框執行緒
private int span = 100;//睡眠的毫秒數
private SurfaceHolder surfaceHolder;
private WelcomeView welcomeView;//歡迎介面的引用
private boolean flag = false;
public TutorialThread(SurfaceHolder surfaceHolder, WelcomeView welcomeView) {//建構式
this.surfaceHolder = surfaceHolder;//SurfaceHolder的引用
this.welcomeView = welcomeView;//歡迎介面的引用
}
public void setFlag(boolean flag) {//設定標準位元
this.flag = flag;
}
public void run() {//覆寫的run方法
Canvas c;
while (this.flag) {//迴圈
c = null;
try {
// 鎖定整個畫布,在記憶體要求比較高的情況下,建議參數不要為null
c = this.surfaceHolder.lockCanvas(null);
synchronized (this.surfaceHolder) {//同步
welcomeView.onDraw(c);//呼叫繪製方法
}
} finally {//用finally保證一定被執行
if (c != null) {
//更新螢幕顯示內容
this.surfaceHolder.unlockCanvasAndPost(c);
}
}
try{
Thread.sleep(span);//睡眠指定毫秒數
}catch(Exception e){//捕獲異常
e.printStackTrace();//列印異常資訊
}
}
}
}
}