2 qi15637293252 qi15637293252 于 2013.06.26 15:01 提问

所做Android打砖块代码其中 出错 求教

下面是代码 可其中出现了一个错误(做了标记),不知道怎么解决,可否有高手帮我看看,能不能稍作修改一下,做成正确的。
就是这句错了 myview = new BallView(this);

package sen.tan;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class dazhuankuaiActivity extends Activity {
    BallView myview;
    public static  int screenWidth ;
    public static  int screenHeight;
    private TextView textview;
    Button kaishi,jieshu,guanyu;
    ImageView image;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String string = "欢迎进入疯狂打砖块游戏";
        image = (ImageView) this.findViewById(R.id.image);
        image.setImageResource(R.drawable.img1);
        textview =(TextView) this.findViewById(R.id.textview);
        textview.setTextSize(20);
        textview.setTextColor(Color.GREEN);
        textview.setBackgroundColor(Color.RED);
        textview.setText(string);
        kaishi=(Button)findViewById(R.id.button1);
        guanyu=(Button)findViewById(R.id.button2);
        jieshu=(Button)findViewById(R.id.button3);
        kaishi.setWidth(250);
        guanyu.setWidth(200);
        jieshu.setWidth(150);
        kaishi.setTextSize(35);
        guanyu.setTextSize(29);
        jieshu.setTextSize(24);


        jieshu.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v)
            {
                dazhuankuaiActivity.this.finish();
            }


        });
        guanyu.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                textview.setText("本游戏由xxx制作,此游戏控制左右按键,来回移动挡板,不让小球掉落,当小球把上方砖块全部打完时,游戏胜利");
            }
        });
        kaishi.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                screenWidth = dm.widthPixels;
                screenHeight = dm.heightPixels;             
                setTitle("宽"+screenWidth+"  高"+screenHeight);
                requestWindowFeature(Window.FEATURE_NO_TITLE); 
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
                    `myview = new BallView(this);`    //错误在这里 
                setContentView(myview);
            }

            public boolean onKeyDown(int keyCode, KeyEvent event)
            {

            switch (keyCode)
                {
        case KeyEvent.KEYCODE_MENU:
            myview.resetGame();
            break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
            myview.ball_isRun = !myview.ball_isRun;
               break;
            case KeyEvent.KEYCODE_DPAD_LEFT:

                    if(myview.ball_isRun){

                if(myview.board_left<=myview.board_x_move)

                    {

            myview.board_left=0;

                myview.board_right=myview.board_length;

                }else{

                myview.board_left-=myview.board_x_move;

                        myview.board_right-=myview.board_x_move;}
        }

                         break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
        if(myview.ball_isRun){

                if(screenWidth-myview.board_right<=myview.board_x_move )

        {
                            myview.board_left=screenWidth-myview.board_length;

                                            myview.board_right=screenWidth;

            }else{
                            myview.board_left+=myview.board_x_move;

                                                myview.board_right+=myview.board_x_move;

                    }
                    }

                    break;
                 case KeyEvent.KEYCODE_DPAD_UP:

                if(myview.ball_isRun){

                if( myview.board_alterable_top==myview.board_default_top)
        {

                myview.board_alterable_top-=myview.boardYadd;

                        myview.board_alterable_bottom-=myview.boardYadd;
                        }
                    }

                    break;  
                 case KeyEvent.KEYCODE_DPAD_DOWN:

            if(myview.ball_isRun){

            if( myview.board_alterable_top==myview.board_default_top-myview.boardYadd )

            {
                            myview.board_alterable_top=myview.board_default_top;

            myview.board_alterable_bottom=myview.board_alterable_top+myview.board_thickness;
                        }
                    }

            break;  

            case KeyEvent.KEYCODE_BACK:

            dazhuankuaiActivity.this.finish();

            break;  

        }//end switch
            return false;
            }

            //按键弹起事件

        }
        );
        }
}
package sen.tan;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class BallView extends SurfaceView implements
        SurfaceHolder.Callback, Runnable {

    //线程延时控制 
    final int ball_sleep=1;//毫秒,延时越大,球速越慢
    final int ball_r= 8;//小球半径
    final float ball2_r= 8;//底下滚珠小球半径
    final int ballXorYadd = 4;//小球的基本位移。测试可行值:2,4

    //获取屏幕宽度和高度
    int screen_width;//320;
    int screen_height;//480;

    //砖的属性
    int brick_width;//每块砖宽
    int brick_height;//每块砖高
    boolean brick_exist[];//砖是否存在
    int k ;// 列//到for循环里才初始化
    int j ;// 行
    int brick_left = brick_width*(k-1);//到判断语句才初始化
    int brick_right = brick_width*k;
    int brick_top = brick_height*j;
    int brick_bottom = brick_height*(j+1);

    //挡板的属性
    int board_length;//挡板长度:80比较合适,可以随意修改,但别超过screen_width
    final int boardYadd = 16;//按上下键时挡板y方向位移量。经验证4、8、16可以,12不行
    final int board_x_move = 30;//挡板x方向位移量:可以随意自定义
    int board_left;//挡板左侧(可变)
    int board_right;//挡板右侧(可变)
    int board_thickness;//挡板厚度
    int board_default_top;//即435,挡板的top面初始位置
    int board_alterable_top;//挡板上侧(可变)
    int board_alterable_bottom;//挡板下侧(可变)

    int ball_default_x;//球的初始x坐标
    int ball_default_y;//球的初始y坐标
    //球的即时坐标(可变):
    int ball_x;//球心横坐标
    int ball_y;//球心纵坐标
    //球的前一步的y坐标
    int ball_previous_y;
    int ball_x_speed;//球的横向偏移量//可变
    int ball_y_speed;//球的纵向偏移量//可变
    boolean ball_isRun;//球是否在动
    // 控制循环
    boolean mbLoop;
    // 定义SurfaceHolder对象
    SurfaceHolder mSurfaceHolder = null;
    //获得分数
    int score;
    /* 唤醒渐变渲染 */
    Shader mRadialGradient = null;


//------------------------------------------------------------------------------------------------------//
    public BallView(Context context) {
        super(context);
        // 实例化SurfaceHolder
        mSurfaceHolder = this.getHolder();
        // 添加回调
        mSurfaceHolder.addCallback(this);
        this.setFocusable(true);

        //获取屏幕宽度和高度
        screen_width = dazhuankuaiActivity.screenWidth;//320
        screen_height = dazhuankuaiActivity.screenHeight;//480

        //砖的属性
        brick_width = screen_width/5;//每块砖宽64
        brick_height = screen_height/15;//每块砖高32

        //挡板的属性
        board_length = screen_width/4;//挡板长度:80比较合适,可以随意修改,但别超过screen_width
        board_left = (screen_width-board_length)/2;//挡板左侧(可变)
        board_right = (screen_width+board_length)/2;//挡板右侧(可变)
        board_thickness = 5;//挡板厚度
        board_default_top = 13*screen_height/15;//即435,挡板的top面初始位置
        board_alterable_top = board_default_top;//挡板上侧(可变)
        board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧(可变)

        ball_default_x = screen_width/2;//球的初始x坐标
        ball_default_y = board_default_top - ball_r;//球的初始y坐标
        //球的即时坐标(可变):
        ball_x = ball_default_x;
        ball_y = ball_default_y;
        //球的前一步的y坐标
        ball_previous_y = 0;
        ball_x_speed = ballXorYadd;//球的横向偏移量
        ball_y_speed = ballXorYadd;//球的纵向偏移量

        mbLoop = true;
        ball_isRun = false;
        score=0;

        brick_exist = new boolean[25];
        for (int i = 0; i < 25; i++) {
            brick_exist[i] = true;
        }
        /* 构建RadialGradient对象,设置半径的属性 */
        mRadialGradient = new RadialGradient(ball_x, ball_y, ball_r,//球中心坐标x,y,半径r
                new int[]{Color.WHITE,Color.BLUE,Color.GREEN,Color.RED,Color.YELLOW},//颜色数组
                null,//颜色数组中每一种颜色对应的相对位置,为空的话就是平均分布,由中心向外排布
                Shader.TileMode.REPEAT);//渲染模式:重复
    }

    public void resetGame(){
        ball_isRun = false;
        score =0;//分数
        ball_x_speed = ballXorYadd;//球的横向偏移量
        ball_y_speed = ballXorYadd;//球的纵向偏移量
        ball_x = screen_width/2;//球心起始横坐标
        ball_y = board_default_top - ball_r;//球心起始纵坐标
        board_left = (screen_width-board_length)/2;//挡板左侧
        board_right = (screen_width+board_length)/2;//挡板右侧
        board_alterable_top = board_default_top;//挡板上侧
        board_alterable_bottom = board_alterable_top+board_thickness;//挡板下侧
        for (int i = 0; i < 25; i++) {
            brick_exist[i] = true;
        }
    }

    //---------------------------------绘图循环开始----------------------------------
    public void run() {

        while (mbLoop&&!Thread.currentThread().isInterrupted()) {
            try {
                Thread.sleep(ball_sleep);
            }
            catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            //球的前一步y坐标
            ball_previous_y = ball_y;

            if (ball_isRun) {
                ballRunning();// 让小球移动
                boardPositionCheck();//检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线”
                hitWallCheck();//墙壁碰撞检测
                hitBoardCheck();//挡板碰撞检测
                hitBrickCheck();//砖块碰撞检测
            }
            synchronized (mSurfaceHolder) 
            {
                Draw();
            }
        }
    }
    //---------------------------------绘图循环结束----------------------------------
    //------------------------------------------------------------------------------

    //让小球移动
    public void ballRunning() {
        ball_x += ball_x_speed;
        ball_y -= ball_y_speed;
    }
    //朝左或朝右碰撞后小球水平方向逆向
    public void ballLeftOrRightHit() {
        ball_x_speed *= -1;
    }
    //朝上或朝下碰撞后小球竖直方向逆向
    public void ballUpOrDownHit() {
        ball_y_speed *= -1;
    }

    public void ballcornerHit() {
        ball_x_speed *= -1;
        ball_y_speed *= -1;
    }

    public void ballStraightUp() {//功能没实现,这是多余代码
        ball_x_speed = 0;//注意在其他地方恢复
        ball_y_speed *= -1;
    }

    //-------------------墙壁碰撞检测开始-------------------------------------
    public void hitWallCheck() {
        // 左碰墙
        if (ball_x <= ball_r && 
            ball_y >= ball_r && ball_y <= screen_height) 
        {
            ballLeftOrRightHit();
        // 右碰墙
        }
        if (ball_x >= screen_width - ball_r && //不能写为 else if,因为可能恰好碰到角落。
                   ball_y >= ball_r && ball_y <= screen_height) 
        {
            ballLeftOrRightHit();
        }
        // 上碰墙
        if (ball_x >= ball_r && ball_x <= screen_width - ball_r && 
                 ball_y <= ball_r + brick_height) //、、、、、、、、、、、、、、、25号修改
        {
            ballUpOrDownHit();
        }
        // 下碰墙
        if (ball_x >= ball_r && ball_x <= screen_width - ball_r && 
                 ball_y >= screen_height-ball_r) 
        {
            ballUpOrDownHit();
            if(score<=10){
                score = 0;
            }
            else score-=10;
        }
    }
    //-------------------墙壁碰撞检测结束-------------------------------------

    //-----------------------------挡板碰撞检测开始---------------------------
    public void hitBoardCheck() {
        // 下碰挡板正面
        if (ball_x >= ball_r && ball_x <= screen_width - ball_r && //在屏幕内,起码条件
            ball_x >= board_left && ball_x <= board_right && //在挡板X域上方
            ball_y == board_alterable_top - ball_r &&//球面与挡板相切
            ball_previous_y <= board_alterable_top - ball_r  //确定球是从上方下落
            )
        {
            if(board_alterable_top==board_default_top-boardYadd ){//如果弹簧伸张,挡板位于上线
                ballHitBoardlower();//作用:ball_y_move减小;挡板被打下;小球Y向运动反向
            }
            else if(board_alterable_top==board_default_top){//如果弹簧压缩,挡板位于下线
                boardHitBallHigher();//作用:ball_y_move增加;挡板弹上;小球Y向运动反向
            }
        }
        //斜碰挡板右上角//经验证有效
        else if(Math.pow(board_right-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&&
                ball_x>board_right && ball_y<board_alterable_top)
        {
            ballcornerHit();
        }
        //斜碰挡板的左上角//经验证有效
        else if(Math.pow(board_left-ball_x, 2)+Math.pow(board_alterable_top-ball_y,2)<=Math.pow(ball_r, 2)&&
                ball_x<board_left && ball_y<board_alterable_top)
        {
            ballcornerHit();
        }
    }
    //-----------------------------挡板碰撞检测结束--------------------------

    private void boardHitBallHigher() {//增强
        ballUpOrDownHit();//小球Y方向反向,ball_y_speed变为正数
        if(ball_y_speed == ballXorYadd ){
            ball_y_speed += ballXorYadd;//离开挡板后小球Y方向速度增强
        }
        if( boardYadd > ball_y_speed ){//在线程这一轮,小球上升多少,挡板就上升多少。
            board_alterable_top = board_default_top - ball_y_speed;
            board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面
        }
    }

    //检测挡板是否处于“中线”位置,是就随小球上升一步,直至到“上线”
    private void boardPositionCheck() {//还可直接利用球的位置刷新,board_top与球心相差ball_r
        if(board_alterable_top < board_default_top && board_alterable_top > board_default_top-boardYadd){
            //挡板随球上升
            if(board_alterable_top - ball_y_speed >= board_default_top-boardYadd){
                board_alterable_top -= ball_y_speed;//挡板上层面
                board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面
            }
            else{
                board_alterable_top = board_default_top-boardYadd;//挡板上层面
                board_alterable_bottom= board_alterable_top+board_thickness;//挡板下层面
            }
        }
    }

    private void ballHitBoardlower() {//减弱
        board_alterable_top=board_default_top;
        board_alterable_bottom=board_default_top+board_thickness;//挡板被打退
        ballUpOrDownHit();//小球Y方向反向
        if(ball_y_speed==2*ballXorYadd){
            ball_y_speed -= ballXorYadd;//小球Y方向速度减弱
        }
    }

    //砖块碰撞检测开始-----------------------------------------------------------
    public void hitBrickCheck() {
        for (int i = 0; i <25 ; i++) {
        if (brick_exist[i]) {
            k = i % 5+1;// 1,2,3,4,5循环
            j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5
            brick_left = brick_width*(k-1);
            brick_right = brick_width*k;
            brick_top = brick_height*j;
            brick_bottom = brick_height*(j+1);
            //朝下碰砖的top面 AAAAAAAAAAAAAAAAAAAAA
            if(ball_x >= brick_left && ball_x <= brick_right &&
               ball_y >= brick_top-ball_r && ball_y < brick_top) 
            {
                ballUpOrDownHit();
                brick_exist[i] = false;
                score+=4;
                //朝下正碰2砖中间,i砖右上角检测
                if(k!=5 && ball_x == brick_right)//如果不是第5列砖的右侧边
                {
                    //如果砖[i+1]存在
                    if(brick_exist[i+1]){
                        brick_exist[i+1] = false;
                        score+=4;
                    }
                }
                //朝下正碰2砖中间,i砖左上角检测
                else if(k!=1 && ball_x == brick_left)//如果不是第1列砖的左侧边
                {
                    //如果砖[i-1]存在
                    if(brick_exist[i-1]){
                        brick_exist[i-1] = false;
                        score+=4;
                    }
                }
            }
            //朝上碰砖的bottom面 BBBBBBBBBBBBBBBBB
            else if(ball_x >= brick_left && ball_x <= brick_right &&
                    ball_y > brick_bottom&& ball_y <= brick_bottom + ball_r )
            {
                ballUpOrDownHit();
                brick_exist[i] = false;
                score+=4;
                //朝上正碰2块砖中间--i砖的右下角检测
                if(k!=5 && ball_x == brick_right) //如果不是第5列砖的右侧边
                {
                    if(brick_exist[i+1]){//如果砖[i+1]存在
                        brick_exist[i+1] = false;
                        score+=4;
                    }
                }
                //朝上正碰2块砖中间--i砖的左下角检测
                else if(k!=1 && ball_x == brick_left) //如果不是第1列砖的左侧边
                {
                    if(brick_exist[i-1]){//如果砖[i-1]存在
                        brick_exist[i-1] = false;
                        score+=4;
                    }
                }
            }
            //朝右碰砖的left面CCCCCCCCCCCCCCCCC
            else if(ball_x >= brick_left -ball_r&&ball_x < brick_left&&
               ball_y >= brick_top && ball_y <= brick_bottom)
            {
                ballLeftOrRightHit();
                brick_exist[i] = false;
                score+=4;
                //朝右正碰2块砖中间,左下角检测
                if(j!=5 && ball_y == brick_bottom)//如果不是第5行砖的下侧边
                {
                    if(brick_exist[i+5]){//如果砖[i+5]存在
                        brick_exist[i+5] = false;
                        score+=4;
                    }
                }
                //朝右正碰2块砖中间,左上角检测
                else if(j!=1 && ball_y == brick_top)//如果不是第1行砖的上侧边
                {
                    if(brick_exist[i-5]){//如果砖[i-5]存在
                        brick_exist[i-5] = false;
                        score+=4;
                    }
                }
            }
            //朝左碰砖的right面DDDDDDDDDDDDDDDDDD
            else if(ball_x >= brick_right && ball_x <= brick_right+ball_r&&
               ball_y >= brick_top && ball_y <= brick_bottom)
            {
                ballLeftOrRightHit();
                brick_exist[i] = false;
                score+=4;
                //朝左正碰2块砖中间,右下角检测
                if(j!=5 && ball_y == brick_bottom )//如果不是第5行砖的下侧边
                {
                    if(brick_exist[i+5]){//如果砖[i+5]存在
                        brick_exist[i+5] = false;
                        score+=4;
                    }
                } 
                //朝左正碰2块砖中间,右上角检测
                else if(j!=1 && ball_y == brick_top )//如果不是第1行砖上侧边
                {
                    if(brick_exist[i-5]){//如果砖[i-5]存在
                        brick_exist[i-5] = false;
                        score+=4;
                    }
                }
            }
            /////////////////////////////////////////
            //斜碰i砖的左下角EEEEEEEEEEEEEEEEEEEEEEEE
            else if(( i-1<0||(i-1>=0&&!brick_exist[i-1]) ) &&
                    (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&&
                    Math.pow(brick_left-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&&
                    ball_x>brick_left-ball_r && ball_x<brick_left && 
                    ball_y>brick_bottom && ball_y<brick_bottom+ball_r )
            {
                ballcornerHit();
                brick_exist[i] = false;
                score+=4;
            }
            //斜碰i砖的右下角FFFFFFFFFFFFFFFFFFFFFFFFFF
            else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&&
                     (i+5>=25||(i+5<25&&!brick_exist[i+5]) )&&
                    Math.pow(brick_right-ball_x, 2)+Math.pow(brick_bottom-ball_y,2)<=Math.pow(ball_r, 2)&&
                    ball_x>brick_right&&ball_x<brick_right+ball_r&&
                    ball_y>brick_bottom&&ball_y<brick_bottom+ball_r )
            {
                Log.v("----------", "right bottom hit"+i+":"+brick_exist[i]);
                ballcornerHit(); 
                brick_exist[i] = false;
                score+=4;
            }
            //斜碰i砖的右上角GGGGGGGGGGGGGGGGGGGGGGGG
            else if( (i+1>=25||(i+1<25&&!brick_exist[i+1]) )&&
                    (i-5<0||(i-5>0&&!brick_exist[i-5]) )&&
                    Math.pow(brick_right-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&&
                    ball_x>brick_right && ball_x<brick_right+ball_r&&
                    ball_y>brick_top-ball_r && ball_y<brick_top)          
            {
                ballcornerHit();
                brick_exist[i] = false;
                score+=4;
            }
            //斜碰i砖的左上角HHHHHHHHHHHHHHHHHHHHHHHHHH
            else if((i-1<0||(i-1>=0&&!brick_exist[i-1])) &&
                    (i-5<0||(i-5>=0&&!brick_exist[i-5])) &&
                    Math.pow(brick_left-ball_x, 2)+Math.pow(brick_top-ball_y,2)<=Math.pow(ball_r, 2)&&
                    ball_x>brick_left-ball_x && ball_x<brick_left &&
                    ball_y>brick_top-ball_r && ball_y<brick_top )
            {
                ballcornerHit();
                brick_exist[i] = false;
                score+=4;
            }
        }//end if
        }//end for  
    }//end hitBrickCheck()
    //砖块碰撞检测结束-----------------------------------------------------------

    //------------------------------------------
    public boolean gameOver(){
        int count = 0;
        for(boolean s:brick_exist){
            if(!s){
                count++;
            }
        }
        if(count == 25){
            return true;
        }else{
            return false;
        }

    }
    //------------------------------------------

    //---------------绘图方法开始------------------------
    public void Draw() {
        // 锁定画布,得到canvas
        Canvas canvas = mSurfaceHolder.lockCanvas();
        if (mSurfaceHolder == null || canvas == null) {
            return;
        }
        // 绘图
        Paint mPaint = new Paint();
        // 设置取消锯齿效果
        mPaint.setAntiAlias(true);

        mPaint.setColor(Color.BLACK);
        // 绘制矩形--背景
        canvas.drawRect(0, 0, screen_width, brick_height-2, mPaint);

        mPaint.setColor(Color.GREEN);
        // 绘制矩形--背景
        canvas.drawRect(0, brick_height-2, screen_width, screen_height, mPaint);

        mPaint.setColor(Color.RED);//设置字体颜色
        mPaint.setTextSize(brick_height-7);//设置字体大小
        canvas.drawText("得分:"+score, 0, brick_height-7, mPaint);

        // 绘制顶层挡板````````````````````````````````````````````````````````25号修改
        mPaint.setColor(Color.BLACK);//设置颜色 
        mPaint.setStrokeWidth(4);//设置粗细
        canvas.drawLine(0, brick_height-2, screen_width, brick_height-2, mPaint);

        for (int i = 0; i <25 ; i++) {
            if (brick_exist[i]) {
                k = i % 5+1;// 1,2,3,4,5循环
                j = i / 5+1;// 1,1,1,1,1;2,2,2,2,2,;...;5,5,5,5,5
                brick_left = brick_width*(k-1);
                brick_right = brick_width*k;
                brick_top = brick_height*j;
                brick_bottom = brick_height*(j+1);

                mPaint.setStyle(Paint.Style.FILL);// 设置实心画笔
                mPaint.setColor(Color.YELLOW);
                canvas.drawRect(brick_left+1, brick_top+1, brick_right-1,brick_bottom-1, mPaint);

                mPaint.setStyle(Paint.Style.STROKE);// 设置空心画笔
                mPaint.setStrokeWidth(2);//设置粗细
                mPaint.setColor(Color.BLUE);
                canvas.drawRect(brick_left+3, brick_top+3, brick_right-3,brick_bottom-3, mPaint);
            }
        }
        // 设置实心画笔
        mPaint.setStyle(Paint.Style.FILL);
        {
            mPaint.setShader(mRadialGradient);
            canvas.drawCircle(ball_x, ball_y, ball_r, mPaint);
        }   

        Paint mPaint2 = new Paint();
            // 设置取消锯齿效果
        mPaint2.setAntiAlias(true); 
        // 设置实心画笔
        mPaint2.setStyle(Paint.Style.FILL);
        {   
        mPaint2.setColor(Color.BLACK);
        /* 绘制矩形挡板 */
        canvas.drawRect(board_left, board_alterable_top, board_right, board_alterable_bottom,mPaint2);
        float board2_bottom = screen_height - 2*ball2_r;//164
        float board2_top = board2_bottom - 4;//160
        //线段端点坐标数组
        float x0 = board_left+(board_right-board_left)/4;
        float y0 = board_alterable_bottom;//440或者444
        float springAdd = (board2_top- board_alterable_bottom)/8;//即2.5或者2:弹簧小线段的y向长度
        float springWidth = 5.0f;//弹簧小线段的x向长度
        float x1 = x0 - springWidth;
        float y1 = y0 + springAdd;
        float x2 = x0 + springWidth;
        float y2 = y0 + 3*springAdd;
        float x3 = x1 ;
        float y3 = y0 + 5*springAdd;
        float x4 = x2;
        float y4 = y0 + 7*springAdd;
        float x5 = x0 ;
        float y5 = board2_top;//即460 
        float between_spring = (board_right-board_left)/2;
        float pts[] = { x0,y0,x1,y1,
                        x1,y1,x2,y2,
                        x2,y2,x3,y3,
                        x3,y3,x4,y4,
                        x4,y4,x5,y5};
        float pts2[] = {x0+between_spring,y0, x1+between_spring,y1,
                        x1+between_spring,y1, x2+between_spring,y2,
                        x2+between_spring,y2, x3+between_spring,y3,
                        x3+between_spring,y3, x4+between_spring,y4,
                        x4+between_spring,y4, x5+between_spring,y5};
        mPaint2.setStrokeWidth(2);//设置弹簧粗细
        //绘制2个弹簧
        canvas.drawLines(pts, mPaint2);
        canvas.drawLines(pts2, mPaint2);
        //绘制下层挡板
        canvas.drawRect(board_left, board2_top, board_right, board2_bottom,mPaint2);
        mPaint2.setColor(Color.BLACK);//
        // 绘制最下面的两个“轮子”(圆心x,圆心y,半径r,p) 
        canvas.drawCircle(board_left+ball2_r, screen_height-ball2_r, ball2_r, mPaint2);//圆
        canvas.drawCircle(board_right-ball2_r, screen_height-ball2_r, ball2_r, mPaint2);
        mPaint2.setColor(Color.WHITE);//
        canvas.drawPoint(board_left+ball2_r, screen_height-ball2_r, mPaint2);//绘制左轮轮心
        canvas.drawPoint(board_right-ball2_r, screen_height-ball2_r, mPaint2);//绘制右轮轮心

        }//实心画笔mPaint2结束

        if(gameOver()){ 
//          mbLoop = false;//注释掉的话GAME OVER后也可重启
            ball_isRun = false;
            mPaint2.setColor(Color.BLACK);//设置字体颜色
            mPaint2.setTextSize(40.0f);//设置字体大小
            canvas.drawText("GAME OVER", screen_width/32+40, screen_height/16*9-70, mPaint2);
            mPaint2.setTextSize(20.0f);//设置字体大小
            canvas.drawText("Press \"MENU\" button to restart,", 
                            screen_width/32, //320/32=10
                            screen_height/16*9, //480/16=30,30*9=270
                            mPaint2);
            canvas.drawText("Press \"BACK\" button to exit.", 
                    screen_width/32, //320/32=10
                    screen_height/16*10, //480/16=30,30*10=300
                    mPaint2);
        }
        // 绘制后解锁,绘制后必须解锁才能显示
        mSurfaceHolder.unlockCanvasAndPost(canvas);
    }// end of Draw()
    //---------------------绘图方法结束-----------------------------------------------

    // 在surface的大小发生改变时激发
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
    }

    // 在surface创建时激发
    public void surfaceCreated(SurfaceHolder holder) {
        new Thread(this).start();// 开启绘图线程
    }

    // 在surface销毁时激发
    public void surfaceDestroyed(SurfaceHolder holder) {
        // 停止循环
        mbLoop = false;
    }
}



下个界面的代码是  
package ab.cc;

import android.app.Activity;

import android.os.Bundle;

import android.util.DisplayMetrics;

import android.view.KeyEvent;

import android.view.MotionEvent;

import android.view.Window;
import android.view.WindowManager;


public class BaomingActivity extends Activity
{

BallView myView;

static int  screenWidth;

static int screenHeight;

/** Called when the activity is first created. */
    @Override

    public void onCreate(Bundle savedInstanceState)
    {

    super.onCreate(savedInstanceState);

    /* 定义DisplayMetrics对象 */

DisplayMetrics dm = new DisplayMetrics();

    /* 取得窗口属性 */

getWindowManager().getDefaultDisplay().getMetrics(dm);

    /* 窗口的宽度 */

screenWidth = dm.widthPixels;

        /* 窗口的高度 */

screenHeight = dm.heightPixels;
//      
setTitle("宽"+screenWidth+"  高"+screenHeight);


        /* 设置为无标题栏 */

requestWindowFeature(Window.FEATURE_NO_TITLE); 

    /* 设置为全屏模式 */ 

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 



myView = new BallView(this);

       //设置显示GameSurfaceView视图

setContentView(myView);
    }//end of onCreate()


       //触笔事件

public boolean onTouchEvent(MotionEvent event)
    {

    return true;
    }


       //按键按下事件

 public boolean onKeyDown(int keyCode, KeyEvent event)
    {

    switch (keyCode)
        {

    //菜单按键-设为初始菜单

case KeyEvent.KEYCODE_MENU:

    myView.resetGame();
        //重新开始

    break;

    //中间按键

    case KeyEvent.KEYCODE_DPAD_CENTER:

    myView.ball_isRun = !myView.ball_isRun;//开始//暂停

        break;


//左方向键  

case KeyEvent.KEYCODE_DPAD_LEFT:

            if(myView.ball_isRun){

        if(myView.board_left<=myView.board_x_move)

            {

    myView.board_left=0;

        myView.board_right=myView.board_length;

        }else{

        myView.board_left-=myView.board_x_move;

                myView.board_right-=myView.board_x_move;}
}

                 break;


//右方向键

case KeyEvent.KEYCODE_DPAD_RIGHT:
if(myView.ball_isRun){

        if(screenWidth-myView.board_right<=myView.board_x_move )

{
                    myView.board_left=screenWidth-myView.board_length;

                                    myView.board_right=screenWidth;

    }else{
                    myView.board_left+=myView.board_x_move;

                                        myView.board_right+=myView.board_x_move;

            }
            }

            break;

            //上方向键

    case KeyEvent.KEYCODE_DPAD_UP:

        if(myView.ball_isRun){

        if( myView.board_alterable_top==myView.board_default_top)
{

        myView.board_alterable_top-=myView.boardYadd;

                myView.board_alterable_bottom-=myView.boardYadd;
                }
            }

            break;  
        //下方向键

       case KeyEvent.KEYCODE_DPAD_DOWN:

    if(myView.ball_isRun){

    if( myView.board_alterable_top==myView.board_default_top-myView.boardYadd )

    {
                    myView.board_alterable_top=myView.board_default_top;

    myView.board_alterable_bottom=myView.board_alterable_top+myView.board_thickness;
                }
            }

    break;  

//返回键
        case KeyEvent.KEYCODE_BACK:

    this.finish();

    break;  

}//end switch
    return false;
    }

    //按键弹起事件

public boolean onKeyUp(int keyCode, KeyEvent event)
    {

        return true;
    }


public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
    {

    return true;
    }
}

2个回答

cj6585256
cj6585256   2013.06.26 17:02
已采纳
kaishi.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v){
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            screenWidth = dm.widthPixels;
            screenHeight = dm.heightPixels;             
            setTitle("宽"+screenWidth+"  高"+screenHeight);
            requestWindowFeature(Window.FEATURE_NO_TITLE); 
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 
                `myview = new BallView(this);`    //错误在这里 
            setContentView(myview);
        }

这里面的这个`myview = new BallView(dazhuankuaiActivity.this);

cj6585256
cj6585256 你是不是代码copy的有问题,不然这个编译就应该出错的啊
4 年多之前 回复
cj6585256
cj6585256   2013.06.26 16:02

myview = new BallView(this);这个错了this代表的是一个listerner
应该使用dazhuankuaiActivity.this
因为BallView构造方法需要的是一个Context对象

cj6585256
cj6585256 myview = new BallView(dazhuankuaiActivity.this); 修改成这样就好了
4 年多之前 回复
qi15637293252
qi15637293252 麻烦给下修改好的语句 新手 看不太懂 与代码相关的页面代码已经贴到问题最后 谢谢
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!