laosanwansyu 2016-03-13 14:44 采纳率: 0%
浏览 1747

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条回答 默认 最新

  • 找工作那些事 2016-03-14 00:13
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮