add a bitmap to a view in Android - android

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>

Related

IMAGE VIEW ANDROID STUDIO

I have a doubt. While working on an App , I came across this query.
When an Imageview1(smaller size) is dragged and dropped on to Imageview2(bigger size), how to make Imageview1 (fit the area of imageview2), that is, enlarge the Imageview1 automatically to the size of Imageview2.
Instead of doing something so twisted, you can try a simpler solution like:
ImageView2.setImageDrawable(ImageView1.getDrawable());
ImageView1.setVisibility(INVISIBLE); //GONE could be used too, depending on the situation
Overwriting ImageView2 would involve many other operations like moving ImageView1 inside the layout, setting new LayoutParams, etc.
EDIT
Use this:
ImageView1.buildDrawingCache();
Bitmap src = ImageView1.getDrawingCache();
BitmapDrawable destDrawable = new BitmapDrawable(Bitmap.createScaledBitmap(src, ImageView2.getWidth(), ImageView2.getHeight(), false));
ImageView2.setImageDrawable(destDrawable);
In general, it is not a good practice to have views cover other views.
When you do the drag action try this
ImageView.ScaleType(FIT_XY);

Screenshot of layout with circular image view makes the image cut

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 to get an imageView on Full Screen in Android?

I know there are lots of questions like these. However, they all seek to bring imageView on full screen after clicking.
I want to bring an imageView on Full Screen without clicking on it. In other words, when I click my bitmap, I should get a full-screen imageView. I tried a lot but could not achieve desired results.
So far, I have tried this thread's all possible solutions.
Moreover, I tried to use multiple scaling options.
I am self-learning android using a big nerd's ranch guide. I am attaching my code and images below
public View onCreateView(LayoutInflater inflater,
ViewGroup parent, Bundle savedInstanceState) {
imageView=new ImageView(getActivity());
String path= (String) getArguments().getSerializable(FILE_PATH_NAME);
BitmapDrawable image = PictureUtils.getScaledDrawable(getActivity(), path);
float rotation= rotateImage(getActivity(), path);
Log.i("RotateImagee", "Rotate valuee: " + rotation);
imageView.setRotation(rotation + 90);
imageView.setImageDrawable(image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
return imageView;
}
Images:
I want screen fully covered with this image with a proper scaling. Therefore, please show me an appropriate solution with proper scaling.
You can use only one scaletype and CENTER_INSIDE overrides FIT_XY.
Use only FIT_XY.
Senconly when you create your ImageView you don't give it layoutparams(default is wrap_content), so you need to set layoutparams to match_parent. Without it imageView will change size to wrap_content and FIT_XY will not take affect.
Here is a link if you want to make activity full screen.
you also need to remove ActionBar, Here.
The fact that the background and Toolbar are greyed-out makes me think you are displaying your ImageView inside a DialogFragment, not a normal Fragment. DialogFragments are a bit different. Getting them to display full screen is a non-trivial task. If you are not trying to display a Dialog then I suggest using a normal Fragment to display the ImageView.
Side notes:
consider rotating the Bitmap, not the ImageView
consider putting the ImageView in a layout file and inflating that layout file (you already have a LayoutInflater passed to you in onCreateView)

Two circular images on top of one another

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.

Really need help figuring out why a bitmap is placed on an an image_view incorrectly

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.

Categories

Resources