1

I asked this question about drawing text to a canvas and I was informed I could add a text view to a layout and draw the layout to the canvas like this

My problem is, when I try to convert to bmp and display I get a black rect.

Here is my code:

  LinearLayout layout = new LinearLayout(getActivity());
    TextView textView = new TextView(getActivity());
    textView.setVisibility(View.VISIBLE);
    textView.setText("صيققق٣يفاوووووومالؤؤتمكا");
    layout.addView(textView);
    layout.setBackgroundColor(getActivity().getResources().getColor(R.color.transparent));
    textView.setTextColor(getResources().getColor(R.color.white));
    layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(layout.getMeasuredWidth(), layout.getMeasuredHeight(), Bitmap.Config.RGB_565);
    //layout.draw(canvas);
    Canvas canvas = new Canvas(bitmap);
    layout.layout(0,0, canvas.getWidth(), canvas.getHeight());
    layout.draw(canvas);

I know I'm going wrong but I don't know where, I think I'm following the correct steps..

Can someone point out to me where I'm going wrong. Thanks

Community
  • 1
  • 1
DJ-DOO
  • 4,545
  • 15
  • 58
  • 98

5 Answers5

1

I can't understand what a problem with draw text on canvas, but try that:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView) findViewById(R.id.image_for_test_displaying);
        imageView.setImageBitmap(getBitmapFromArabicText("صيققق٣يفاوووووومالؤؤتمكا"));
    }

    private Bitmap getBitmapFromArabicText(String string) {
        Paint paint = new Paint();
        //size can be customized
        Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        //Draw black background
        paint.setColor(Color.BLACK);
        canvas.drawRect(0, 0, bitmap.getWidth(), bitmap.getHeight(), paint);
        //draw white text
        paint.setColor(Color.WHITE);
        //text position you can manage from yourself
        canvas.drawText(string, 10, 10, paint);
        return bitmap;
    }
}
Tronum
  • 707
  • 3
  • 13
  • I will need to use the layout approach as I will need to add a few arabic text views. – DJ-DOO Jul 14 '15 at 13:43
  • @DJ-DOO your problem is not draw view on canvas. Problem is this view not attached to real parent and they haven't real drawing. You can check it: attach your runtime created layout to some container from your activity layout and then draw it on canvas - all will be fine. – Tronum Jul 14 '15 at 14:24
  • but that won't work when trying to print..it may work to display on screen which I don't want to do. – DJ-DOO Jul 14 '15 at 14:34
  • I just need to figure out how to change the background to transparent, I am going to have black text colour in the text view. – DJ-DOO Jul 14 '15 at 14:40
  • Doe anyone know how to ensure that the background of the bitmap is transparent so I can have black text with transparent background? – DJ-DOO Jul 14 '15 at 14:59
1

This question may already be answered but here is my simple solution for making sure the background and view itself display ok. "content" was a tablelayout.

Bitmap foreground = Bitmap.createBitmap(content.getWidth(), content.getHeight(), 
            Config.ARGB_8888);
    Canvas canvas = new Canvas(foreground);
    canvas.drawColor(Color.WHITE);
    content.draw(canvas);
user960914
  • 197
  • 1
  • 3
  • 12
1

I have same problem. Set background attribute on root layout fix my problem.

Ref: https://www.cnblogs.com/graphics/p/5933773.html

4b0
  • 21,981
  • 30
  • 95
  • 142
Tom
  • 333
  • 2
  • 8
0

You are not creating Bitmap from the Layout. Create it using below code:

layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = layout.getDrawingCache();
Green goblin
  • 9,898
  • 13
  • 71
  • 100
  • I found this doesn't always work. It depends where the view is handled. [Sometimes](http://stackoverflow.com/questions/11560882/call-to-getdrawingcache-returns-null-when-scroll-is-enabled) the call to getDrawingCache() returns null so the way i posted guarantees there is a background (in my case white..no more black screens) and then draw the content of the layout/view. PS My answer also draws on the answer in the link. – user960914 Feb 25 '16 at 15:22
0

Set a color to your root view

  android:background="@color/white"
san88
  • 1,286
  • 5
  • 35
  • 60