android ondraw显示不了图形

chinachenjiang 2011-10-23 05:27:13
这是源代码
package chenjiang.android.Test_6;

import android.app.Activity;
import android.os.Bundle;

public class Test_6Activity extends Activity {
/** Called when the activity is first created. */
private GameView gameView=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gameView=new GameView(this);
setContentView(gameView);
}
}


package chenjiang.android.Test_6;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class GameView extends View implements Runnable {

private Paint paint1=null;
public GameView(Context context) {
super(context);
paint1=new Paint();
//开启线程
new Thread(this).start();
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
super.onDraw(canvas);
Rect rect=new Rect(50,10,50, 100);
canvas.clipRect(rect);
paint1.setAntiAlias(true);
paint1.setColor(Color.WHITE);
paint1.setStyle(Paint.Style.FILL);//设置为实心
canvas.save();
canvas.drawRect(rect, paint1);
canvas.restore();
}
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().isInterrupted();
}
//使用postInvalidate()可以直接更新界面
postInvalidate();
}

}
}


为什么显示不了图形啊,黑压压的一片,如果我把画布设置为白色,也就白了
...全文
443 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
chinachenjiang 2011-10-23
  • 打赏
  • 举报
回复
确实是左右两条变重合了,谢谢,但是我没有看到一条线,现在修改后就可以了
java3jk 2011-10-23
  • 打赏
  • 举报
回复
楼上正解

Rect rect = new Rect(int left, int top, int right, int bottom);

楼主左、右边X坐标重合了
zhaohuaxishiwzw 2011-10-23
  • 打赏
  • 举报
回复
Rect rect=new Rect(50,10,50, 100);

这矩形定义成一条线了吧?

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧