2 laosanwansyu laosanwansyu 于 2016.03.13 22:44 提问

android 如和在activity里更改自己写的view.java界面

我要做一个数独游戏 在GameActivity中我已经从assest中随机读取了一个数独,但是我不知道该如何把这些数字加到九宫格里

图片说明
public class GameActivity extends Activity{
private Button btn01;
private Button btn02;
private Button btn03;
private GameView gameView;
private Handler handler=new Handler(){
public void handleMessage(android.os.Message msag) {

    }
};


private String mQuestionFilename[] = {"easy", "medium", "hard"};

private int[] shudu =new int[9*9];

private enum QuestionLevel {
    easy(0), medium(1), hard(2);
    private final int value;
    public int getValue(){
        return value;
    }
    QuestionLevel(int value){
        this.value = value;
    }
}


private int getTile(int x,int y) {
    return shudu[y*9+x];
}


public String getTileString(int x,int y){
    int v=getTile(x, y);
    if (v==0) {
        return "";
    }
    else{
        return String.valueOf(v);
    }
}


private void NewPuzzle(QuestionLevel level){

    String filename = mQuestionFilename[level.getValue()];
    int row = 0;
    try{
        InputStream in = this.getResources().getAssets().open(filename);
        int count = in.available();
        byte[] buff =  new byte[count];
        in.read(buff);
        String []data = EncodingUtils.getString(buff, "UTF-8").split("\n");
        in.close();
        Random  rdm = new Random(); 
        row = Math.abs(rdm.nextInt()) % data.length; // row sometimes <0
        String temp = data[row];
        int k=0;
        for (int i=0; i<81; i++){
            String dt = temp.substring(k);
            shudu[i]= temp.charAt(k)-'0'; 
            k=k+2; 
        } 
    }catch(Exception e){
        e.printStackTrace();
    } 
} 

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    gameView=(GameView)findViewById(R.id.gameview);

    btn01=(Button)findViewById(R.id.button_1);



    btn01.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            final String[] cities = {"Easy", "Medium", "Hard"};
            Dialog dlg = new AlertDialog.Builder(GameActivity.this)
                .setTitle("请选择难度")
                .setItems(cities,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        switch (which){
                        case 0:
                            NewPuzzle(QuestionLevel.easy);

                            break;
                        case 1:
                            //NewPuzzle(QuestionLevel.medium);
                            break;
                        case 2:
                            //NewPuzzle(QuestionLevel.hard);
                            break;
                        default:
                            break;
                        }
                    }
                })  

                .create();
            dlg.show();
        }
    });
}

}

这个就是画九宫格 和 数字

package com.example.hzp;

public class GameView extends View{
private float width;
private float height;
private GameActivity gameActivity=new GameActivity();

public GameView(Context context, float width, float height) {
    super(context);
    this.width = width;
    this.height = height;
}


public GameView(Context context){
    super(context);
}





public GameView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}


public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub

}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    this.width=w/9f;
    this.height=(float) (h/9f*0.8);
    super.onSizeChanged(w, h, oldw, oldh);
}


@Override
protected void onDraw(Canvas canvas) {
    Paint bgpaint=new Paint();
    Rect rect=new Rect();
    rect.bottom=getHeight();
    rect.left=0;
    rect.right=getWidth();
    rect.top=0;
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bj01), null, rect, null);

    Paint darkPaint=new Paint();
    darkPaint.setColor(getResources().getColor(R.color.dark));

    Paint hillitePaint=new Paint();
    hillitePaint.setColor(getResources().getColor(R.color.hillite));

    Paint lightPaint=new Paint();
    lightPaint.setColor(getResources().getColor(R.color.light));

    for(int i=0;i<9;i++){
        canvas.drawLine(0, i*height, getWidth(), i*height, lightPaint);
        canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hillitePaint);
        canvas.drawLine(i*width, 0, i*width,height*9, lightPaint);
        canvas.drawLine(i*width+1, 0, i*width+1,height*9, hillitePaint);
    }

    for(int i=0;i<9;i++){
        if(i%3!=0){
            continue;
        }
        canvas.drawLine(0, i*height, getWidth(), i*height, darkPaint);
        canvas.drawLine(0, i*height+1, getWidth(), i*height+1, darkPaint);
        canvas.drawLine(0, i*height+2, getWidth(), i*height+2, darkPaint);
        canvas.drawLine(i*width, 0, i*width,height*9, darkPaint);
        canvas.drawLine(i*width+1, 0, i*width+1,height*9, darkPaint);
        canvas.drawLine(i*width+2, 0, i*width+2,height*9, darkPaint);

    }
    canvas.drawLine(0, 9*height, getWidth(), 9*height, darkPaint);
    canvas.drawLine(0, 9*height+1, getWidth(), 9*height+1, darkPaint);
    canvas.drawLine(9*width, 0, 9*width,height*9, darkPaint);
    canvas.drawLine(9*width+1, 0, 9*width+1,height*9, darkPaint);

    Paint numPaint=new Paint();
    numPaint.setColor(Color.BLACK);
    numPaint.setStyle(Paint.Style.STROKE);
    numPaint.setTextSize(height*0.75f);
    numPaint.setTextAlign(Paint.Align.CENTER);

    FontMetrics fM =numPaint.getFontMetrics();
    float x=width/2;
    float y=height/2-(fM.ascent+fM.descent)/2;

    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            canvas.drawText(gameActivity.getTileString(i, j), i*width+x, j*height+y, numPaint);
        }
    }

    super.onDraw(canvas);
}


public void setcontent(){
    Canvas canvas=new Canvas();
    Paint numPaint=new Paint();
    numPaint.setColor(Color.BLACK);
    numPaint.setStyle(Paint.Style.STROKE);
    numPaint.setTextSize(height*0.75f);
    numPaint.setTextAlign(Paint.Align.CENTER);

    FontMetrics fM =numPaint.getFontMetrics();
    float x=width/2;
    float y=height/2-(fM.ascent+fM.descent)/2;

    for(int i=0;i<9;i++){
        for(int j=0;j<9;j++){
            canvas.drawText(gameActivity.getTileString(i, j), i*width+x, j*height+y, numPaint);
        }
    }
}

}

1个回答

wen_wxpk
wen_wxpk   2016.03.14 08:13

gameView=(GameView)findViewById(R.id.gameview);缺少适配器
接着要写adapter,然后gameView.setAdater(adapter),要重写adapter

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!