Displaying Saved Bitmaps in Android (LinearLayout) - android

I have 2 bitmaps that I saved with view.getDrawingCache(); called firstBitmap and secondBitmap. They were saved from initial drawings made by the user. Now I want to display them. My issue is that only the first picture is displayed by itself (I want both to be shown at the same time). I know that the bitmaps are correct because if I remove layout.addView(pic1), I can see the second image by itself clearly. layout is a LinearLayout.
My suspicion is that the size is an issue but I believe that by default views that are added to the layout have fill parent so this should still result in 2 images not one. I have tried dynamically changing the width and size with LayoutParams but this makes the image disappear entirely. I am open to any suggestions.
ImageView pic1 = new ImageView(this);
pic1.setImageBitmap(firstBitmap);
layout.addView(pic1);
ImageView pic2 = new ImageView(this);
pic2.setImageBitmap(secondBitmap);
layout.addView(pic2);

Try to distribute the weight in your linear layout so that both bitmaps will be visible.
you could use the below code to do that.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height, weight);
layout.addView(pic1, params);
layout.addView(pic2, params);

Related

Android ImageView: Replace Image (Drawable) While keeping View Size

In Short
I have an ImageView with size set to wrap_content.
The image has a drawable already set, and everything looks great.
Now, I want to change the drawable, while forcing the ImageView to keep it's size.
Explanation
Well, the easy answer is of course to set the width & height to a fixed size.
But in my case, I am copying an ImageView + layout from a Google Map to add my custom map button. It works well, because I have an icon with same size.
Now I want to change the icon for a moment (loading icon btw), but because the Drawable source is larger, it changes the ImageView size accordingly.
ScaleType doesn't work as the width and height are not fixed.
What I've Tried
1. Setting the new Drawable bounds using getBounds on the old one.
2. setRight() on the ImageView with the right bound of the existing map button (the original one which doesn't change).
(left is already fixed, it's inside a RelativeLayout).
Some (working) Code of how I created the new button
ImageView newBtn = new ImageView(context);
newBtn.setAdjustViewBounds(false);
newBtn.setBackgroundResource(R.drawable.map_btn_background);
newBtn.setScaleType(ScaleType.CENTER_INSIDE);
newBtn.setImageResource(imageResId);
newBtn.setClickable(true);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationBtn.getLayoutParams();
rlp = copyRelativeLayoutParams(rlp);
//add to Google's map button layout
btnContainer.addView(newBtn, rlp);
Some (not working) Code of how I change the drawable.
This is the point were I want the ImageView to keep it's size.
perfectlyMadeNewMapButtonImageView.setImageDrawable(mLoadingDrawable);
//mLoadingDrawable.setBounds(
// perfectlyMadeNewMapButtonImageView.getDrawable().getBounds());
// not working

Create and remove image at certain coordinates on android

I have an XML Relative layout, with just a few textviews and buttons on it. But I would like to place an image (or sometimes multiple copies of the image) at different x and Y coordinate which is worked out later on.
Unfortunately I can't seem to figure out how to create the ImageView in android (besides XML) and make it appear at the desired coordinate.
Also it would be great if I could help in making it disappear or removing it at a later stage as well.
You can create a ImageView programmatically with ImageView iv = new ImageView(context); Then you have to set the LayoutParameters for the view. If you plan to add the view to a RelativeLayout you must use RelayiveLayout.LayoutParams. So you can have to do the same as you know from xml: add layout rules. See the documentation.
Then overall something like this:
ImageView iv = new ImageView(context);
RelayiveLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
// TODO set the params
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
iv.setLayoutParams(params);
relativeLayout.addView(iv);
To hide the imageView you can user imageView.setVisibility(View.GONE)

Resize ImageView in run time using a button - Android

I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update
resize is just for display, the original resource remains as it is. NO editing, save, etc
xml layout
<ImageView
android:background="#drawable/picture01"
android:id="#+id/picture01holder"
android:layout_below="#+id/TextView01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
</ImageView>
main.java
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
now what do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.
basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java
change the Imageview size by taking LayoutParams
final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
pic01.setLayoutParams(layoutParams);
First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.
myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200);
myImageView.setLayoutParams(params);
Or you can set the components minimal size:
myImageView.setMinimumWidth(200);
myImageView.setMinimumHeight(200);
In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.
RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere);
pic01.setLayoutParams(myParams)

Resize all ImageViews in a layout

I have four ImageViews in a layout. I set the maximum width for each one of them by setMaxWidth function. The problem is that they keep a small distance between them and I don't want that. If I don't stipulate the maximum width, these gaps disappear, but the ImageViews sizes are inappropriate (bigger than it should be).
The main code is:
musicSubmenuButton = new ImageView(context);
musicSubmenuButton.setId(id++);
musicSubmenuButton.setImageResource(R.drawable.bt_musicas_on);
musicSubmenuButton.setMaxWidth(buttonSize);
musicSubmenuButton.setAdjustViewBounds(true);
photosSubmenuButton = new ImageView(context);
photosSubmenuButton.setId(id++);
photosSubmenuButton.setImageResource(R.drawable.bt_fotos_off);
photosSubmenuButton.setMaxWidth(buttonSize);
photosSubmenuButton.setAdjustViewBounds(true);
agendaSubmenuButton = new ImageView(context);
agendaSubmenuButton.setId(id++);
agendaSubmenuButton.setImageResource(R.drawable.bt_agenda_off);
agendaSubmenuButton.setMaxWidth(buttonSize);
agendaSubmenuButton.setAdjustViewBounds(true);
infoSubmenuButton = new ImageView(context);
infoSubmenuButton.setId(id++);
infoSubmenuButton.setImageResource(R.drawable.bt_info_off);
infoSubmenuButton.setMaxWidth(buttonSize);
infoSubmenuButton.setAdjustViewBounds(true);
The size I wanted, but without the gaps (obtained using the above code):
The ImageViews without the gaps, but in the original resolution (obtained using the above code, but commenting the setAdjustViewBounds calls):
Does anybody know why these gaps appear and/or a workaround to get rid of these gaps and keep my desired size?
Instead of setting max width, set layout_width on them to fill_parent. Then they will space out evenly.

Android - how to add a dynamic image to activity

I currently have an activity that does not have an xml layout file. All items are added when needed. I am not in need to add a image to it and it doesnt seem to do anything...
ImageView iv = new ImageView(this);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/Pics/Pic_1.jpg");
iv.setImageBitmap(bm);
ll.addView(iv);
The image is in the correct folder...11 at the end is the Linear view i am adding the image view too...am i missing something?
You can use debugger to see if image is loaded correctly.
And have you tried to set layout params on that ImageView. Because in your case it wouldn't take any space (I think). Try something like:
ll.addView(iv, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Or use exact sizes instead of LayoutParams.WRAP_CONTENT

Categories

Resources