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 ;-)
Related
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);
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);
I have an application with two RelativeViews, the dimension of one is Matchparent, 250dp, and the other one is 300dp, 200dp.
They are initialized as follows:
//These are global variables for the views
View mView;
View resizedView;
//These lines allow the user to draw to the RelativeViews, this code is on the onCreate
//SignatureView class courtesy of Square
mView = new SignatureView(activity, null);
resizedView = new SignatureView(activity, null);
layout.addView(mView, new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
resized.addView(resizedView, new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
What I want to happen is that after the user draws on the larger View (mView) and presses a button, a scaled version of that appears in the smaller View (resizedView).
This is what I have so far in that onClick function of the button but it does not work.
//this code fetches the drawing from the larger view
Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(),
mView.getHeight(), Bitmap.Config.ARGB_8888);
//this code resized it accordingly to a new bitmap
Bitmap resizedImage = Bitmap.createScaledBitmap(bitmap, 399, 266, true);
//this is my sad attempt on creating a canvas with the resized image
Canvas c = new Canvas(resizedImage);
c.drawColor(Color.WHITE);
c.drawBitmap(resizedImage, 0, 0, null);
//this is where I attempt to attach the scaled image to the smaller view
resizedView.draw(c);
//then I go resize it because I need to save/print it
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resizedImage.compress(Bitmap.CompressFormat.PNG, 90, stream);
What am I doing wrong here?
The problem is that the Bitmap of the resized image is instantiated right after the creation of the bitmap that gets the user input. This was my code that worked.
However, note that I made an imageView to hold the resized image.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//get the user input and store in a bitmap
Bitmap bitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);
//Create a canvas containing white background and draw the user input on it
//this is necessary because the png format thinks that white is transparent
Canvas c = new Canvas(bitmap);
c.drawColor(Color.WHITE);
c.drawBitmap(bitmap, 0, 0, null);
//redraw
mView.draw(c);
//create resized image and display
Bitmap resizedImage = Bitmap.createScaledBitmap(bitmap, 399, 266, true);
imageView.setImageBitmap(resizedImage);
How do I make my editText what is written in, get written on the Bitmap
I found this code, but it doesn't work
EditText et = (EditText) findViewById(R.id.etWrite);
Bitmap b = Bitmap.createBitmap(500,500,Bitmap.Config.Alpha_8);
Canvas c = new Canvas(b);
et.draw(c);
I think I should use currentBitmap I think, I tried but it doesn't work
Try something like I show you blow:
EditText et = (EditText) findViewById(R.id.etWrite);
et.buildDrawingCache();
Bitmap bmp = Bitmap.createBitmap(et.getDrawingCache());
Canvas c = new Canvas(bmp);
et.draw(c);
Hope it works!
Bitmap.Config.ALPHA_8 only draws the alpha channel. Use Bitmap.Config.ARGB_8888.
This code should be enough to put the generated bitmap to a view.
EditText et = (EditText) findViewById(R.id.etWrite);
et.buildDrawingCache();
Bitmap bitmap = et.getDrawingCache();
Now use the following line for a normal view
view.setBackgroundDrawable(new BitmapDrawable(bitmap));
for imageview use
imageview.setImageDrawable(new BitmapDrawable(bitmap));
AFAIK the draw method will overwrite the pixel values of the bitmap you pass to it using the canvas.
Ok, so this might be a bit late, but basically, you need to use these calls to write onto your canvas. First, use an OnFocusChanged listener to set your edittext text to a string, then write it onto your canvas in the Ondraw(c) call for the canvas. Before you do, though you need a string to write to (etstring) from your edittext, and you need to declare a paint object.
Paint paint= new Paint();
paint.setTypeface(Typeface.SERIF); //sets typface
int textx = screenwidth/2; //use screenwidth/2 to center the text
c.drawText(etstring, textx, 85, paint); //85 is the height
I need to "compute" the resulting bitmap obtained from overlapping two different bitmaps that have an alpha value somewhere between 0 and 255. I need to do this in java code, not xml, because bitmaps are being loaded dinamically and not from resources.
Here is my first try (which always yields a black bitmap...):
private Drawable composeBitmaps(int alpha1, BitmapDrawable bm1,
int alpha2, BitmapDrawable bm2)
{
Canvas c = new Canvas();
Bitmap b1 = bm1.getBitmap();
BitmapDrawable draw1 = new BitmapDrawable(b1.copy(b1.getConfig(), true));
draw1.setAlpha(alpha1);
c.setBitmap(draw1.getBitmap());
Paint p = new Paint();
p.setAlpha(alpha2);
c.drawBitmap(bm2.getBitmap(), 0, 0, p);
return draw1;
}
...
View v = // whatever
v.setBackgroundDrawable(composeBitmaps(100, bdrawable1, 150, bdrawable2));
And the view goes black background. What am I doing wrong?
My code above is absolutely correct. The bug that made the bitmap go black was elsewhere. Found it, fixed it, now it works.
The only thing to note is that my code is slow as hell and it cannot be used to crossfade two fullscreen bitmaps at a reasonable fps rate.