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();'
Related
I have a relative layout in my app which im trying to take screenshot of. The problem is that it has CircularImageView in it (https://github.com/lopspower/CircularImageView) and the whole image is transformed and no longer cropped to center.
The code goes like this:
View rl = findViewById(R.id.toBeScreenShot);
rl.setDrawingCacheEnabled(true);
Bitmap screenshot = Bitmap.createBitmap(rl.getWidth(),rl.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
rl.draw(c);
rl.setDrawingCacheEnabled(false);
And this is the result:
The XML code is too big to share, there is a Relative layout that im taking screenshot of (in code, its 'rl'). Then there is a FrameLayout, then two LinearLayouts, some ImageViews and then there is the CircularImageView:
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/userPicture"
android:layout_width="#dimen/_120sdp"
android:layout_height="#dimen/_130sdp"
android:layout_gravity="center"
app:civ_border="false" />
Any idea what might cause this thing to happen?
So far I've been able to figure out that the bug must have something to do with the library that I'm using. So I tried to figure out some other way to create circular image, and I found this answer from Waza_Be:
https://stackoverflow.com/a/14180142/6148510
After some short testing, the screenshot of this circular bitmap works properly. If there will be no answer to my problem, I will mark this workaround as a correct answer.
How can I create an ImageView programmatically and draw on canvas in Android?
I also saw this post of-canvas and tried the below code:
ImageView img = new ImageView(HighScore.ctx);
img.setImageResource(R.drawable.interactive);
img.draw(canvas);
img.bringToFront();
img.invalidate();
img.draw(canvas);
But the answer doesn't seem complete, it's not working. plz can anybody help me..
Refer to this link: http://developer.android.com/reference/android/widget/ImageView.html#lattrs
Here equivalent methods of xml attributes for imageview are given.
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 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))
this problem has been bothering me for days and I cannot figure out why on earth it is happening. I have a method that detects for a face, if a face is detected, the method will draw a rectangle on a canvas along the face.
That part works fine.
The problem is, when it displays to the image_view in my xml file, it will display on the far left middle of the image view, in a box, rather than in the center with the width as fill_parent.
I thought of a possible workaround : to set the bitmap as a bitmapdrawable, but canvas only draws to bitmaps, so I can get the bitmapdrawable in the middle, but with no red box around it.
I commented out my testing of bitmap drawable, and left it as what I have now...it only displays to my xml in the far left of the image_view. Please take a look and help!
I posted to pastebin because the code is a bit long (I am also using a linear layout within a view flipper).
method:
http://pastebin.com/KQ5Pmx45
xml:
http://pastebin.com/jbt3j40h
Since I am able to get the faces to detect, and I can paste a bitmap drawable to an imagebutton and it centers just fine, is there a way to get the rectangle around the bitmapdrawable if my problem cannot be solved?
Thanks!
EDIT: I tried to implement it into a RelativeLayout and still doing the same thing:
http://pastebin.com/daqMdXjB
Could this code be getting my way of centering?
public void processCameraImage(Intent intent) {
mFlipper.setDisplayedChild(1); // 1 == your second layout
mThePicture = (ImageView) findViewById(R.id.image_view); //
cameraBitmap = (Bitmap) intent.getExtras().get("data");
mThePicture.setImageBitmap(cameraBitmap);
detectFaces();
}
Use on Imageview tag android:scaleType="fitCenter" or android:scaleType="centerInside" or android:scaleType="center" sclaetype attr doc
I hope, I understood your problem right.