Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating bitmap from a drawable
    primarykey
    data
    text
    <p>I am going to be working on an app which requires drag/drop on a <code>Canvas</code>. Basically, I want to take a <code>ShapeDrawable</code> and convert it into a <code>Bitmap</code> which I can have the user drag around the screen. This is a simple exercise in itself.</p> <p>However, I want to add text inside my shape. Is there a way I can add text to the <code>drawable</code> itself then convert to a bitmap? I looked into creating a <code>TextView</code> with a drawable as a background.</p> <p>Is this the best way to do it? I sort of want to avoid creating <code>TextViews</code> in my code. Any advice is appreciated.</p> <p>Edit 2/21/2013:</p> <p>In response to JustDanyul's post I have the following code:</p> <pre><code>int width = 40; int height = 40; Bitmap.Config config = Bitmap.Config.ARGB_8888; bitmap = Bitmap.createBitmap(width, height, config); Canvas canvas = new Canvas(bitmap); Resources res = context.getResources(); Drawable shape = res.getDrawable(R.drawable.miss_scarlet); shape.draw(canvas); Paint paint = new Paint(); paint.setTextSize(fontSize); paint.setColor(Color.BLACK); canvas.drawText(gameToken.getDbName(), 5, 5, paint); </code></pre> <p>My drawable is not showing up when I draw the bitmap on another canvas. The drawable itself is fine (I tested it as a background to a TextView). The text shows up. Am i missing something in this code?</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"&gt; &lt;corners android:radius="4dp" /&gt; &lt;solid android:color="#FF0000" /&gt; &lt;stroke android:width="3dp" android:color="#000000" /&gt; &lt;/shape&gt; </code></pre> <p>Edit #2 2/21/2013:</p> <p>I added:</p> <pre><code>shape.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight()); </code></pre> <p>to my code and now the drawable appears but my text is gone (or just hidden).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload