4Answers
  • 11
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to convert Views to bitmaps?

I have two Views (Textview & ImageView) in the FrameLayout, I want to save the image with text. For this, I covert the View to a bitmap.

My xml is:

<FrameLayout 
     android:id="@+id/framelayout"
     android:layout_marginTop="30dip"
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent">

     <ImageView 
          android:id="@+id/ImageView01"
          android:layout_height="wrap_content" 
          android:layout_width="wrap_content"/>

    <TextView android:id="@+id/text_view"
          android:layout_marginTop="30dip"
          android:layout_width="wrap_content" 
          android:maxLines="20"
          android:scrollbars="vertical"
          android:layout_height="wrap_content"/>

</FrameLayout>

How to convert View into Bitmap

FrameLayout view = (FrameLayout)findViewById(R.id.framelayout);

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap bm = view.getDrawingCache();
  • 81
Reply Report

I used to use the buildDrawingCache() method to get a bitmap of my layout, but I was having trouble with it when the view was large. Now I use the following method:

FrameLayout view = findViewById(R.id.framelayout);
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
  • 20
Reply Report
      • 2
    • Thank you!!! I was searching the whole week to find a solution to my problem with large view and this solution worked perfectly!
      • 2
    • @V.Y. This answer assumes that the view has already been laid out. If it hasn't then you will need to create it programmatically (rather than with findViewById).

why don't you write your class that extends ImageView and override method onDraw and put there your image and text, it's more easy

  • 1
Reply Report
    • i tried this but i am getting same image without text.int bw = originalBitmap.getWidth(); int bh = originalBitmap.getHeight(); nb = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);c = new Canvas(nb);Paint paint = new Paint() c.drawText(my_text, 0, 0, paint);