2017-06-02 64 views
-1

我需要使用android的Canvas类来创建像在android项目中的图纸。但我没有开发Android应用程序,只是简单的cmd行脚本来生成一堆图纸。现在我发现了javas Graphics2D不能满足我的需求。那么我怎样才能在eclipse中的java项目中使用android canvas图形呢?Java:如何在Eclipse java项目中使用android类android.graphics.Canvas?

我想创建画布,画到它像drawArc的drawLine ......然后将其保存为位图

+0

该类不会在Android以外的GUI中绘制,尽管 –

+0

@ cricket_007我不想绘制GUI,我想要的是创建画布并像绘制drawAine一样绘制它,然后将其另存为位图 – Michal

+0

和Graphics2D已经可以做到这一点,或多或少 https://stackoverflow.com/questions/8202253/saving-a-java-2d-graphics-image-as-png-file –

回答

0

林你的意思是不知道,但我认为你正在试图做的是用户是能够放入形状或它会自动创建形状。您可以使用希望提供为出发点这个链接,您可以设置以下的第二个链接:)

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) 

或者像这样

不同的形状
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; 
} 

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

相关问题