How to set opaque picture as layout background - android

I have a full screen layout and I want to use an opaque blurry version of image or bitmap as the background. So I need to know:
1) how to set a image/bitmap as background of a layout like LinearLayout or RelativeLayout. Is there a different approach for bitmap vs. image?
2) How to make it look blurry.

RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
There is no difference between bitmap and image .. just go through this link for more details ..
Android - ImageView: setImageBitmap VS setImageDrawable

Related

Setting LinearLayout Background Resource scale type programmatically

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?

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

What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src

Disabling background image shrinking in Android

It seems background images are automatically shrunk in Android, for example, I use setBackgroundDrawable to set background of a view:
Drawable background = getBackground();
myView.setBackgroundDrawable(background);
Instead of shrinking, I want the image to be cropped to fit the screen size. How to do it? Thanks.
Finally I fixed this problem. I use BitmapDrawable instead of Drawable, since BitmapDrawable has method setGravity(int) which can set the gravity to position/stretch the object.

Categories

Resources