使用Canvas时,背景会变黑

时间:2015-03-08 15:29:09

标签: android android-canvas

我正在尝试学习如何使用画布来创建简单的图形,但是现在它拒绝绘制任何内容,而只是创建一个黑色背景。

这是MainActivity:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.canvastesting.MainActivity" >

<com.example.canvastesting.DrawingPanel android:id="@+id/DrawingPanel01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:maxHeight="40dip">
</com.example.canvastesting.DrawingPanel>

</RelativeLayout>

DrawingPanel类:

public class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
PanelThread canvasthread;

public DrawingPanel(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);
    canvasthread = new PanelThread(getHolder(), this);
    setFocusable(true);
    // TODO Auto-generated constructor stub
}

@Override
public void onDraw(Canvas canvas){
    //Draw stuff here
    canvas.drawColor(Color.TRANSPARENT);
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    canvasthread.setRunning(true);
    canvasthread.start();       
}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    boolean retry = true;
    canvasthread.setRunning(false);
    while(retry){
        try{
            canvasthread.join();
            retry = false;
        }
        catch(InterruptedException e){

        }
    }
}
}

我的PanelThread类:     公共类PanelThread扩展Thread {     private SurfaceHolder _surfaceHolder;     私人DrawingPanel _panel;     private boolean _run = false;

public PanelThread(SurfaceHolder surfaceHolder, DrawingPanel panel){
    _surfaceHolder = surfaceHolder;
    _panel = panel;
}

public void setRunning(boolean run){
    _run = run;
}

@Override
public void run(){
    Canvas c;
    while(_run){
        c = null;
        try{
            c = _surfaceHolder.lockCanvas(null);
            synchronized(_surfaceHolder){
                _panel.onDraw(c);
            }
        }
        finally{
            if(c!=null){
                _surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

}

任何帮助将不胜感激!我完全被这个问题困扰了。

1 个答案:

答案 0 :(得分:1)

在第canvas.drawColor(Color.TRANSPARENT);行后添加更多代码或更改颜色。绘制透明色将在屏幕上显示不同的内容。此外,您错过了对onDraw的超级电话。

以下是Canvas的Android文档,您可以在其中找到许多绘制路径,形状和位图的方法: http://developer.android.com/reference/android/graphics/Canvas.html

@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas); ///add missing super
    canvas.drawColor(Color.RED);
}