CircularImageView with a solid color - android

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

Related

How to Screenshot programmatically with custom drawable

I'm taking screenshots using the code below.
Inside the layout I am using a custom drawable. The problem is that the bitmap shows everything as expected, except the custom drawable which looks kind of messed up.
I know the problem is related to the drawable. but I don't know what it is.
` View view = app.getCurrentActivity().getWindow().getDecorView().getRootView();
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);`
Thank you for your help, Niv
ok so i figure it out.
the problem was that inside the onDraw in the custom drawable i used the 'canvas.getWidth()' instead of 'getBounds().width();'

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"

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

display image with transparent color at runtime

I try to display a jpg in android-canvas with a specific transparent color.
It works well with a png and I also know how to convert a jpg to png with java so at the end I have a new png-file on the filesystem.
Now my question:
Is there a way to read a jpg file from the filesystem, set a transparent color at runtime (convert to png) and display the image at runtime ?
additional comment:
I try to do this in my custom view with the ondraw method and drawbitmap. I can't use an imageview. :-(
regards
Andreas
don't make the image transparent but the view.
ImageView myImage = (ImageView) findViewById(R.id.img);
myImage.setAlpha(value);
Value is something between 0 and 255. The lower the number, the more transparent the imageview.

Deep copy of a Drawable

I have an ImageView. In its onClick I get its Drawable:
Drawable dr = ((ImageView) v).getDrawable();
And set it to a dialog's ImageView:
zoomedImage.setImageDrawable(dr);
But when I close the dialog or the activity is resumed. The image at the original position gets stretched and is shown larger than its size, leading to only a portion of the image is visible in the ImageView.
Is this a case of deep copy or there is another problem?
If it is, how can do I deep copy the original Drawable so that I could set the copy to zoomed image?
Thanks in advance.
Finally I succeed!
I had similar problem, when I used color filter on my drawable it changed the drawable, its very close to the solution of the other people here, but only this worked for me:
Drawable drwNewCopy = dr.getConstantState().newDrawable().mutate();
I managed to copy the drawable using following code:
drawable.mutate().getConstantState().newDrawable();
Here mutate() makes the drawable mutable to avoid sharing its state, and getConstantState().newDrawable() creates a new copy.
Thus different ImageViews use different drawables and there's no stretching.
Use BitmapFactory to convert the drawable into bitmap separately make or perform changes on it.
The above solutions won't work for me, But it works
val myDrawable = DrawableCompat.wrap(view.background).mutate() as GradientDrawable
myDrawable.setColor(ContextCompat.getColor(view.context, R.color.White))

Categories

Resources