How to get Listview all sub views(visible and invisible) - android

my problem is similar to How to get all children (visible and invisible) from a ListView?
i need to save all listview items to bitmap, listview item is inflated by textview. whatever they are visible or invisible in the lisview.
i want to get all child views and convert to bitmap, i tried as below:
view = adapter.getView(i,null,lv);
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth()
view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
bitmap.eraseColor(Color.WHITE);
Canvas canvas = new Canvas(bitmap);
canvas.translate(10, 10);
view.draw(canvas);
mybitmap = combineImages(mybitmap, bitmap);
but if item text is too much (some of them contain long text) the methods ,view.getMeasuredWidth() and view.getMeasuredHeight(), which are not working properly.
I`m wondering if my idea to this problem is right , if not , how can I solve it ?

Used the following to fix:
view.measure(MeasureSpec.makeMeasureSpec(lv.getWidth(),
MeasureSpec.AT_MOST), MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED));

Related

Converting a view to Bitmap without displaying (with setHorizontallyScrollyng) in Android?

In my code, I have a LinearLayout with many TextViews (like a table). And I try to convert this to Bitmap. there is my code:
public static Bitmap convertToBitmap(LinearLayout view){
view.measure(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.draw(c);
return bitmap;
}
But, there is a problem.
Views witch has setSingleLine(true) not display text in bitmap.
Exactly, the problem is in setHorizontallyScrolling(true), which is calling in setSingleLine code...
I try use .setScrollTo(X), but what exactly X must be. X = 0 - not works.
Is there any, who knows how fix it?
EDIT: TextViews has fixed Width.
EDIT: Problem is only for TextViews with Gravity = Center. With gravity Left all's ok.
Try one of this code? It's a bit different from the one you posted above.
/**
* this actually also provided by Romain Guy, but let's try another for performance improvements
*/
public static Bitmap getBitmapFromView(View view, int width, int height) {
view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
// Build the Drawing Cache
view.buildDrawingCache();
// Create Bitmap
Bitmap drawingCache = view.getDrawingCache();
if (drawingCache == null) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(drawingCache);
drawingCache.recycle();
view.setDrawingCacheEnabled(false);
return bitmap;
}
/**
* This method provided by Romain Guy, so it should do the job better, especially it includes case for listViews
*/
public static Bitmap getBitmapFromView(View view, int width, int height) {
//Pre-measure the view so that height and width don't remain null.
view.measure(View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
//Assign a size and position to the view and all of its descendants
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
// Create bitmap
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
//Create a canvas with the specified bitmap to draw into
Canvas canvas = new Canvas(bitmap);
// if it's scrollView we get gull size
canvas.translate(-view.getScrollX(), -view.getScrollY());
//Render this view (and all of its children) to the given Canvas
view.draw(canvas);
return bitmap;
}

TextView to Bitmap, alignment issues

I am facing a situation in which I need to draw bitmap for a given text. I tried to render a TextView with required properties and then draw its bitmap using canvas. My try looks like this:
TextView nameTv = new TextView(context);
Bitmap bmp = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
nameTv.layout(0, 0, 200, 200);
nameTv.setGravity(Gravity.CENTER);
nameTv.setText("Test");
nameTv.setBackgroundColor(Color.RED);
nameTv.draw(canvas);
The problem with this code is that the text on TextView not center aligned, which I require.
Any suggestions are welcomed :)
Thanks,
Ammar
I think the problem is in measuring. You not calling measure for that and it don't have properly size. Call nameTv.measure() before layout().
Call measure like this:
int measureSpecWidth = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);
int measureSpecHeight = MeasureSpec.makeMeasureSpec(200, MeasureSpec.EXACTLY);
nameTv.measure(measureSpecWidth, measureSpecHeight);

Achartengine create bitmap from chart always null

I tried various methods after googling for hours. Some of the methods seem to help some people, but not for me. What am I missing here? Maybe a permission? I know this is a common bug or something, but there must be a solution.
All the methods are similar.
First:
GraphicalView v = ChartFactory.getBarChartView(ChartsDuration.this, buildBarDataset(titles, values), renderer, Type.DEFAULT);
v.setDrawingCacheEnabled(true);
v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
v.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(v.toBitmap()); //bitmap is null
v.setDrawingCacheEnabled(false);
or this:
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache()); //bitmap is null
I also tried the solution here which is:
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
Then the method on the bottom of this page:
v.setDrawingCacheEnabled(false);
if (!v.isDrawingCacheEnabled()) {
v.setDrawingCacheEnabled(true);
}
if (renderer.isApplyBackgroundColor()) {
v.setDrawingCacheBackgroundColor(renderer.getBackgroundColor());
}
v.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
v.getDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
I also did not get any luck with this:
v.setDrawingCacheEnabled(true);
v.requestFocus();
v.getRootView();
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap mBitmap = v.getDrawingCache();
The bitmaps are always null.
just an idea , not sure if it would work:
put the Achartengine view in another view (a frameLayout , for example) , and then capture it instead of the Achartengine view.
also , sure your code runs only after some time , that all of the views have drawn themselves? in order to check it , try getting the width or height of the view.
you also don't have any exceptions or special logs?

List View using bitmap

I have been trying to create a bit map of a list view, where the entire list view is not visible on the screen. I am using
Bitmap mBitmap = fullView.getDrawingCache();
to create bitmap. It works ok for the part of list view that is visible on the screen but, not for the part that isn't.I would like to know if a bitmap of a list view can be created without having to display it completely on the screen. All suggestions and or solutions are appreciated.
As android only draws what is visible, may idea would be to take bitmaps in pieces and then group the bitmaps into a single one. You could first get the bitmap of first n items. Then use the scrollTo method to scroll beyond the previous n items and get the bitmap for the next n items. This way you can get bitmap of the whole list view.
Try using this method. Hack here is calling the layout and measure method of the view yourself.
public Bitmap loadBitmapFromView(View v) {
v.setLayoutParams(new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return bitmap;
}
Pass the view in this method like
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedFrame = inflater.inflate(R.layout.my_view, null);
Bitmap bitmap = loadBitmapFromView(inflatedFrame.findViewById(R.id.id_of_your_view_in_layout));

measure() doesnt work properly with dynamic layouts and textView - Android

I want to convert a RelativeLaout view into a Bitmap. I've tried another answers and different options without success.
My XML View is kind of:
----RelativeLayout
-------ImageView
-------TextView
Every ones have wrap_content measures.
As i need several bitmaps of the view, im inflating it this way:
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.localviewlayout, null);
ImageView img = (ImageView) v.findViewById(R.id.imgPlace);
img.setImageBitmap(MyDynamicBitmap);
TextView txt1 = (TextView)v.findViewById(R.id.text1);
txt1.setText(MyDynamicText);
return v;
After that :
//in measure() i get a nullpointerException on RelativeLayout.onMeasure().I also have tried //with MeasureSpec.EXACTLY
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
v.draw(new Canvas(b));
Paint p = new Paint();
myMainCanvas.drawBitmap(b, myPointX, myPointY, p);
The content of textView of the RelativeLayout is dynamic, so i cant know the width or height of the text.
Besides of the exception, if i manually set enough size on the measure function (insted of 0), my dynamic text doesnt display entirely.
I hope you can help me because right now, im stuck. Thank youuu
Try adding
v.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
before calling measure(). Does that fix the NullPointerException?

Categories

Resources