简体   繁体   中英

Android - draw image and texts on canvas in different positions

I want to draw some texts and an image on canvas, The template would be something like this

I managed to draw the texts, but I'm stuck on drawing the image.

Here is what I have done

 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" />

Any idea how to draw the image?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM