大家好,目前我写一个小程序,想实现用一个小游戏框架,其中,在屏幕上方放置一个SurfaceView用于绘制图形,屏幕下方放置一个按钮控制图形。但是这个小程序一直运行不正常,请高手们帮忙看一看,多谢!主程序如下:
】
public class DrawTest extends Activity {
private GameView mGameView = null;
private Button startButton = null;
private TextView resultText = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.mGameView = new GameView(this);
startButton = (Button)findViewById(R.id.startButton);
startButton.setText("开始绘制");
startButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
resultText.setText("开始绘制");
mGameView.runPaint(); }
});
resultText = (TextView)findViewById(R.id.resultText);
resultText.setText("结果");
}
}
[/code
SurfaceView程序如下:
[code=Java]
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {int y = 20;
private SurfaceHolder surfaceHolder;
Canvas c;
Paint mPaint;
public GameView(Context context) {
super(context);
mPaint = new Paint();
surfaceHolder = this.getHolder();//获取holder
surfaceHolder.addCallback(this);
this.setFocusable(true);
}
//@Override
protected void paint(Canvas canvas) { mPaint.setColor(Color.RED);
canvas.drawRect((320-80)/2, y, (320-80)/2+80, y+40, mPaint);
}
public void repaint() {
try
{
c = surfaceHolder.lockCanvas();
if (c != null)
paint(c);
}catch (Exception e){
}
finally
{
if (c != null)
{
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
public void runPaint()
{
new Thread(this).start();
}
@Override
public void run() {
// TODO Auto-generated method stub
repaint();
}
}其main.xml文件是这样的:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<com.szj.draw.GameView
android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
>
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</LinearLayout>
</LinearLayout>请大家帮帮忙,感激不尽
】
public class DrawTest extends Activity {
private GameView mGameView = null;
private Button startButton = null;
private TextView resultText = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.mGameView = new GameView(this);
startButton = (Button)findViewById(R.id.startButton);
startButton.setText("开始绘制");
startButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
resultText.setText("开始绘制");
mGameView.runPaint(); }
});
resultText = (TextView)findViewById(R.id.resultText);
resultText.setText("结果");
}
}
[/code
SurfaceView程序如下:
[code=Java]
public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {int y = 20;
private SurfaceHolder surfaceHolder;
Canvas c;
Paint mPaint;
public GameView(Context context) {
super(context);
mPaint = new Paint();
surfaceHolder = this.getHolder();//获取holder
surfaceHolder.addCallback(this);
this.setFocusable(true);
}
//@Override
protected void paint(Canvas canvas) { mPaint.setColor(Color.RED);
canvas.drawRect((320-80)/2, y, (320-80)/2+80, y+40, mPaint);
}
public void repaint() {
try
{
c = surfaceHolder.lockCanvas();
if (c != null)
paint(c);
}catch (Exception e){
}
finally
{
if (c != null)
{
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
public void runPaint()
{
new Thread(this).start();
}
@Override
public void run() {
// TODO Auto-generated method stub
repaint();
}
}其main.xml文件是这样的:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<com.szj.draw.GameView
android:id="@+id/gameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="4"
>
<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</LinearLayout>
</LinearLayout>请大家帮帮忙,感激不尽
得到的canvas总是为NULL,不知道为什么?
其实这个小程序很简单,在教科书里讲SurfaceView 的地方也有类似的说明,
只不过我多加了一个按钮,并和SurfaceView一起在main.xml里布局而已
应该要这样才行,所以你那些按钮textview什么的,都不能要了,的确需要的话,需要自己绘制。
我不知道GameView有没有draw啊,如果是View类的话,它显示的时候会自动调用onDraw()函数去显示,但是我使用View的时候,按钮触发绘图总是有问题,一般是第一次可以成功,第二次就不行了。
所以我改用SurfaceView,这个类是没有onDraw()函数的,我也不知道它到底正确显示了没有。
另外以前我用View就可以和按钮一起布局,但是刷新总是有问题,无论是否用线程都一样。
谢谢你的指点,如果你还有什么关于View更多信息,请多指教。就先结贴吧