Xamarin Studio - Imageview - android

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);

Related

Cannot get an Image from ImageButton because returns null

I have an activity which is populated with data from sqlite.
I want to update the data and I click my Update Button, and I see all my data including the image in ImageButton loaded from my sqlite db.
I try to get the already saved and displayed image with
ImageButton Logo;
Drawabable drawableLogo = Logo.getDrawable();
but I get null ! How I am getting null if the Logo imagebutton has an Image ?
What I am forgetting here?
thank you
Use BitmapDrawable to get bitmap from ImageView/ImageButton.
Bitmap drawableLogo = ((BitmapDrawable) Logo.getDrawable()).getBitmap();
ImageButton Logo; // Logo??
Drawabable drawableLogo = Logo.getDrawable();
ImageButton logo;
logo = (ImageButton ) findViewById(R.id.mylogo):
Drawabable drawableLogo = logo.getDrawable();
so
Drawabable drawableLogo = new Drawable():
drawableLogo = logo.getDrawable();

get ImageView src programmatically

How to get ImageView src programmatically and set it in another ImageView
Drawable drawable = imageViewA....?
imageViewB.setImageDrawable(drawable);
You can do something like this:
Drawable drawable = imageViewA.getDrawable();
if(drawable != null){
imageViewB.setImageDrawable(drawable);
}
You can use setImageResource(int)
imageView.setImageResource(R.drawable.bg_image);
Instead of get drawable you do...
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
and set to new image
newImageView.setImageBitmap(bitmap);

URL images are smaller than the original image

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.

What is quick way to check if ImageView has bitmap assigned?

I need to quickly and safely check if an android.widget.ImageView currently has a Bitmap of non zero value attached. What is a quick way to do this.
Update my image view is set initially drawable set to "logo.png" so if I know that it is currently set to logo.png than I know that the bitmap has not yet been set. So how can I check if background drawable is currently logo.png
Thanks
Use getDrawable() method, if it return null then nothing assigned
if( mImageView.getDrawable() != null){
//Image Assigned
}else{
//nothing assigned
}
Assign ImageView to variable in onCreate:
someImage = (ImageView) findViewById(R.id.imageView);
Then a simple if-else statement:
Bitmap bm = ((BitmapDrawable) someImage.getDrawable()).getBitmap();
boolean hasDrawable = (bm != null);
if(hasDrawable)
{
hasImage();
}
else
{
Toast.makeText(AppSettings.this, "No Bitmap", Toast.LENGTH_SHORT).show();
}
Kotlin Extension Solution:
Add this to simplify checks and make it more readable
fun ImageView.hasDrawable() = drawable != null
and then call
myView.hasDrawable()
Get Bitmap attached to ImageView
Check this out.
It looks like you have to create a bitmap from the ImageView and check to see if it is null.
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);

Copy Bitmap contents of one ImageView to anoher

This has me baffled. I need to copy the Bitmap from one ImageView into another. I do not want to simply copy one ImageView to another because I need to do some changes to the bitmap on its way over.
Here is some code that doesn't work.
ImageView ivSrc = (ImageView) findViewById(R.id.photo);
ivSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc1 = ivSrc.getDrawingCache(); // will cause nullPointerException
Bitmap bmSrc2 = Bitmap.createBitmap(ivSrc.getDrawingCache());//bmSrc2 will be null
View vSrc = (View) ivSrc.getParent();
vSrc.setDrawingCacheEnabled(true);
Bitmap bmSrc3 = Bitmap.createBitmap(vSrc.getDrawingCache()); //black bitmap
//To test the bitmaps:
ImageView ivDest = (ImageView) findViewById(R.id.photo2);
ivDest.setImageBitmap(bmSrc1); //bmSrc1, 2, 3 results shown above
I have to going about this wrong because doing a copy should be so easy. TIA
Not used the drawing cache, but wouldn't you need to call buildDrawingCache() ?
The way I'd do it:
Bitmap bmSrc1 = ((BitmapDrawable)ivSrc.getDrawable()).getBitmap();
Bitmap bmSrc2 = bmSrc1.copy(bmSrc1.getConfig(), true);
Note that bmSrc2 is mutable, i.e. you can stick it in a Canvas and do whatever you like with it before drawing it somewhere.

Categories

Resources