【问题标题】:Java: How to use android class android.graphics.Canvas in Eclipse java project?Java:如何在 Eclipse java 项目中使用 android 类 android.graphics.Canvas?
【发布时间】:2017-11-04 10:39:11
【问题描述】:

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

我想创建画布并像 drawArc drawLine 一样绘制它...然后将其保存为位图

【问题讨论】:

  • 不过,该类不会在 Android 以外的 GUI 中绘制
  • @cricket_007 我不想绘制 GUI,我想要的是创建画布并像 drawArc drawLine 一样绘制它...然后将其保存为位图
  • Graphics2D 已经可以做到这一点,或多或少stackoverflow.com/questions/8202253/…

标签: java android android-canvas graphics2d


【解决方案1】:

我不确定你的意思,但我认为你想要做的是用户能够放入形状或者它会自动创建形状。您可以使用希望提供的链接作为起点,您可以在第二个链接之后设置不同的形状:)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2012-12-29
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多