I'm trying to set a Bitmap Drawable as the background like this, root.setBackground(bitmapDrawable);, I've set its bounds to be 1080 wide by 300 high, but it keeps stretching to fit the entire background. Is there any way to avoid this? This is how i created the BitmapDrawable BitmapDrawable bitmapDrawable= new BitmapDrawable(getResources(), bmp1);
I believe the default Gravity of a BitmapDrawable is FILL. Try setting the Gravity to CENTER:
bitmapDrawable.setGravity(Gravity.CENTER);
Related
I have a linear layout that when i want programmatically to enter a TileXY background image to it i can do it that way:
ImageButton.SetImageBitmap(BitmapFactory.DecodeFile(Button1ImageURI));
Bitmap temp = BitmapFactory.DecodeFile(Button1ImageURI);
BitmapDrawable bitmapDrawable = new BitmapDrawable(Resources, temp);
bitmapDrawable.SetTileModeXY(Shader.TileMode.Repeat, Shader.TileMode.Repeat);
MainLinearLayout.Background = bitmapDrawable;
I'm not sure how i can do the same but this time i want the bitmapDrawable to ScaleType.FitCenter in the LinearLayout.
I'm not sure how i can do that, if i don't put a SetTileModeXY in the BitmapDrawable, Background in MainLinearLayout is scaled with FitXY,
but i want to be scaled in FitCenter
Why you want to set image as background on the LinearLayout, when you can create ImageView as a child of the LinearLayout with width and height that match the parent(LinearLayout), and use the benefits of the ImageView. Setting the image as source and use the ScaleType?
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);
}
While developping an Android App, I saw that all my images displayed from the assets are pixelated.
I'm using this 2 lines to display them:
Drawable imageDrawable = Drawable.createFromStream("my_logo.png", null);
imageView.setBackground(imageDrawable);
The images have as dimension: 128/128 or 256/256 and are displayed in a square ImageView.
Do anyone have an idea to keep the quality of my images ?
Changing the dimensions of your image can and will decrease the quality. There are a couple of things you can do
Use vectors instead. Vectors can be scaled up and down infinitely without loss of quality.
Use an ImageView height and width that is the same size as your image or smaller and maintains the aspect ratio.
void setImageDrawable (Drawable drawable)
Sets a drawable as the content of ImageView.
To set drawable to ImageView from java, you need to use setImageDrawable (Drawable drawable) method.
You are setting the drawable as imageView's background, your code should be,
Drawable imageDrawable = Drawable.createFromStream("my_logo.png", null);
imageView.setImageDrawable(imageDrawable);
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
I updated my build to build against Android 1.6, and now my bitmaps are scaled down on high density screens. I do NOT want this behavior. I gave this a shot:
http://blog.tomgibara.com/post/190539066/android-unscaled-bitmaps
but the images are STILL scaling, that is UNLESS I set them to a specific height & width. If I use wrap_content they are scaled down.
I have an image loader using the unscaled bitmap loader to create a drawable like so:
Bitmap bm = UnscaledBitmapLoader.loadFromResource(imageBufferInputStream);
drawable = new BitmapDrawable(bm);
which I later assign to an ImageView like so:
imageView.setImageDrawable( copyBitmapDrawable( (BitmapDrawable) drawable) );
In order to have an image not scaled when loading it from a resource (e.g. with BitmapFactory.decodeResource) you have to locate it in res/drawable-nodpi instead of the usual drawable, drawable-ldpi and so on.
Using
new BitmapDrawable(this.getResources(), bmp);
instead of
new BitmapDrawable(bmp);
should solve the issue.
Wrapping the bitmap with a Drawable is the problem. The Drawable scales the Bitmap. Instead of using a Drawable, assign the Bitmap to the ImageView directly using imageView.setImageBitmap(Bitmap).
Use ImageView.ScaleType. The CENTER constant preforms no scaling, so I guess that's what you are looking for.
By the way, do you use pixels or dips as size units? Using dips (density-independent-pixels) is a means of standardizing display on multiple resolutions. You'll find a couple of tips here.