为什么这段代码执行会出现问题?各位熟悉位图的大侠 给看看

ccboy 发布于 2013/08/20 13:43
阅读 121
收藏 0

package mylove.select_firesofheaven;

import java.io.InputStream;
import java.util.Timer;
import java.util.TimerTask;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;

public class Main_Activity extends Activity {
    /** Called when the activity is first created. */
 
 //进入游戏主页UI        collection
 
 //定义一对int数值来装鼠标位置
 int eventdownX=0;
 int eventdownY=0;
    //设置当前界面
    int jiemian=1;
 int tableHeight;
 int tableWight;
 Thread thread;
 GameView gameview;
 Handler handler;
 boolean muenbegain=false;
 boolean muenmystar=false;
 boolean muenshowweb=false;
 boolean muenhelp=false;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
       
        //去掉窗口标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       
        //全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
          WindowManager.LayoutParams.FLAG_FULLSCREEN);
       
        //获取窗口管理器
        WindowManager windowManager=getWindowManager();
        Display display=windowManager.getDefaultDisplay();
        //获取屏幕的高和宽
        tableHeight=display.getHeight();
        tableWight=display.getWidth();
       
       
        //创建画板控件
        gameview=new GameView(this);
        setContentView(gameview);
       
       
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.game1);
        // 计算缩放率,新尺寸除原始尺寸 
        final float scaleWidth = ((float) tableWight ) / bitmap.getWidth();
        final float scaleHeight = ((float) tableHeight ) / bitmap.getHeight();
       
       
        //
       
        //设置一个手指滑动监听事件
        gameview.setOnTouchListener(new OnTouchListener() {
   
   @Override
   public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    return detector.onTouchEvent(event);
   }
  });
    }
   
    private GestureDetector detector = new GestureDetector(new OnGestureListener()
 {
  //手势识别函数,到此函数被系统回调时说明系统认为发生了手势事件,
        //我们可以做进一步判定。
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
  {
   if (e1.getX() - e2.getX() > 50) {
    if(jiemian==1)
    {
     jiemian=2;//设置提示界面的页面
    }
    if(jiemian==2) {
//     //跳转到指南界面
     startActivity(new Intent(Main_Activity.this,Direction_Activity.class));
     overridePendingTransition(R.anim.push_left_in,R.anim.push_left_out);
    }
    return true;
   }
   return false;
  }
    
  @Override
  public boolean onSingleTapUp(MotionEvent e) {    
   return false;
  }
  
  @Override
  public void onShowPress(MotionEvent e) {
   
  }
  
  @Override
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
   return false;
  }
  
  @Override
  public void onLongPress(MotionEvent e) {
   
  }
  
  @Override
  public boolean onDown(MotionEvent e) {
   return false;
  }
 });
   
    class GameView extends View
   {

     //改变效果
     boolean xiaoguo=true;

     
     int miao=0;//每过15s出现一个段字
     //定义星星第一次画的x,y坐标
  int x1=150;
        int y1=-50;
        float scaleWidth;
        float scaleHeight;
        Matrix matrix;
        //背景需要的位图
        Bitmap bitmapback,bitmapstar;
        //导航界面需要的位图
  Bitmap bitmapbottomback1,bitmapbottomback2,bitmapbottomback3
    ,text1,text2,text3,text4;
  //菜单选项界面需要的位图
     Bitmap bitmapbottom,bitmaptop1,bitmaptop2,bitmapcenter
     ,bitmapmuenbegain1,bitmapmuenbegain2,bitmapmuenmystar1,bitmapmuenmystar2
     ,bitmapmuenshowweb1,bitmapmuenshowweb2,bitmapmuenhelp1,bitmapmuenhelp2;
     
   public GameView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    setFocusable(true);

//    InputStream is1=this.getResources().openRawResource(R.drawable.game1);
//    InputStream is2=this.getResources().openRawResource(R.drawable.game1_2);
//    InputStream is3=this.getResources().openRawResource(R.drawable.game1_7);
//    InputStream is4=this.getResources().openRawResource(R.drawable.game1_9);
//    InputStream is5=this.getResources().openRawResource(R.drawable.game1_10);
//    InputStream is6=this.getResources().openRawResource(R.drawable.game1_3);
//    InputStream is7=this.getResources().openRawResource(R.drawable.game1_4);
//    InputStream is8=this.getResources().openRawResource(R.drawable.game1_5);
//    InputStream is9=this.getResources().openRawResource(R.drawable.game1_6);
//    InputStream is10=this.getResources().openRawResource(R.drawable.game2_1);
//    InputStream is11=this.getResources().openRawResource(R.drawable.game2_2);
//    InputStream is12=this.getResources().openRawResource(R.drawable.game2_7);
//    InputStream is13=this.getResources().openRawResource(R.drawable.game2_7_1);
//    InputStream is14=this.getResources().openRawResource(R.drawable.game2_3_1);
//    InputStream is15=this.getResources().openRawResource(R.drawable.game2_3);
//    InputStream is16=this.getResources().openRawResource(R.drawable.game2_4_1);
//    InputStream is17=this.getResources().openRawResource(R.drawable.game2_4);
//    InputStream is18=this.getResources().openRawResource(R.drawable.game2_5_1);
//    InputStream is19=this.getResources().openRawResource(R.drawable.game2_5);
//    InputStream is20=this.getResources().openRawResource(R.drawable.game2_6_1);
//    InputStream is21=this.getResources().openRawResource(R.drawable.game2_6);
//    BitmapFactory.Options options2=new BitmapFactory.Options();
//    options2.inJustDecodeBounds=false;
//    options2.inSampleSize=2;
//          bitmapback=BitmapFactory.decodeStream(is1,null,options2);
//    bitmapstar=BitmapFactory.decodeStream(is2,null,options2);
//    bitmapbottomback1=BitmapFactory.decodeStream(is3,null,options2);
//    bitmapbottomback2=BitmapFactory.decodeStream(is4,null,options2);
//    bitmapbottomback3=BitmapFactory.decodeStream(is5,null,options2);
//    text1=BitmapFactory.decodeStream(is6,null,options2);
//    text2=BitmapFactory.decodeStream(is7,null,options2);
//    text3=BitmapFactory.decodeStream(is8,null,options2);
//    text4=BitmapFactory.decodeStream(is9,null,options2);
//    bitmapbottom=BitmapFactory.decodeStream(is10,null,options2);
//    bitmapcenter=BitmapFactory.decodeStream(is11,null,options2);
//    bitmaptop1=BitmapFactory.decodeStream(is12,null,options2);
//    bitmaptop2=BitmapFactory.decodeStream(is13,null,options2);
//    bitmapmuenbegain2=BitmapFactory.decodeStream(is14,null,options2);
//    bitmapmuenbegain1=BitmapFactory.decodeStream(is15,null,options2);
//    bitmapmuenmystar2=BitmapFactory.decodeStream(is16,null,options2);
//    bitmapmuenmystar1=BitmapFactory.decodeStream(is17,null,options2);
//    bitmapmuenshowweb2=BitmapFactory.decodeStream(is18,null,options2);
//    bitmapmuenshowweb1=BitmapFactory.decodeStream(is19,null,options2);
//    bitmapmuenhelp2=BitmapFactory.decodeStream(is20,null,options2);
//    bitmapmuenhelp1=BitmapFactory.decodeStream(is21,null,options2);
    
    bitmapback=bitmap(bitmapback, R.drawable.game1);
    bitmapstar=bitmap(bitmapback, R.drawable.game1_2);
    bitmapbottomback1=bitmap(bitmapback, R.drawable.game1_7);
    bitmapbottomback2=bitmap(bitmapback, R.drawable.game1_9);
    bitmapbottomback3=bitmap(bitmapback, R.drawable.game1_10);
    text1=bitmap(bitmapback, R.drawable.game1_3);
    text2=bitmap(bitmapback, R.drawable.game1_4);
    text3=bitmap(bitmapback, R.drawable.game1_5);
    text4=bitmap(bitmapback, R.drawable.game1_6);
    bitmapbottom=bitmap(bitmapback, R.drawable.game2_1);
    bitmapcenter=bitmap(bitmapback, R.drawable.game2_2);
    bitmaptop1=bitmap(bitmapback, R.drawable.game2_7);
    bitmaptop2=bitmap(bitmapback, R.drawable.game2_7_1);
    bitmapmuenbegain2=bitmap(bitmapback, R.drawable.game2_3_1);
    bitmapmuenbegain1=bitmap(bitmapback, R.drawable.game2_3);
    bitmapmuenmystar2=bitmap(bitmapback, R.drawable.game2_4_1);
    bitmapmuenmystar1=bitmap(bitmapback, R.drawable.game2_4);
    bitmapmuenshowweb2=bitmap(bitmapback, R.drawable.game2_5_1);
    bitmapmuenshowweb1=bitmap(bitmapback, R.drawable.game2_5);
    bitmapmuenhelp2=bitmap(bitmapback, R.drawable.game2_6_1);
    bitmapmuenhelp1=bitmap(bitmapback, R.drawable.game2_6);

    
//    //让流星飞行
          handler=new Handler()
          {
           
     public void handleMessage(Message mes)
           {
//      //更换图片
            if (mes.what==0x123) {
             
             //每一次+1
             miao++;
             
             if (x1<=-bitmapback.getWidth()) {
        x1=bitmapback.getWidth()+80;
        y1=-190;
       }
             else
       {
        x1=x1-9;
        y1=y1+3;
       }
      }
            if (mes.what==0x111) {
             if (xiaoguo) {
        xiaoguo=false;
       }
       else {
        xiaoguo=true;
       }
      }
            //重绘
            gameview.invalidate();
           }
          };
          //创建一个timer对象来更换图片
          final Timer timer=new Timer();
          timer.schedule(new TimerTask() {
     
     @Override
     public void run() {
      // TODO Auto-generated method stub
      handler.sendEmptyMessage(0x123);
     }
    }, 0, 100);
          //创建一个timer对象来更换图片
          final Timer timer1=new Timer();
          timer1.schedule(new TimerTask() {
     
     @Override
     public void run() {
      // TODO Auto-generated method stub
      handler.sendEmptyMessage(0x111);
     }
    }, 0, 1000);
         
   }
  
   @Override
   protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    //创建一个画笔
    Paint paint=new Paint();
    
    //画出背景
    
          canvas.drawBitmap(bitmapback, 0, 0, paint);
    canvas.drawBitmap(bitmapstar, x1, y1, paint);//画星星
    
    
    //画导航界面
    if (jiemian==1) {
     canvas.drawBitmap(bitmapbottomback1, 0, 0, paint);
     if (xiaoguo) {
      canvas.drawBitmap(bitmapbottomback2, 0, 0, paint);
     }
     else {
      canvas.drawBitmap(bitmapbottomback3, 0, 0, paint);
     }
     if (miao>=5) {
      canvas.drawBitmap(text1, 0, 0, paint);
     }
     if (miao>=20) {
      canvas.drawBitmap(text2, 0, 0, paint);
     }
     if (miao>=35) {
      canvas.drawBitmap(text3, 0, 0, paint);
     }
     if (miao>=50) {
      canvas.drawBitmap(text4, 0, 0, paint);
     }
    }
    if (jiemian==2) {
     //画菜单界面
     canvas.drawBitmap(bitmapbottom, 0, 0, paint);
     canvas.drawBitmap(bitmapcenter, 0, 0, paint);
     //activity头部logo效果
     if (xiaoguo) {
      canvas.drawBitmap(bitmaptop1, 0, 0, paint);
     }
     else {
      canvas.drawBitmap(bitmaptop2, 0, 0, paint);
     }
     
     //根据手指点击的坐标,画出选项图片
     if (muenbegain) {
      canvas.drawBitmap(bitmapmuenbegain2, 0, 0, paint);
     }
     else {
      canvas.drawBitmap(bitmapmuenbegain1, 0, 0, paint);
     }
     
     if (muenmystar) {
      canvas.drawBitmap(bitmapmuenmystar2, 0, 0, paint);
     }
     else {
      canvas.drawBitmap(bitmapmuenmystar1, 0, 0, paint);
     }
     if (muenshowweb) {
      canvas.drawBitmap(bitmapmuenshowweb2, 0, 0, paint);
     }
     else {
      canvas.drawBitmap(bitmapmuenshowweb1, 0, 0, paint);
     }
     if (muenhelp) {
      canvas.drawBitmap(bitmapmuenhelp2, 0, 0, paint);
     }
     else {
      canvas.drawBitmap(bitmapmuenhelp1, 0, 0, paint);
     }
    }
    
   }
   
   
   //定义一个设置位图的方法
   public Bitmap bitmap(Bitmap bitmap,int r)
   {
//    InputStream is1=this.getResources().openRawResource(r);
//    BitmapFactory.Options options2=new BitmapFactory.Options();
//    options2.inJustDecodeBounds=false;
//    //设置位图的比例
//    options2.inSampleSize=2;
//    bitmap=BitmapFactory.decodeStream(is1,null,options2);
    
    bitmap = BitmapFactory.decodeResource(this.getResources(), r);
    // 计算缩放率,新尺寸除原始尺寸 
          float scaleWidth = ((float) tableWight ) / bitmap.getWidth();
          float scaleHeight = ((float) tableHeight ) / bitmap.getHeight();
          // 创建操作图片用的matrix对象 
          Matrix matrix = new Matrix();
          // 缩放图片动作        
          matrix.postScale(scaleWidth, scaleHeight);
          // 新得到的图片是原图片经过变换填充到整个屏幕的图片 
          Bitmap picNewRes = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, false);
         
    return picNewRes;
   }
   
   }
 

}

加载中
返回顶部
顶部