I try to show a simple bitmap with below code snippets.
imageView.setImageBitmap(bitmap.extractAlpha());
ImageView visible as empty.
If your original bitmap is opaque then extractAlpha will return a mask that is basically black.
By default your view's background is black so in your case you are setting black on black which is why you can't see it.
Try setting your background to white and see what happens. Something like this:
imageView.setBackgroundColor(Color.WHITE);
imageView.setImageBitmap(bitmap.extractAlpha());
Related
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.
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"
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 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?
Earlier, I was able to dynamically create an android.widget.Button whose background color was visible through the transparent parts of the Button's background image. I have not been able to re-create this. I have tried:
Button button = (Button) findViewById(id.button1);
try {
button.setBackgroundColor(Color.RED);
Bitmap bm = BitmapFactory.decodeStream(getAssets().open("transparent.png"));
button.setBackgroundDrawable(new BitmapDrawable(bm));
button.invalidate();
} catch (IOException e) {
throw new RuntimeException(e);
}
Only the image is visible if I run the above. If I move the call to setBackgroundColor below the call to setBackgroundDrawable, I only see the red, not the image. How can I make it so I can see both the image and, through its transparent parts, the background?
First it is easier to use an ImageButton, which has two layers, one is the background and the other the image on top.
Set your background color with button.setBackgroundColor
then set the transparent image on top: button.setImageDrawable(getResources().getDrawable(R.drawable.transparent)); or set SRC preperty in XML
Use the button padding settings to adjust how much backround color should go around the image.