TextView to Bitmap, alignment issues - android

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);

Related

android: add bitmap to another bitmap or increase bitmap size

I need to increase the size of a bitmap in Android, like in this image:
Basically, the user save the Image1. Then, the user can open again Image1 but I need that the Image2 (that is just a white rectangle) is added to the top of the Image1. So, I need to create another image, that is the Image1 plus another image.
How can I do that?
However, I just need to "increase" the Image1 size, so if there's another way to do that please let me know.
Thanks a lot
Since you only need the rectangle on top you can do this, but replace drawRect by some drawBitmap if you want to draw another image on top.
Bitmap i1 = //load the one
int w = i1.getWidth(), h = i1.getHeight();
Bitmap i3 = Bitmap.createBitmap(i1.getConfig(), w, h + 200); //200 will be the white
Canvas surface = new Canvas(i3);
Paint white = new Paint();
white.setColor(Color.WHITE);
surface.drawRect(0, 0, 200, w, white);
surface.drawBitmap(i1, 0, 200, w, h, white);
About increasing height, you can do this with the BitmapConfig.Options, but that will streth the result to match your proportions.

How can i take screenshot of rotated TextView with emoji?

I'm trying to make a screenshot of the rotated TextView which contain emoji icons. But on resulting bitmap i see that emoji are not rotated! Why is this happening? How can i make a screenshot with rotated emoji ?
What i expect:
And this is what i get:
I'm using this method to get screenshot of view:
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = null;
if (layout.getDrawingCache() != null)
bitmap = layout.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
layout.setDrawingCacheEnabled(false);
layout.destroyDrawingCache();
Update:
as i figured, if I set textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); then emoji will not be rotated even in the TextView (if you rotate TextView - they will not be rotated, they will be just moving around like a carousel), but still I don't really understand why is this happening, or rotation of emoji (on first picture) is only because of hardware acceleration?
ok, i couldnt find a way to solve this really annoying issue, so i had to hack it a bit.
imageviews works perfectly with rotation.
so i basically do all the manipulations with image view - and setting it's image out of the emoji text i want using this method:
private Bitmap generateBitmapFromText(int size, String res) {
TextView tv = new TextView(getContext());
tv.setText(res);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 150f);
tv.setTextColor(0xffffffff);
tv.measure(size, size);
int questionWidth = tv.getMeasuredWidth();
int questionHeight = tv.getMeasuredHeight();
Bitmap bitmap = Bitmap.createBitmap(questionWidth, questionHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
tv.layout(0, 0, questionWidth, questionHeight);
tv.draw(c);
return bitmap;
}
and then i call
Bitmap bitmap = generateBitmapFromText(stickersStartingSize, res);
imageview.setImageBitmap(bitmap);

Android custom view: Set canvas size to wrap bitmap

I have a custom view that has Canvas in it. I'm using this canvas to show a bitmap on it and then I can draw over the bitmap on touch. When I load the bitmap it is a lot bigger then the view size and I can't see the whole bitmap (it's picture taken with camera). I tried to create scaled bitmap and then draw it on the canvas, but in that case the bitmap is smaller and the canvas is taking the whole layout space available. I'm adding this view programmatically, not int the xml layout. I have set this to the view but not working:
fdvImage = new ImageEditingView(this, b);
RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
fdvImage.setLayoutParams(lp);
and this is my view constructor:
public ImageEditingView(Context c, Bitmap b) {
super(c);
this.setMinimumHeight(0);
mBitmap = Bitmap.createScaledBitmap(b, b.getWidth()/2, b.getHeight()/2, true);
mCanvas = new Canvas(mBitmap);
}
and in the onDraw I have this:
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
}
So, my question is: Why is the canvas taking all the layout space even if the bitmap is smaller then it?
Use this code to draw bitmap
canvas.drawBitmapMesh(yourBitmap, 1, 1,vertices, 0, null, 0,
paint);
Make vertices according to your view
[EDIT]
float[] vertices = new float[8];
vertices[0]=0; vertices[1]=0; vertices[2]=100; vertices[3]=0;
vertices[4]=0;vertices[5]=200; vertices[6]=100; vertices[7]=200;
change the 100 to your width and 200 to height

Text in OpenGL on an Object

I'm developing a sphere which should show text at certain positions. I tried it with CBFG and it's API, but I just counld saw a text at a fixed position on the screen. But I need to have a dynamic text which I can move around with a onTouchListener. With my Sphere it works fine, also with a Bitmap on it, but how can i make it with the text?
I solved my problem:
TextView textV = new TextView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(128, 128);
textV.setLayoutParams(layoutParams);
textV.setTextColor(Color.WHITE);
textV.setBackgroundColor(Color.TRANSPARENT);
textV.setGravity(Gravity.CENTER);
textV.setText(text);
textV.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap( textV.getLayoutParams().width, textV.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
textV.layout(0, 0, textV.getLayoutParams().width, textV.getLayoutParams().height);
textV.draw(c);
textV.setDrawingCacheEnabled(false);
With that code I'll get a Bitmap which I can use as a texture ;-)

Retrieving the image and add caption to it

I wanted to create an app by taking images from the sd card and add caption to it. Also I wanted to move the caption as per our wish and place it somewhere as desired and save it .can you please suggest an idea for doing this. Also if we want to add a caption to the existing image from SD card then , do we need to have database for doing this. or can it be saved directly to the SD card.
Refer This Answer
You can put an EditText and Write into it and after writting you first convert it to Bitmap like
Bitmap bmp = Bitmap.createBitmap(mEditText.getDrawingCache());
Now you can add created image bmp to your original image like this
call :
Bitmap combined = combineImages(bgBitmap,bmp);
public Bitmap combineImages(Bitmap background, Bitmap foreground) {
int width = 0, height = 0;
Bitmap cs;
width = getWindowManager().getDefaultDisplay().getWidth();
height = getWindowManager().getDefaultDisplay().getHeight();
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawBitmap(foreground, matrix, null);
return cs;
}
Use canvas (Custom Views) for it..
I think this is the only way to make one view (Image in one canvas) on other view (caption in other canvas) on it. So in this you have to complete knowledge of how views draw on canvas and how to move to that views. And using canvas you can also smoothly move your views on screen.
After creating custom views you can save those views in Bitmap, now you can also combine those images to one bitmap..
(Here Customeviews are ImageViews, TextViews, EditText etc..)
All the best. :-)
USe like this.
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
background = Bitmap.createScaledBitmap(background, width, height, true);
comboImage.drawBitmap(background, 0, 0, null);
comboImage.drawText("title", x, y, paint);

Categories

Resources