-1

I need to use android Canvas class to create drawings like in android project. But I am not developing android app just simple cmd line script to generate bunch of drawings. Now i found that javas Graphics2D not fill my needs. So how can I use android canvas graphics to in java project in eclipse?

I want to create canvas and draw to it like drawArc drawLine... and then save it as bitmap

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Michal
  • 3,584
  • 9
  • 47
  • 74
  • That class won't draw in a GUI other than Android, though – OneCricketeer Jun 02 '17 at 23:51
  • @cricket_007 I do not want to draw GUI, what I want is to create canvas and draw to it like drawArc drawLine... and then save it as bitmap – Michal Jun 03 '17 at 00:46
  • And Graphics2D can already do that, more or less https://stackoverflow.com/questions/8202253/saving-a-java-2d-graphics-image-as-png-file – OneCricketeer Jun 03 '17 at 03:28

1 Answers1

0

Im not sure what you mean, but I think what you are trying to do is that the user is able to put in shapes or it will automatically create shapes. You can use the link provided hopefully as a starting point for this, you can set up different shapes following the second link :)

https://examples.javacodegeeks.com/android/core/graphics/canvas-graphics/android-canvas-example/

https://developer.android.com/reference/android/graphics/Canvas.html

void    drawOval(float left, float top, float right, float bottom, Paint paint)

Or something like this

public static Bitmap getRoundedShape(Bitmap scaleBitmapImage) {
if (scaleBitmapImage == null) {
    return null;
}
int sourceWidth = scaleBitmapImage.getWidth();
int sourceHeight = scaleBitmapImage.getHeight();
int targetWidth = Math.min(sourceWidth, sourceHeight);
int targetHeight = targetWidth;
Bitmap targetBitmap = Bitmap.createBitmap(
        targetWidth,
        targetHeight,
        Bitmap.Config.ARGB_8888
        );

Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(
        ((float) targetWidth - 1) / 2,
        ((float) targetHeight - 1) / 2,
        Math.min(((float) targetWidth), ((float) targetHeight)) / 2,
        Path.Direction.CCW
        );

canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(
        sourceBitmap,
    new Rect(0, 0, sourceBitmap.getWidth(),
    sourceBitmap.getHeight()),
    new Rect(0, 0, targetWidth, targetHeight),
    null
        );
return targetBitmap;
}

Source: http://www.programcreek.com/java-api-examples/index.php?api=android.graphics.Canvas

77rshah
  • 29
  • 6