Raistlin
Raistlin

Reputation: 167

Background just comes up black when using Canvas

I'm trying to learn how to use canvas to create simple graphics, but right now it is refusing to draw anything and instead only creates a black background.

Here's the 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 file:

<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 class:

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){

        }
    }
}
}

And my PanelThread class: public class PanelThread extends Thread{ private SurfaceHolder _surfaceHolder; private 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);
            }
        }
    }
}

}

Any help would be much appreciated! I'm completely stumped on this problem.

Upvotes: 1

Views: 1890

Answers (1)

petey
petey

Reputation: 17170

Add some more code after the line canvas.drawColor(Color.TRANSPARENT); or change the color. Painting a transparent color will show nothing different on the screen. Also, you are missing the super call to onDraw.

Here is the android docs for Canvas where you will find many methods for drawing paths, shapes and bitmaps: 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);
}

Upvotes: 1

Related Questions