I'm using the follow code, to create a non-scaled, centred image as a background, in a relative layout:-
RelativeLayout explosionlayout = (RelativeLayout) findViewById (R.id.explosionlayout);
explosionlayout.setBackgroundColor(R.color.white);
Bitmap myBitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.bomb);
BitmapDrawable test1 = new BitmapDrawable(myBitmap);
test1.setGravity(Gravity.CENTER);
The only problem I have, is that the background of the relativelayout is grey, regardless of what I set it to, either via XML or in code.
Any ideas would be appreciated, thanks.
You probably want this instead:
explosionlayout.setBackgroundColor(getResources().getColor(R.color.white));
or just
explosionlayout.setBackgroundColor(0xffffffff);
The reason is that R.color.white is an ID, while setBackgroundColor expects an actual 32-bit integer representation of the color.
Related
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);
}
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
The CircularImageView, https://github.com/Pkmmte/CircularImageView, works great when setting an image bitmap like so...
circularImageView.setImageBitmap(bitmap);
However, there are times where I just want to set a solid color instead of a bitmap. If I do something like this,
circularImageView.setBackgroundResource(R.color.blue);
The color of the view is set but the image is never made circular, so it fill the entire rectangular view. I'm assuming the getDrawable() is returning null so it can't actually manipulate the view. Anyone ran into this problem or any suggestions on what to do?
Edit:
I can do this but it seems a bit flimsy:
Bitmap image = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
circularImageView.setImageBitmap(image);
You should call circularImageView.setImageResource(R.color.blue) instead.
The code you wrote sets the background of the view, not the content image. Looking at the code on github, this view will only clip the content image to the circle--it has no effect on the background at all.
Give a try for ColorDrawable;
int decode = Integer.decode("FF6666");
ColorDrawable colorDrawable = new ColorDrawable(decode);
I'm downloading the image from server and storing it in bitmap object. I want to set this image as background for button. But the button doesn't have the property setImageBitmap. So is there anyway I can set the background of button with the downloaded bitmap image? Like by converting the bitmap to drawable? Sorry, I'm new to Android, please bear with my mistakes (if any).
P.S : I want to use button control only. Because I want some text at the bottom of each buttons and I'm creating these buttons dynamically.
The best way to convert a Bitmap to drawable in android is as follows,
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
where bitmap is the name the Bitmap. Then set the drawable as your Button background as follows,
btn.setBackground(drawable);
N.B: Without specifying getResources() as the first argument, you may experience inconsistent image sizing across different screen densities.
for more info refer this
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
Simply use BitmapDrawable.
Drawable drawable=new BitmapDrawable(contact_pic);
Convert Bitmap to BitmapDrawable like this
Drawable drawable = new BitmapDrawable(getResources(),your_bitmap);
and then set it as background to button as ,
button.setBackgroundDrawable(drawable);
P.S. You can also do the same inline as ,
button.setBackgroundDrawable(new BitmapDrawable(getResources(),your_bitmap));
This is how to do it, I've used several times:
Drawable drawable = (Drawable)new BitmapDrawable(Bitmap);
Button button= new Button(this);
button.setBackground(drawable);
I want to create an imagebutton, but the image doesn't fill the imagebutton. The image has the same dimensions as the imagebutton.
The imagebutton has the correct dimension, it seems like the imagebutton has an padding?!
I tried most of the scaletypes.
My code:
ImageButton ib = new ImageButton(Context);
AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams((int)getWidth(3), (int)rowHeight, (int)offsetValue, (int)(offsetRow+row*rowHeight));
Stream bitmap = act.Assets.Open("somefolder/" + name + ".gif");
Bitmap bMap = Android.Graphics.BitmapFactory.DecodeStream(bitmap);
bMap = Bitmap.CreateScaledBitmap(bMap, (int)getWidth(3), (int)rowHeight, false);
ib.SetScaleType(ImageButton.ScaleType.FitXy);
ib.SetAdjustViewBounds(true);
ib.SetImageBitmap(bMap);
bitmap = null;
bMap = null;
ib.LayoutParameters = lp;
layout.AddView(ib);
Yeah it has a padding or something like that, I wanted to create a button in an EditText in order to allow the user to make a password viewable and had the same issue... and haven't found a solution for this until now.
For what you are trying I recomment using an ImageView and make it clickable than you have the same functionality, just like an imagebutton,
Standard 9-patch drawable for ImageButton defined in Android framework has content padding which doesn't allow you to use all space inside the button. To solve this problem, use your own background drawable (set it with android:background="#drawable/..." attribute) or adjust the drawable you set in android:src attribute.
Please read this article for more information on how content padding is defined by 9-patch drawables.