Android make bitmap opaque against background color - android

Given a bitmap, I know how to make an ImageView more transparent by setting alpha. But I want to also set a white background color (different from the view its sitting on), so it appears more opaque than transparent.
Referring to the image below,
I get the left pic using , ImageView.SetBitmapImage(bitmap).
Using ImageView.SetAlpha = 0.5, I get the middle pic
but how do I get the right pic.
Also, in my axml, I have set the ImageView background color to white, but it still doesn't help...I still get the middle pic.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" />

I solved this by wrapping the ImageView in a LinearLayout, where the backgroundcolor was set to white. Then the bitmap alpha was fine

Related

How to replace color by another on ImageView in Android?

I would like to change (map) white color to blue color. How can I do this on ImageView in Android? I tried setColorFilter by PorterDuff / LightingColorFilter / ColorMatrixColorFilter but I can't figure out how to set it up. There is a transparent background around the image.
Try to use Background Tint color function in xml or java code.
or
I would suggest another way. Take on frame layout and place view and then image view.
white portion in image should be transparent use PNG image.
and you can give color to base view color which ever you want.

Button ripple effect spread to cover only alpha image OR avoid covering transparent spots

What I have read previously
Add ripple effect to my button with button background color?
Android ripple for button not working
Ripples on a shape with a transparent background
Transparent background in ImageButton with ripple effect?
RippleDrawable - dev
What I need is to make custom ripple effect cover only alpha channel of my image OR avoid covering transparent spots.
My original botton
<Button
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center"
android:background="#drawable/btn_round"
android:foreground="#drawable/btn_ripple"/>
What I have tried from suggested with button
android:background="?attr/selectableItemBackgroundBorderless‌​"
android:foreground="?attr/selectableItemBackgroundBorderless‌​". It did not quite work. In fact it ignores borders of my image and shape set in ripple whilst I need to constrain ripple spread shape to alpha channel.
As you already can see I have 3 options to combine
custom button shape
custom ripple
cutting-off alpha channel from suggested options
So I tried transforming button into imageButton
<ImageButton
android:layout_width="96dp"
android:layout_height="96dp"
android:layout_gravity="center"
android:background="#drawable/btn_round"
android:foreground="#drawable/btn_ripple"
android:scaleType="fitXY"/>
However I have tried all combinations of src, background and foreground for all 3 and did not succeed. Also tried adding
android:background="?attr/selectableItemBackgroundBorderless‌​"
android:foreground="?attr/selectableItemBackgroundBorderless‌​" to ripple. That doesn't work either.
Reference this picture (that one does not have alpha but that is not the case)
Solution was plain to see:
in ripple.xml
<item
android:id="#android:id/mask"
android:drawable="#drawable/btn_enable" />
without < shape > inside
Add a mask to the ripple effect. Only the alpha channel of the mask is used so it can be the same as your background image.
var rippleColorLst = android.content.res.ColorStateList.valueOf(
android.graphics.Color.argb(255,50,150,255)
);
var ripple = new android.graphics.drawable.RippleDrawable(
rippleColorLst,
yourImageDrawableWithAlpha,
yourImageDrawableWithAlpha // the 3rd parameter is mask
);
yourButtor.setBackground(ripple);
(Sorry for the JavaScript/NativeScript code, hope everyone can understand it.)

Bitmap otained is transparent but when set to imageview becomes non-transparent

I'm using OpenCV to implement something simple, that if Mat has color of pixel(x,y) equal to white, it turns to transparent (alpha = 0). After that convert Mat to bitmap.
When debugging the bitmap returned is correct, but when i set it to imageview, the image returned still has white background (as in the original image) instead of the transparent image.
This is the original image (with white background)
And debugging image, with transparent background
Please help me to fix it :(
Add following line in your ImageView,
android:background="#android:color/transparent"

Set background behind image

I got a picture that I want to use.
I set it as following:
ImageView menu = new ImageView(this);
menu.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT));
menu.setImageResource(R.drawable.menu);
They annoying thing is that I get white pixels on the sides of it cause I want to keep the aspect of the pic.
I can stretch the image by using menu.setScaleType(ScaleType.FIT_XY); but that will make the person on it look really fat. The picture is dark and the pixels are white so they do show quite well. :/
Is there I way I can first apply black color and then the image above that color?
To set a background color to any view on android you can use android:background attribute in layout xml or by calling setBackgroundColor(int id) in java code.
But if you really want just to set the image in bounds you can give a try to android:scaletype="centerCrop"
Using set BackgroundColor will also remove any padding, borders and what not attached to the object.
If that is the result you want, that is a reasonable approach.
If blasting the objects current style will cause problems, I would look to use CSS to set the background color and change the css styles through code if things are happening after page load.
Consider using a border around the image that is colored the way you want to hide what is underneath?

Android: 9-patch image not stretching as expected

The actual image is this:
the red boxex are where i made 1 px black likes. Even the preview shows fine. So no problem with 9-patch
but the image i get for the following layout is:
<ImageView
android:id="#+id/image_landing"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/loginheader"
android:contentDescription="#string/login_header"
/>
I Expected, the logo to be on the left and black dots on the right and the rest of the space between them is filled with grey color i selected on the top
Thank You
Is your ImageView really bigger than your 9patch?
If not, you need to change scaleType as defaut is FIT_CENTER.
Use android:scaleType="fitXY" or android:adjustViewBounds="true".
It looks like your 9 patch is larger than the ImageView that you are trying to put it in. Try setting the ImageView to wrap_content to see if it fixes the problem. If it does, try making the 9 patch smaller, the ImageView bigger or set the scaleType as pcans mentioned.

Categories

Resources