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.
Related
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();'
I am using Picasso to resize my background images on my Android interface.
I am stretching the width of the image to fill the width, but I cannot adjust the height, as the background image needs to line up with the circular image in the middle.
Problem is that on different devices, the height appears to adjust as the background is not positioned consistently. I would post an example but I can't as I dont have 10 reputation yet.
Here is my java
ImageView imgBackground = (ImageView) v.findViewById(R.id.imgBackground);
aParent = (MainActivity) getActivity();
//Picasso.with(getActivity()).load(R.drawable.morn_blur_bg).fetch();
Picasso.with(aParent.getApplicationContext())
.load(R.drawable.morn_blur_bg)
.resize(aParent.getBackgroundWidth(), aParent.getBackgroundHeight())
.into(imgBackground);
And this is my XML
<ImageView
android:id="#+id/imgBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_alignParentTop="true" />
Any help is greatly appreciated.
It turns out that the imageview was stretching the image after picasso did its thing.
I tried to set the imageview height, but the OS overrode me.
In the end I set imageView.scaletype="matrix", and now it does not stretch my image.
hope that helps someone else, i've spent a lot of time on this.
In my case, I put android:scaleType="centerCrop" on my ImageView and then no need to apply centerCrop() through Picasso.
I am having two different circular images with different sizes.I have to place both in same place where the center point will be same.These two imageviews are in a relative layout.
please help..
When using a relative layout the only option to do that is to center both images in the layout, but if you start adding more elements, such as some text above/below any of the images, the result will not be as expected.
So my recommendation is to do it programatically. You can define a View and override the onDraw() method. Then you would load the bitmaps by means of two ImageView (or BitmapFactory). Then you paint it to the canvas at the desired location. To find out the center of each image you can use the Rect class that you obtain from the View method getDrawingRect after you apply the layout properties (so the size is calculated),, or by hand (create a Rect with de dimensions of the loaded Bitmap if you use BitmapFactory)
Other alterative is using LayerDrawable and define the image positions so they are centered (you need to know the image dimensions before hand).
Difficult to help without your layout XML code. Having said that, the moment you see "one on top of the other", you need to consider FrameLayout.
Use Framelayout, and set the background for the same with an image and then add images on top with setting layout gravity to left or right or middle.
In stead of creating a new sublayout, you can potentially have the ImageViews align on all sides and set the scaleType attribute to something like center:
<ImageView
android:id="#+id/circle_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/..."
android:scaleType="center"/>
<ImageView
android:id="#+id/circle_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/..."
android:layout_alignTop="#+id/circle_a"
android:layout_alignBottom="#+id/circle_a"
android:layout_alignLeft="#+id/circle_a"
android:layout_alignRight="#+id/circle_a"
android:scaleType="center" />
This will give the circle_b ImageView actually the same dimensions as circle_a, but setting the appropriate scaleType will prevent the image from being stretched or misaligned.
//Edit: ups, I meant to say center... corrected.
I have a vew, received by:
(ImageView) findViewById(R.id.photo)
That view currently has a bitmap on it. I want to add another bitmap, at the corner. The bitmap is retreieved by v.getDrawingCache(), from the listener I am currently in.Right now I have a code that completely replaces the image in the 'photo' view:
OnClickListener mImageListener = new OnClickListener() {
public void onClick(View v) {
v.buildDrawingCache();
((ImageView) findViewById(R.id.photo)).setImageBitmap(v.getDrawingCache());
}
};
Doesanyone know how to just add the image? The answer on Draw Bitmap When Still Using XML View
didn't really work for mefor some reason :(
Note: The bitmap I am adding will be moved around later on using a MoveListener or something. Just in case that was important...
I decided to use a SimView instead of an ImageView, it solved all the problems. Thanks for the help!
You can only associate one bitmap to one ImageView, it is a one to one mapping.
If you want another picture overlay'ed on top of the existing image then you will need to have both of them within a FrameLayout.
Also it will be easier for you to move around the smaller bitmap later on if it is in a different ImageView, acts as two different UI elements.
Sample pseudo xml
<RelativeLayout fill parent>
<FrameLayout fill parent>
<ImageView center in parent>
<ImageView align bottom right = true>
</FrameLayout>
</RelativeLayout>
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.