x,y-coordinate example for drawtext

Put your problem here if it does not fit any of the other categories.

x,y-coordinate example for drawtext

Postby massmoss » Fri Feb 22, 2008 4:05 am

Hello,

I am trying to use drawtext(...) in Overlay class. In SDK, there is description for drawtext(...):
Draw the text, with origin at (x,y), using the specified paint. The origin is interpreted based on the Align setting in the paint.
Parameters
text The text to be drawn
x The x-coordinate of the origin of the text being drawn
y The y-coordinate of the origin of the text being drawn
paint The paint used for the text (e.g. color, size, style)

I want to know the x/y example for this android screen. (600,-600) has been tried but it isn't ok. Does anyone have the example for them or it's better to know the x scope and y scope for android screen.

Thanks in advance!

massmoss
massmoss
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 19, 2008 4:34 am

Top

Postby res » Fri Feb 22, 2008 5:18 pm

The co-ordinates 600,-600 are going to draw the text off screen. If you are using the default HVGA emulator in SDK m5 the screen will be 320x455 or 320x434 with the title bar visible.

So in order to see the text you have drawn on the screen your x and y co-ordinates must be less than the emulator screen size.

0,0 will be the top left of the emulator, incrementing x will shift your text right and incrementing y will shift your text down.

ps, this should be in the your coding problems section.
res
Senior Developer
Senior Developer
 
Posts: 146
Joined: Tue Nov 20, 2007 8:14 pm
Location: United States

Postby massmoss » Sat Feb 23, 2008 4:16 am

I have tried again as your suggestion, but it isn't work either.
Here is all my code:
MyMapView.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.app.Activity;
  2.  
  3. import android.os.Bundle;
  4.  
  5.  
  6.  
  7. import com.google.android.maps.MapActivity;
  8.  
  9. import com.google.android.maps.MapView;
  10.  
  11. import com.google.android.maps.OverlayController;
  12.  
  13. import com.google.android.maps.Overlay;
  14.  
  15.  
  16.  
  17. public class MyMapView extends MapActivity {
  18.  
  19.     /** Called when the activity is first created. */
  20.  
  21.        
  22.  
  23.         private MapView myMapView;
  24.  
  25.        
  26.  
  27.     @Override
  28.  
  29.     public void onCreate(Bundle icicle) {
  30.  
  31.         super.onCreate(icicle);
  32.  
  33.         setContentView(R.layout.main);
  34.  
  35.        
  36.  
  37.         myMapView=new MapView(this);
  38.  
  39.         OverlayController myOc=myMapView.createOverlayController();
  40.  
  41.        
  42.  
  43.         myOc.add(new MyMapOverlay(this), true);  
  44.  
  45.        
  46.  
  47.     }
  48.  
  49. }
  50.  
  51.  
  52.  
  53. MyMapOverlay.java
  54.  
  55.  
  56.  
  57. import android.graphics.Canvas;
  58.  
  59. import com.google.android.maps.Overlay;
  60.  
  61. import android.graphics.Paint;
  62.  
  63. import android.graphics.Paint.Style;
  64.  
  65.  
  66.  
  67. public class MyMapOverlay extends Overlay{
  68.  
  69.        
  70.  
  71.         MyMapView itsMap=null;
  72.  
  73.         Paint paint=null;
  74.  
  75.  
  76.  
  77.         public MyMapOverlay(MyMapView map){
  78.  
  79.                 itsMap=map;
  80.  
  81.                 this.paint=new Paint();
  82.  
  83.         }
  84.  
  85.         @Override
  86.  
  87.         public void draw(Canvas canvas, PixelCalculator calculator, boolean shadow) {
  88.  
  89.                 // TODO Auto-generated method stub
  90.  
  91.                 super.draw(canvas, calculator, shadow);
  92.  
  93.                
  94.  
  95.                 //Paint paint=new Paint();
  96.  
  97.                 paint.setTextSize(14);
  98.  
  99.                
  100.  
  101.                 paint.setStyle(Style.FILL);
  102.  
  103.                 paint.setARGB(80, 156, 192, 36);
  104.  
  105.                 canvas.drawText("Hello Guys", 100, -100, paint);
  106.  
  107.                                
  108.  
  109.                 paint.setARGB(255, 0, 0, 0);
  110.  
  111.                 paint.setStyle(Style.STROKE);
  112.  
  113.                 canvas.drawCircle(100, -100, 7, paint);
  114.  
  115.        
  116.  
  117.         }
  118.  
  119. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



The emulator doesn't show the overlay of map and my output. Its output is in the attachment.


What's the problem? Please help!

B.R.
massmoss
Attachments
Error_Result.jpg
Emulator's error output
Error_Result.jpg (12.42 KiB) Viewed 2386 times
massmoss
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 19, 2008 4:34 am

Postby plusminus » Sat Feb 23, 2008 11:46 am

Hello massmoss,

there are 3 things you are doing wrong:

  1. You are using [font=Courier New]setContentView(R.layout.main);[/font] and didn't define a MapView inside of it.
    a.) Your main.xml is probably the default one. So, alter your main.xml to sth like this:
    Syntax: [ Download ] [ Hide ]
    Using xml Syntax Highlighting
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3.         android:orientation="vertical"
    4.         android:layout_width="fill_parent"
    5.         android:layout_height="fill_parent">
    6.         <view class="com.google.android.maps.MapView"
    7.                 android:id="@+id/myMapView"
    8.                 android:layout_width="fill_parent"
    9.                 android:layout_height="fill_parent"/>
    10. </LinearLayout>
    Parsed in 0.002 seconds, using GeSHi 1.0.8.4

    and call :darrow: after [font=Courier New]setContentView(R.layout.main);[/font] instead of creating a new MapView.
    Syntax: [ Download ] [ Hide ]
    Using java Syntax Highlighting
    1. this.myMapView = (MapView)findViewById(R.id.myMapView);
    Parsed in 0.031 seconds, using GeSHi 1.0.8.4


    b.) or call :darrow: in onCreate() instead of [font=Courier New]setContentView(R.layout.main);[/font]
    Syntax: [ Download ] [ Hide ]
    Using java Syntax Highlighting
    1. this.setContentView(this.myMapView);
    Parsed in 0.033 seconds, using GeSHi 1.0.8.4
  2. The coordinates of the visible area on the device-screen are both positive. (as res said)
  3. Don't name your Activity "xyzView" because its an Activity not a View ;)

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 massmoss » Sun Feb 24, 2008 2:40 pm

Hello plusminus,

Your comments help me on cleanning up these func. It's great to improve. And I also revised the main.xml as your suggestion. But after build, eclipse reports "No resource identifier found for attribute 'id' in package 'android". It's odd.

Did you ever meet this? Thanks

B.R.
massmoss
massmoss
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 19, 2008 4:34 am

Postby plusminus » Sun Feb 24, 2008 9:31 pm

Hello massmoss,

are you using sdk-version m5 :?:
Instead of:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:id="@+id/myMapView"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

try
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. id="@+id/myMapView"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4

if it works then, you are still working with sdk-version m3 :!:

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

Top

Postby massmoss » Mon Feb 25, 2008 12:20 pm

Hello plusminus,

Yes, I am using sdk-version m3. After I revised main.xml, the build can finish successfully.
But the emulator still can't show the map and overlay the text and one circule on the screen.
I have attached the result from the emulator. Is there any other point that I missed?

Thanks

massmoss
Attachments
error_result.jpg
error_result.jpg (13.65 KiB) Viewed 2332 times
massmoss
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 19, 2008 4:34 am

Postby plusminus » Mon Feb 25, 2008 1:41 pm

Pleas post your current main.xml and your onCreate().

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 massmoss » Tue Feb 26, 2008 4:56 pm

Hello,

Here are the all the code that I have changed:
1.
MyMapView.java:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package www.wirelessculture.com;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9.  
  10.  
  11. import com.google.android.maps.MapActivity;
  12.  
  13. import com.google.android.maps.MapView;
  14.  
  15. import com.google.android.maps.OverlayController;
  16.  
  17. import com.google.android.maps.Overlay;
  18.  
  19.  
  20.  
  21.  
  22.  
  23. public class MyMapView extends MapActivity {
  24.  
  25.     /** Called when the activity is first created. */
  26.  
  27.        
  28.  
  29.         private MapView myMapView;
  30.  
  31.        
  32.  
  33.     @Override
  34.  
  35.     public void onCreate(Bundle icicle) {
  36.  
  37.         super.onCreate(icicle);
  38.  
  39.        
  40.  
  41.         setContentView(R.layout.main);
  42.  
  43.         this.myMapView = (MapView)findViewById(R.id.myMapView);
  44.  
  45.  
  46.  
  47.        
  48.  
  49.         OverlayController myOc=myMapView.createOverlayController();
  50.  
  51.         myOc.add(new MyMapOverlay(this), true);  
  52.  
  53.        
  54.  
  55.       }
  56.  
  57. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


2.
MyMapOverlay.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package www.wirelessculture.com;
  2.  
  3.  
  4.  
  5. import android.graphics.Canvas;
  6.  
  7. import com.google.android.maps.Overlay;
  8.  
  9. import android.graphics.Paint;
  10.  
  11. import android.graphics.Paint.Style;
  12.  
  13.  
  14.  
  15.  
  16.  
  17. public class MyMapOverlay extends Overlay{
  18.  
  19.        
  20.  
  21.         MyMapView itsMap=null;
  22.  
  23.         Paint paint=null;
  24.  
  25.  
  26.  
  27.         public MyMapOverlay(MyMapView map){
  28.  
  29.                 itsMap=map;
  30.  
  31.                 this.paint=new Paint();
  32.  
  33.         }
  34.  
  35.        
  36.  
  37.        
  38.  
  39.         @Override
  40.  
  41.         public void draw(Canvas canvas, PixelCalculator calculator, boolean shadow) {
  42.  
  43.                 // TODO Auto-generated method stub
  44.  
  45.                 super.draw(canvas, calculator, shadow);
  46.  
  47.                
  48.  
  49.                 //Paint paint=new Paint();
  50.  
  51.                 paint.setTextSize(14);
  52.  
  53.                
  54.  
  55.                 paint.setStyle(Style.FILL);
  56.  
  57.                 paint.setARGB(80, 156, 192, 36);
  58.  
  59.                 canvas.drawText("Hello Guys", 100, 100, paint);
  60.  
  61.                                
  62.  
  63.                 paint.setARGB(255, 0, 0, 0);
  64.  
  65.                 paint.setStyle(Style.STROKE);
  66.  
  67.                 canvas.drawCircle(100, 100, 7, paint);
  68.  
  69.        
  70.  
  71.                
  72.  
  73.         }
  74.  
  75.        
  76.  
  77. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

3.
main.xml

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


I also try to change the ***view to another name. But there is some problem for build. Please just refer the above part. Thanks

massmoss
massmoss
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 19, 2008 4:34 am

Postby plusminus » Tue Feb 26, 2008 6:40 pm

Hello massmoss,

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


and your main.xml doesn't contain a TextView.
:arrow: This is probably a problem with the emulator, that it doesn't update your application on the emulator.

Run your project once with "-wipe-data" (use the search to find where to put it).
That will reset your emulator and wipes all problems related to it away.

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 massmoss » Wed Feb 27, 2008 10:19 am

Hello plusminus,

It works, great thanks!

B.R.
massmoss
massmoss
Junior Developer
Junior Developer
 
Posts: 16
Joined: Sat Jan 19, 2008 4:34 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 17 guests