Problem with Canvas drawArc

All your problems with Audio, Video and Images.

Problem with Canvas drawArc

Postby xspotlivin » Thu Feb 26, 2009 8:40 am

I'm trying to create a customized clock that will highlight certain intervals that I choose (like a pie slice). I'm trying to use the drawArc command for the Canvas class. However, it will only draw arcs of 360 degrees or more (full circles). I'd like to draw arcs of about 15 to 20 degrees or so. I have no idea what is wrong with my code. Can anyone figure out the problem? I know it's not being drawn behind my background because it will draw the full circle on top (you can just remove the background line of code). I'd really appreciate you all help. My code for my Activity and View class is below:

Activity Class:

Code: Select all
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class AdherenceClock extends Activity {
   
   private ClockView clockView = null;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        this.clockView = new ClockView(this);
       
        //Remove the title bar, set the application to full screen, and set the orientation to landscrape
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
              WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
       
        setContentView(this.clockView);
    }
}


And the ClockView Class:

Code: Select all
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.View;

public class ClockView extends View {
   
   protected final Paint amColor = new Paint();
      
   public ClockView(Context context) {
      super(context);
      
      this.setBackgroundDrawable(getResources().getDrawable(R.drawable.clockbackground));
               
      this.amColor.setARGB(100, 248, 225, 110);
   }
   
   @Override
   protected void onDraw(Canvas canvas) {
      
      float startAngle = 0;
      float sweepAngle = 60;
      
      RectF clockRect = new RectF(86, 314, 394, 6);
      canvas.drawArc(clockRect, startAngle, sweepAngle, true, this.amColor);
   }
}


Thanks.
xspotlivin
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 26, 2009 8:15 am
Location: Cambridge, MA

Top

Postby xspotlivin » Sat Feb 28, 2009 11:34 pm

I figured out what the problem was. As you may not (or not know), the (x,y) coordinate system on the Android screen is a little different. The origin is in the top left corner, so therefore x increases across to the right and y increases down to the bottom.

So instead of:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. RectF clockRect = new RectF(86, 314, 394, 6);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


It should have been:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. RectF clockRect = new RectF(86, 6, 394, 314);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


It all makes sense now.
xspotlivin
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 26, 2009 8:15 am
Location: Cambridge, MA

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 21 guests