Cannot draw a simple line

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Cannot draw a simple line

Postby xplosive » Wed Oct 08, 2008 12:25 pm

Hi,

I am trying to draw a simple line, but cannot get it done. This is the code i use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Paint;
  6. import android.graphics.Bitmap.Config;
  7. import android.os.Bundle;
  8.  
  9. public class Happer extends Activity {
  10.     /** Called when the activity is first created. */
  11.     Bitmap bmp;
  12.         @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.main);
  16.        
  17.         bmp = Bitmap.createBitmap(100, 100, true);
  18.        
  19.         Paint p = new Paint();
  20.         p.setColor(0xFFFFFF00);
  21.         p.setStyle(Paint.Style.STROKE);
  22.  
  23.     }
  24.     protected void onDraw(Canvas canvas)
  25.     {
  26.         canvas.drawBitmap(bmp, 2, 2, null);
  27.     }
  28.  
  29. }
  30.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I don't get any errors, but in the emulator i see "Hello world, Happer" But i don't see a line. Can anyone help?

Thnx in advance
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Top

Postby ninor » Wed Oct 08, 2008 12:50 pm

I haven't drawn anything yet in Android, but what's clear in the code is that bitmap isn't attached to anything.
Try first to paint in an image (add it in the xml layout), and then paint before you execute the setcontentview().
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby xplosive » Wed Oct 08, 2008 1:01 pm

So you suggest to attach the canvas to a layout (don't know how) and after that execute setContentView?

I have another another questions:

Is the onDraw() method executed by Android when the application is launched?

What are the meanings of the Config enum? Config.ARGB_8888, Config.ARGB_4444, Config.ALPHA_8 and Config.RGB_565

Thnx

EDIT:

After 1,5 days i finally have the solution:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package android.happer;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.os.Bundle;
  9. import android.view.View;
  10.  
  11. public class Happer extends Activity {
  12.     /** Called when the activity is first created. */
  13.         @Override
  14.     public void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.        
  17.         setContentView(new SampleView(this));
  18.        
  19.     }
  20.        
  21.         private static class SampleView extends View
  22.         {              
  23.                 public SampleView(Context context)
  24.                 {
  25.                         super(context);
  26.  
  27.                 }
  28.                
  29.                 protected void onDraw(Canvas canvas)
  30.             {
  31.                          Paint p = new Paint();
  32.                          p.setColor(Color.WHITE);
  33.                          p.setStyle(Paint.Style.STROKE);
  34.                          
  35.                          canvas.drawColor(Color.BLUE);
  36.                          
  37.                          canvas.drawLine(5, 5, 25, 5, p);
  38.                          
  39.             }
  40.         }
  41.    
  42.  
  43. }
  44.  
  45.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Can somebody tell me why i have to make a class in a class? And why i need to extend View (the class in the class)?

And what does super(context) mean in the SampleView constructor?

I know these are some basic questions, but it is very helpful for me.

Thnx in advance
Last edited by xplosive on Wed Oct 08, 2008 1:54 pm, edited 1 time in total.
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Re: Cannot draw a simple line

Postby MrSnowflake » Wed Oct 08, 2008 1:53 pm

xplosive wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     protected void onDraw(Canvas canvas)
  2.     {
  3.         canvas.drawBitmap(bmp, 2, 2, null);
  4.         <span style="font-weight: bold">super.onDraw(canvas);</span>
  5.     }
  6. }
  7.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I don't get any errors, but in the emulator i see "Hello world, Happer" But i don't see a line. Can anyone help?
Shouldn't you call super.onDraw(canvas)?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby plusminus » Wed Oct 08, 2008 2:09 pm

You explicitly need it only if the super-class also draws something.
I always add it anyway.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Wed Oct 08, 2008 2:14 pm

I didn't know that, but I always did that, so I thought it was mandatory...
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby xplosive » Wed Oct 08, 2008 2:45 pm

Thanx for the replies, but i still have some questions :)

When i want to draw something, is it always necessary to create a class in a class? And why does the class in the class extend View? Can you only draw in a View class?

And does someone has the answer to the enum question:

What are the meanings of the Config enum? Config.ARGB_8888, Config.ARGB_4444, Config.ALPHA_8 and Config.RGB_565

Thnx again :)
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby plusminus » Wed Oct 08, 2008 3:39 pm

MrSnowflake wrote:I didn't know that, but I always did that, so I thought it was mandatory...

I'm not 100% sure, but I think it still worker, when I accidentially removed it :?

xplosive wrote:Thanx for the replies, but i still have some questions :)

When i want to draw something, is it always necessary to create a class in a class? And why does the class in the class extend View? Can you only draw in a View class?

And does someone has the answer to the enum question:

What are the meanings of the Config enum? Config.ARGB_8888, Config.ARGB_4444, Config.ALPHA_8 and Config.RGB_565

Thnx again :)


No, you can also make a TopLevelClass (no Inner Class), but you have to extend View, which is the baseclass of everything you can see on whole Android.

I'm not 100% sure about the Bitmap.Configs, but as far as I know the numbers behind indicate the numebr of bits used for each channel. So for example Config.ARGB_8888 would use 8 bits for each RGB-Color and 8 bits for transparency.
Thanks for asking that, I never thought about that before... it might save me a couple of memory-bytes :D

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby xplosive » Thu Oct 09, 2008 12:44 pm

Thnx for the replay :)

Now i have another question. How can i bind a View class to a layout, so i can center the drawing of which i made in the view class?
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby MrSnowflake » Thu Oct 09, 2008 1:16 pm

I'm doing something like that at the momen. I made a layout with an empty LinearLayout with an id set (layMain) and android:gravity="center". In the application you should layMain=(LinearLayout)findViewById(layMain) and then layMain.addView(yourView);
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby xplosive » Thu Oct 09, 2008 8:42 pm

Thnx for the reply, i tried your suggestion, but i didn't get it work

What i have done:

1. My main.xml looks like this:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="horizontal"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    android:gravity="center"
  13.  
  14.    android:id="@+id/layMain"
  15.  
  16.    >
  17.  
  18. </LinearLayout>
  19.  
  20.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


2. An external class (which extends View) where i draw something, look like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package android.happer;
  3.  
  4.  
  5.  
  6. import android.content.Context;
  7.  
  8. import android.graphics.Bitmap;
  9.  
  10. import android.graphics.Canvas;
  11.  
  12. import android.graphics.Color;
  13.  
  14. import android.graphics.Paint;
  15.  
  16. import android.graphics.Bitmap.Config;
  17.  
  18. import android.view.View;
  19.  
  20.  
  21.  
  22. public class Mens extends View {
  23.  
  24.        
  25.  
  26.         public Mens(Context context)
  27.  
  28.         {
  29.  
  30.                 super(context);
  31.  
  32.                
  33.  
  34.         }
  35.  
  36.        
  37.  
  38.         protected void onDraw(Canvas canvas)
  39.  
  40.     {
  41.  
  42.                 Paint p = new Paint();
  43.  
  44.                 p.setColor(Color.RED);
  45.  
  46.                
  47.  
  48.                 canvas.drawLine(15, 15, 25, 5, p);
  49.  
  50.     }
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


3. In my main class (the relevant code):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public void onCreate(Bundle savedInstanceState) {
  3.  
  4.         super.onCreate(savedInstanceState);
  5.  
  6.        
  7.  
  8.         LinearLayout layMain = (LinearLayout)findViewById(R.id.layMain);
  9.  
  10.         layMain.addView(new Mens(this));
  11.  
  12.         // setContentView(layMain);
  13.  
  14.     }
  15.  
  16.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I don't get any errors in Eclipse, but i do get an error when i try to run the app on the Android emulator: The application has stopped unexpectedly. Please try again.

What's going on? Can someone help?

Thnx again :)
xplosive
Developer
Developer
 
Posts: 26
Joined: Wed Oct 08, 2008 12:21 pm

Postby plusminus » Fri Oct 10, 2008 5:08 am

The activity needs to know what it display. So in the very beginning (after super.onCreate();) call:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.main);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Usually you would also add the view in xml before.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Fri Oct 10, 2008 11:21 am

plusminus wrote:The activity needs to know what it display. So in the very beginning (after super.onCreate();) call:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.main);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Usually you would also add the view in xml before.
What he sais, or setContentView(layMain);
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby plusminus » Fri Oct 10, 2008 2:28 pm

MrSnowflake wrote:
plusminus wrote:The activity needs to know what it display. So in the very beginning (after super.onCreate();) call:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setContentView(R.layout.main);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Usually you would also add the view in xml before.
What he sais, or setContentView(layMain);


Then [font=Courier New]findViewById[/font] would fail :P
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby MrSnowflake » Fri Oct 10, 2008 4:45 pm

Hehe true :D. Wasn't thinking :).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top
Next

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests