代码之家  ›  专栏  ›  技术社区  ›  Bisma Frühling

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

  •  0
  • Bisma Frühling  · 技术社区  · 7 年前

    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 回复  |  直到 4 年前
        1
  •  0
  •   veritas1    7 年前
    ImageView iv = (ImageView) findViewById(R.id.iv);
    iv.setImageBitmap(bmp);
    iv.draw(canvas); // Pass canvas to view to draw