I am using xamarin studio. I have to download a bunch of images from a web service. I only get their url and then I use the following code to make the images bitmap
SetContentView (Resource.Layout.test);
ImageView img = FindViewById<ImageView> (Resource.Id.image);
Bitmap bitimage = GetImageBitmapFromUrl ("http://apk.payment24.co.za/promotions/engensa/muffin.png");
img.SetImageBitmap (bitimage);
public static Bitmap GetImageBitmapFromUrl(string url)
{
Bitmap imageBitmap = null;
using (var webClient = new WebClient())
{
var imageBytes = webClient.DownloadData(url);
if (imageBytes != null && imageBytes.Length > 0)
{
imageBitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
}
}
return imageBitmap;
}
The problem is the image I display is very small. If I manually download the images from the url and add it to the project it works perfectly fine.
My image view settings are with: match_parent and height: wrap_content.
How can I get the original size of the images if I download it via url.
Please see this link to see how the image look http://postimg.org/image/c3kbsz903/
You need to add android:adjustViewBounds ="true" and android:src="#drawable/ attribute in your Imageview
<ImageView
layout_width="match_parent"
layout_height="wrap_content"
android:adjustViewBounds ="true"
android:src="#drawable/ dummy drawable/>
Please Check ScaleType in android .
You stated that your view attributes are something like this
<ImageView
layout_width="match_parent"
layout_height="wrap_content" />
In order to have the original size of the image, you must set the layout_width attribute of your ImageView to wrap_content so that the ImageView won't use its parent's width but will adapt to the image original size.
Related
I'm trying to load a photo (downloaded via a url) into an imageview and have it span the entire width of its parent view, which is a row in a listview.
<ImageView
android:id="#+id/property_row_photo"
android:layout_width="match_parent"
android:layout_height="#dimen/search_list_item_height"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
Setting the scaleType directly on the ImageView seems to do nothing. I know the height of each row, so I'm measuring the width and then using that to call Picasso's Resize and CenterCrop methods like so:
var height = Convert.ToInt32 (ApplicationContext.Activity.Resources.GetDimension (Resource.Dimension.search_list_item_height));
viewHolder.Photo.Measure (0, height);
var width = viewHolder.Photo.MeasuredWidth;
var url = property.ThumbnailPhotoUrl.Replace ("/thumbnails/", "/{0}/{1}/".WithFormat (width, height));
viewHolder.Photo.SetUrlBitmapWithCenterCrop (url, width, height,
loadingResourceId: Resource.Drawable.property_row_background,
errorResourceId: Resource.Drawable.image_not_available);
public static void SetUrlBitmapWithCenterCrop (this ImageView imageView, string url,
int width, int height, int? loadingResourceId = null, int? errorResourceId = null) {
Picasso.With (imageView.Context)
.Load (url)
.Placeholder (loadingResourceId ?? 0)
.Error (errorResourceId ?? 0)
.Resize (width, height)
.CenterCrop ()
.Tag (imageView.Context)
.Into (imageView);
}
Unfortunately, the centerCrop scaleType isn't being applied, I see big white borders to the left and right of each photo. Any idea what I'm missing? Note that I've tried the Fit method as well, no luck.
try using both fit and centerinside
Picasso.with(getContext()).load(path).fit().centerInside().into(iv);
The image I want to do is get the shape of the imageview before it comes to the screen.
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"/>
I did it this way but the imageview does not appear because the height is wrap_content before the image is loaded. The image expands after being uploaded, but I want to get the image size without loading the image.
I assume that you are getting the image URL / path from the server. Like, http://......
Then, you will definitely get the height and the width of the image from its path / URL by converting it into the Bitmap.
Try this:
try {
URL url = new URL("http://....");// here put your string URL comes from server
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
int width = b.getWidth(); // here is the your image width
int height = b.getHeight(); // here is the your image height
} catch(IOException e) {
System.out.println(e);
}
with below piece of code I try to set an ImageView flexible:
var FindImage ="#drawable/Tabulator_E1_" +P2.ToString ();
var imageView = FindViewById<ImageView> (Resource.Id.imageView1);
imageView.SetImageBitmap (FindImage);
In my #drawable I have several bitmaps such as Tabulator_E1_1, Tabulator_E1_2 etc
Unfortunately SetImageBitmap doesn't take a string. I am looking for a way to do this.
use BitmapFactory.DecodeFile() to load a bitmap from a string path
var imageView = FindViewById<ImageView> (Resource.Id.imageView1);
var bitmap = BitmapFactory.DecodeFile(bitmap_path);
imageView.SetImageBitmap (bitmap);
I want to create my entire app's UI using Droid.Dialog in MvvmCross. More specifically setting the background image and then adding a header image to the view, as well as input fields and buttons.
I am stuck trying to get a header image into the view. Firstly here is a wireframe explaining what I mean by header image:
I have managed to set the background gradient image using:
using (var drawable = Resources.GetDrawable(Resource.Drawable.Backgroud))
Window.SetBackgroundDrawable(drawable);
I have then tried to add the header image using the Cirrious.MvvmCross.Dialog.Droid stuff like this:
var img = new ImageView(this.BaseContext);
img.LayoutParameters = new Gallery.LayoutParams(330, 110);
img.SetImageResource(Resource.Drawable.Header);
Root = new RootElement()
{
new Section(){new ImageElement(img)}
};
However, the image appears as a tiny blotch in the UI.
Doing this in AXML I would set the image as follows:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:layout_width="330dp"
android:layout_height="110dp"
android:src="#drawable/Header" />
</LinearLayout>
I'm basically stuck trying to figure out how to get the image into the view at the top of the screen and with the right dimensions!
Any help would be greatly appreciated. Or am I better off creating the views using the native AXML and IB respectively?
If you look what is inside the ImageElement and what happens when you call the ctor with the ImageView argument, then you will discover that the image is scaled to 48x44.
https://github.com/MvvmCross/MvvmCross/blob/v3/CrossUI/CrossUI.Droid/Dialog/Elements/ImageElement.cs#L52:
private const int dimx = 48;
private const int dimy = 44;
...
private ImageView Scale(ImageView source)
{
var drawable = (BitmapDrawable) source.Drawable;
var bitmap = drawable.Bitmap;
var bMapScaled = Bitmap.CreateScaledBitmap(bitmap, dimx, dimy, true);
source.SetImageBitmap(bMapScaled);
return source;
}
So you have a couple of options:
Create your own Element, where you are in the control of what is done with the ImageView
Do it by creating a native Android Layout instead.
For some reason, when I create a bitmap from an image in asset, it does not display fullscreen. When I take an image from drawable, resId, the image shows fullscreen. Why is this?
//the image is not fullscreen
ImageView iv = new ImageView(getBaseContext());
iv.setImageBitmap(bitmapFileFromAssetorSD);
//this makes the image fullscreen
//iv.setBackgroundResource(resId);
iv.setLayoutParams(
new LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT
)
);
Try setting the appropriate ImageView.ScaleType
ImageView.setScaleType(ImageView.ScaleType.CENTER_CROP)