Android - 在画布上以不同的位置绘制图像和文本

时间:2017-09-19 13:32:58

标签: android image canvas draw

我想在画布上绘制一些文字和图像,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" />

知道如何绘制图像吗?

1 个答案:

答案 0 :(得分:0)

ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setImageBitmap(bmp);
iv.draw(canvas); // Pass canvas to view to draw