Create image of imageview having textview on it - android

HI'm having an imageview having EditText on that Image. I want to create single Image of that imageview with EditText.
I tryed this,
editTextOptOneInput.buildDrawingCache();
imageViewOptOne.setImageBitmap(editTextOptOneInput.getDrawingCache());
imageViewOptOne.buildDrawingCache();
Bitmap bitmap1 = imageViewOptOne.getDrawingCache();
but chamge my image to black as my text color is black(I guess).

You may use only 1 buildDrawingCache(), the subsequent ones will replace the previous contents.
So, you better group the EditText and the ImageView into a container and shoot that one.
OR...
Instead of using an EditText, just use a TextView.
This one can have one or more compound drawable inside.
So, you may shoot the TextView only.
This is a preferred solution, since it reduces the View and (possibly) the layout count.
[EDIT]
To use compound drawables simply use in xml the android:drawableLeft = "#drawable/your_drawable" (and/or drawableRight, drawableTop, drawableBottom) attribute/s of your TextView.
To set them in Java, use setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom), as found in the official docs: http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds(int, int, int, int)

You need get drawing cache of Parent of that imageview & EditText.
parentLayout.buildDrawingCache();
Bitmap bitmap1 = parentLayout.getDrawingCache();
Where parentLayout contains imageview & EditText.

I don't understand why you need that but as I understand you should create new Bitmap object from drawing cache before set as source of ImageView

Reminder:
If you are capturing the result image+text from screen, the quality of the output image is certainly compromised
If you want to keep the original quality of image, you should use Canvas and Bitmap to help you
Canvas c=new Canvas();
c.setBitmap(bitmap); // *mutable* copy of bitmap of the image for the ImageView
c.drawText(text, x, y, paint); // font size and typeface can be set through "Paint" class
bitmap.compress(CompressFormat.PNG, 100, new FileOutputStream(out)); // export output

Related

Should I use a LinearLayout with repeated background or a SVG ImageView to fill a whole row?

Linked questions
The below questions couldn't finally solve my problem, described below.
Android: Is it possible to repeat an (SVG!) drawable inside an ImageView?
Android: Keep ratio for a full-width and undefined height ImageView in a ConstraintLayout?
Showing a full-width ImageView containing a SVG distorts it
Context
I use a ConstraintLayout. My aim is to show an SVG image which contains a button:
Its width is 100% the screen's width - it must be repeated
Its height is defined to be the space between the bottom side of a widget and the bottom side of the button - it must be repeated
The problem
Each time I tried to show this image, either the drawable was badly scaled, badly cropped or blurred.
An exemple is:
It should look like this:
Many tests
I have tried to use an ImageView with the attribute src: I used every scale type, with and without the attribute that allows to set a custom ratio
I have tried to use an ImageView with a background instead of src
I have tried to use a RelativeLayout with a background drawable file that is repeated: so I didn't use an SVG image but its JPEG version, and even this way has resulted in bad results
Nota for 3.: I'd really want to use an SVG image instead of a bitmap one, because it will be resolutions-compliant.
My question
So, given all these explanations (cf.: part Context) and given the above illustrations, how would you proceed to show this image?
use java code
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#888888")); // bg color
Drawable vDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_vector_star); // vector drawable
if (vDrawable != null) {
Bitmap bitmap = Bitmap.createBitmap(vDrawable.getIntrinsicWidth(), vDrawable.getIntrinsicHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vDrawable.draw(canvas);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); // set repeat
LayerDrawable drawable = new LayerDrawable(new Drawable[]{colorDrawable, bitmapDrawable});
findViewById(R.id.frameLayout).setBackground(drawable);
}

Using ClipDrawable to hide a part of and ImageView

I am trying to hide a part of an image so that the user does not see it. Initially I copied the Bitmap pixels on another Bitmap, without copying only the pixels that I needed and making the second bitmap the correct size at creation. That worked, but I have many large images and that results in OOMs unfortunately. So instead of doing that I thought on using a ClipDrawable to draw the image, and making the pixels that I don't need invisible.
The code is as follows
ClipDrawable clipDrawable = new ClipDrawable(new BitmapDrawable(resources, bitmap), gravity, orientation);
clipDrawable.setLevel(level);
// Cannot use as the imageview source. Must use background or else we don't get anything on the screen.
picture.setBackground(clipDrawable);
// This is super important. Do not modify this! Without it you will not get the fullscreen image working and the ImageView will be deleted
// from the parent layout.
picture.setImageResource(android.R.color.transparent);
The idea is that I calculate the level based on the image size so that I hide the pixels that I don't need. And it's working. Except I don't understand why I need to use
picture.setBackground(clipDrawable);
picture.setImageResource(android.R.color.transparent);
instead of the more normal:
picture.setImageDrawable(clipDrawable);
If I do the second more normal example then I don't get anything in the ImageView, but if I set it as a background and put a transparent image over it, then it works. Since I want to further manipulate the ImageView using a zooming class that needs the picture set as the src and not as background, I cannot have both, either I get the ClipDrawable showing or I get to have zoom on the image.
Any help would be appreciated!
picture.setImageDrawable(new
ClipDrawable(new BitmapDrawable(resources, bitmap), gravity, orientation
));
ClipDrawable clipDrawable = (ClipDrawable) picture.getDrawable();
clipDrawable.setLevel(level);

Define layout content area

I want to define the contentArea of a Frame/Linear/Relative layout so that when I do layout.addView(view); it only added within the boundries and the rest of the view is clipped away. Like the picture of shirt, if I want to define the boundries of shirt to be the area of content...how can I do that. Any suggestions/help?
Thanks
PS: cannot do that in 9-patch (right and bottom) as it only defines in rectangular shape, in this case shape can be anything like that of a shirt. Want to omit the white/transparent part from the contentArea.
try the below code :-
Just in case someone is trying to solve same problem, there is a better solution: Bitmap.createBitmap(Bitmap, int x, int y, int width, int height). For example, if you need to crop 10 pixels from each side of a bitmap then use this:
Bitmap croppedBitmap = Bitmap.createBitmap(originalBitmap, 10, 10, originalBitmap.get
That's not really something you can do.
From what i understand you can't use PorterDuff because you need to add views to this clipped container.
I would suggest an easy trick that might be useful for u. Just add an imageview on top of your Frame/RelativeLayout that has as content an image with a full transparency on the part you want to be visible and it's opaque in the area u want to clip.
Graphically speaking that way you should achieve exactly what u need.

How to set a drawable to a TextView's text?

Short question:
Suppose I have a TextView, and I wish that the text itself (not the backround) will have a drawable set to it (and not just a solid color), how should I do it?
The only solution I've found is this one , but it's only for gradient colors, and I wish to be able to use any drawable.
Ok, I think I've found a possible solution, but I have some notes about it:
It's not for any drawable. It's for a bitmap, so you will need to somehow convert it to a bitmap.
It requires that you know the size of the textView (which you can use this for this purpose)
It requires that you have enough memory for the scaled bitmap that is in the same size of the textView.
Not sure how to make it work per character or per line.
Here's the code:
public static void setBitmapOnTextView(final TextView tv, final Bitmap bitmap) {
final TileMode tile_mode = TileMode.CLAMP;
final int height = tv.getHeight();
final int width = tv.getWidth();
final Bitmap temp = Bitmap.createScaledBitmap(bitmap, width, height, true);
final BitmapShader bitmapShader = new BitmapShader(temp, tile_mode, tile_mode);
tv.getPaint().setShader(bitmapShader);
}
I hope that there is a better solution for this.
edited:
I don't believe it's possible to change the actual stroke used on the text, the best closest option you probably have is to use a different font.
In android you can "draw" any text using any *.ttf font.
for that you must include the file in your assets folder and call from code (maybe there's a way using XML, but I don't know how) this:
TypeFace mFont = TypeFace.createFromAsset(mContext.getAssets(), "my_font.ttf");
mTextView.setTypeFace(mFont);
I reckon that is the closes you'll get to have a different stroke on the text.
original answer:
TextView have the following properties:
Drawable Top
Drawable Bottom
Drawable Left
Drawable Right
that you set on the XML. Or you can call
setCompoundDrawables(left, top, right, bottom)
from code!

Drawing a large number of ImageViews with the same Bitmap but different size

I am trying to draw multiple ImageViews inside a single LinearLayout.
All the ImageViews need to have a single bitmap.
The ImageViews will only vary in size.
The single bitmap will not be resized.
The simple way is to create one Bitmap per ImageView. But, this runs out of memory quickly.
final Bitmap placeholderBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
final Canvas canvas = new Canvas(placeholderBitmap);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.placeholder_image), 0, 0, null);
imageView.setImageBitmap(placeholderBitmap);
linearLayout.addView(imageView);
I also tried setting the max and min height and width, no effect. The images remain the same size.
imageView.setImageResource(R.drawable.ic_no_image);
imageView.setMaxHeight(imageViewInfo.height);
imageView.setMaxWidth(imageViewInfo.width);
I believe working with Drawables is the right "Android" way to do it, but I can't find a way to dynamically create a Drawable with the right side and layering the shared bitmap into it.
Just use one Bitmap -- as you say they will all use the same Bitmap, so no duplication is necessary.
If you want the ImageViews to have different sizes and have the Bitmap scale itself to the size of the ImageView then use the setScaleType or android:scaleType attribute to set the scaling of the ImageView. For instance FIT_START maintains the aspect ratio of your image and tries to fill the ImageView starting from the top-left corner.
I don't see any need to create a Drawable, though if you need to you can just create one from a Bitmap using:
Drawable d = new BitmapDrawable(bitmap);
ImageView actually does this automatically when you call setImageBitmap(...).
ImageView will always resize the bitmap to it need proportions.
If you want to save memory, resize the bitmaps manually and set them into the ImageViews this should stop the ImageView from resizing it internally - to make sure, you can set the layout_width and layout_hieght of the image view to 'wrap_content'.
I think may be looking for a ClipDrawable.
You would set the android:drawable property of the clip XML to your bitmap
That or you can do it with the Java code also given in the example

Categories

Resources