Odd Behavior: Drawn Line Only Appearing After Reorienation

All your problems with Audio, Video and Images.

Odd Behavior: Drawn Line Only Appearing After Reorienation

Postby adamkomar » Fri Jan 15, 2010 6:23 am

This is only my second app I'm trying to make for my DROID, but the first I've had to use any drawing. It's simply a grid that I want to be able to draw a line on from the center of one square on the grid to another. The selection of squares to draw from and to is done by spinners. Here's the code:

Code: Select all
public class crosshairs extends Activity {
   final int BUFFER = 1;
   final int SPAN = 30;
   final int CENTER = 15;
   final int BASE = 46;

    Bitmap bmpLine = Bitmap.createBitmap(467, 467, Bitmap.Config.ARGB_8888);
    Canvas cvsLine = new Canvas();
    Paint pntLine = new Paint();

   ImageView imgGrid;
   Spinner spinRowStart, spinColStart, spinRowEnd, spinColEnd;

   OnItemSelectedListener spinListener = new OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> a, View v, int i, long l) {
         int rStart = spinRowStart.getSelectedItemPosition();
         int cStart = spinColStart.getSelectedItemPosition();
         int rEnd = spinRowEnd.getSelectedItemPosition();
         int cEnd = spinColEnd.getSelectedItemPosition();

         float xStart = BASE + (rStart * (BUFFER + SPAN));
         float yStart = BASE + (cStart * (BUFFER + SPAN));

         float xEnd = BASE + (rEnd * (BUFFER + SPAN));
         float yEnd = BASE + (cEnd * (BUFFER + SPAN));

           cvsLine.drawLine(xStart, yStart, xEnd, yEnd, pntLine);
           imgGrid.draw(cvsLine);
           imgGrid.invalidate();
      }

      @Override
      public void onNothingSelected(AdapterView<?> a) {
      }
   };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        imgGrid = (ImageView)findViewById(R.id.imgGrid);

        cvsLine.setBitmap(bmpLine);
        imgGrid.setImageBitmap(bmpLine);
        pntLine.setColor(Color.RED);
        pntLine.setStrokeWidth(4);

        spinRowStart = (Spinner)findViewById(R.id.spinRowStart);
        spinRowEnd = (Spinner)findViewById(R.id.spinRowEnd);
        spinColStart = (Spinner)findViewById(R.id.spinColStart);
        spinColEnd = (Spinner)findViewById(R.id.spinColEnd);

        spinRowStart.setOnItemSelectedListener(spinListener);
        spinRowEnd.setOnItemSelectedListener(spinListener);
        spinColStart.setOnItemSelectedListener(spinListener);
        spinColEnd.setOnItemSelectedListener(spinListener);
    }
}


First, the line only seems to be drawn if I hardcode starting and stopping points, but the line only shows up after I tilt my phone to change the orientation. If I use the values of the variables like in the code above then the line never shows up.

I want the line to be drawn and show up anytime the selection of one of the spinners changes. Any idea how I would get that done?
User avatar
adamkomar
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 15, 2010 6:04 am
Location: Sunrise, FL

Top

Postby adamkomar » Fri Jan 15, 2010 4:26 pm

Okay, telling imgGrid to draw the canvas seemed to clear the drawn line from the screen. I removed imgGrid.draw(cvsline); and it's now working as intended.
User avatar
adamkomar
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 15, 2010 6:04 am
Location: Sunrise, FL

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 13 guests