【问题标题】:Android - draw image and texts on canvas in different positionsAndroid - 在画布上的不同位置绘制图像和文本
【发布时间】:2018-02-28 07:46:15
【问题描述】:

我想在画布上画一些文字和一张图片,The template would be something like this

我设法绘制了文本,但我一直在绘制图像。

这是我所做的

Bitmap bmp = Bitmap.createBitmap(949, 300, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);

        TextPaint tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        tp.setColor(0xFF000000);
        tp.setTextSize(15 * getApplicationContext().getResources().getDisplayMetrics().density + 0.5f);
        tp.setShadowLayer(2, 0.5f, 0.5f, Color.BLACK);
        StaticLayout sl = new StaticLayout("This is",
                tp, 300, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);
        StaticLayout sm = new StaticLayout("This is",
                tp, 300, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, false);


        canvas.save();
        canvas.translate(50, 20); //position text on the canvas
        sl.draw(canvas);
        canvas.restore();

        canvas.save();
        canvas.translate(50, 90); //position text on the canvas
        sm.draw(canvas);
        canvas.restore();

        ImageView iv = (ImageView) findViewById(R.id.iv);
        iv.setImageBitmap(bmp);
<ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/border_ui" />

知道如何绘制图像吗?

【问题讨论】:

  • 为什么不使用4个不同的组件:一个RelativeLayout作为卡片,两个TextView和一个ImageView。会容易得多
  • 因为我打算在绘制完所有需要绘制的内容后导出图像并保存它@Eselfar

标签: android image canvas draw


【解决方案1】:
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmp);
iv.draw(canvas); // Pass canvas to view to draw

【讨论】:

    猜你喜欢
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    相关资源
    最近更新 更多