Hi everyone,

I want to draw a dashed line in Android.
I know I can achieve it using Paint#setPathEffect.
When I simply get the picture object, I can draw a dashed line wihtout
any problem.

However, for speed, I get a ByteArrayOutputStream
from the Picture object and get another Pciture.
This method almost works correctly, but it seems to lose the dashed
line style.
Here is the sample code to reproduce it.

---

import java.io.*;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.View;

public class DashBugActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new DashBugView(this));
  }
}

class DashBugView extends View {
  DashBugView(Context context) {
    super(context);
  }

  protected void onDraw(Canvas canvas) {
    int w = getWidth();
    int h = getHeight();
    Paint paint = new Paint();
    paint.setColor(-1);
    canvas.drawRect(0, 0, w, h, paint);

    Picture picture = createPicture();
    picture.draw(canvas);
  }

  static Picture createPicture() {
    Picture picture = new Picture();
    Canvas canvas = picture.beginRecording(100, 100);
    Paint paint = new Paint();
    paint.setStrokeWidth(8);
    paint.setPathEffect(new DashPathEffect(new float[] {
        8, 8
    }, 0));
    canvas.drawLine(0, 0, 100, 100, paint);
    picture.endRecording();

    if (false) {
      // Picture draws dash correctly.
      return picture;
    } else {
      // Export and Import bytes, and picture draws solid line !
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      picture.writeToStream(baos);
      byte[] pictureBytes = baos.toByteArray();
      Picture picture2 = Picture.createFromStream(new
ByteArrayInputStream(pictureBytes));
      return picture2;
    }
  }
}
---

The sample code above does not draw the dashed line. However,
if I change from "if(false)" to "if(true)", then I could get the
correct result.
Does anyone know the work-around to avoid it?


-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to