Different rendering when using Drawable and Image Resource? ImageButton - Android - android

I have something strange in here. I have the very same image being loaded in two different ways:
Here is the code:
if(imageAddress.startsWith("drawable")){
btn.setImageResource(this.getResources().getIdentifier(imageAddress, null, this.getPackageName()));
}else{
btn.setImageURI(Uri.parse(new File(imageAddress).toString()));
}
As you can see above, I test if the image address begins with "drawable", if it does, then I load the image as a drawable. If it doesn't, then it's an image from sdcard, then I load it through its URI.
Here is the code for the ImageButton:
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/roundcorners"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight=".95">
</ImageButton>
And below is the RESULT of this:
Above image shows ImageButton when loaded AS DRAWABLE (imageAddress = "drawable/eu_quero")
Above image shows ImageButton when loaded THROUGH IMAGE URI (imageAddress = "/mnt/sdcard/myapp/images/eu_quero.png")
What I need is the images to be the same size, no matter if I load it as drawable or through ImageURI.
Can anyone help me please??
IMPORTANT: IMAGES ARE EXACTLY THE SAME!!! SAME SIZE!!

Can you try implementing android:margin_left instead of padding:left ?

Related

Displaying an image from within an Android app?

I have filepath which is a String holding the absolute path to an image file.
Is there a way to display the image to the user similar to when they use the Gallery app?
When I looked up solutions to this problem, most of them involved setting an ImageView's bitmap to the bitmap decoded from the file, but I don't want to go this route because the orientations may be different, the size of the picture may not be maximized depending on how it fits within the ImageView, and so on. I basically want to have it displayed full-screen. Or if this can be done with an ImageView I'd be curious to know how.
Is this possible to do?
You can declare an ImageView in your layout that fills the parent
<ImageView
android:layout_gravity="fill"
android:id="#+id/your_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Then you use the Picasso library to load the image by
1/ Inflating the ImageView
yourImage = (ImageView) view.findViewById(R.id.your_image);
2/ Loading the image to the ImageView like
Picasso.with(context).load(filepath).fit().into(yourImage);
Check http://square.github.io/picasso/
Use the following library: TouchImageView
Usage:
Place TouchImageView.java in your project. It can then be used the same as
ImageView. Example:
TouchImageView img = (TouchImageView) findViewById(R.id.img);
If you are using TouchImageView in xml, then you must provide the full package
name, because it is a custom view. Example:
<com.example.touch.TouchImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Wrap content doesn't wrap it's content when the picture is cropped

I'm using this to crop an image and then i load it to this imageView:
<RelativeLayout
android:id="#+id/activity_edit_image_relative_layout_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/activity_edit_image_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff2299"
android:scaleType="fitCenter"
android:src="#drawable/phone" />
</RelativeLayout>
the color i'm giving to the background of the image view is pink ( so it will be easy to spot) and the relative layout will act as the view group and i will add other views to it at run time.
the pink background must only appear for the images that have transparent background (png files) like this (this picture is not cropped)
but when i crop an image and load it to the image view i get this
the pink background must not appear. the issue is i'm trying to figure out where on the image is the user taping (overloading onTouch for the imageView) now when i tap on the pink area around the Alien it tells me that i'm tapping the image which is not correct. whats the issue?
Add the following property to your ImageView:
android:adjustViewBounds="true"

android imageview display in bad quality

I try to display 3 images medium size.
The problem is that they display in low quality and it damages the UI a lot,I checked over the internet and the best solution is to use is Piccaso,but I want to know how can I display the image in it's full quality without third-library?
I load images like this:
<ImageView
android:id="#+id/btnring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/itayring1"
/>
and I also tried this:
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.itayicon);
b1.setImageBitmap(bb);
When I used Eclipse I could load images easly. There must be a way to do in Android Studio too,
Thanks.
Put Images in xhdpi drawable image only and try this code :
<ImageView
android:id="#+id/btnring"
android:layout_height="50dp"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/itayring1"
/>

Prevent Image Scaling in Android App

I have an image that I am downloading from the internet in my android application. I do not want this image to be resized/scaled. Is there anyway I can stop the image from being scaled?
Here is my XML entry for the ImageView:
<ImageView
android:id="#+id/conditionImage"
android:scaleType="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textMain"
android:contentDescription="#string/noaaLogoDescription" />
Here is my code for showing it in the image view:
BitmapFactory.Options bmpOptions = new Options();
bmpOptions.inScaled = false;
URL url = new URL("URLPATHTOIMAGE");
ImageView conditionImageView = (ImageView)findViewById(R.id.conditionImage);
InputStream content = (InputStream)url.getContent();
conditionImage = BitmapFactory.decodeStream(content, null, bmpOptions);
conditionImageView.setImageBitmap(conditionImage);
I have tried changing the scaleType in the ImageView XML element to use matrix, center, and centerInside. My last try was using the above code to set inScale to false. Does anyone have any other ideas?
The odd part is this is not happening in the emulator.
Have you put all images in simple "drawable" folder. I mean without extending hdpi,mdpi etc.
Try set
android:layout_width="wrap_content"
android:adjustViewBounds="true"
EDIT:
Not sure why, but changing
conditionImageView.setBackgroundDrawable(getResources().getDrawable(R.id.conditionImage));
to
conditionImageView.setImageDrawable(getResources().getDrawable(R.id.conditionImage));
solves my problem.
Adnan, using your example of giving the image view a fixed width and height and also removing the scaleType=centerImage fixed the issue.
This is a simple problem. The scaling happens when the ImageView is a lot larger than the rendered image itself. To avoid this ugly pixelated scailing, just "wrap content" the imageview and you should get your original image quality.
I resolved this problem by hardcoding height of imageview ( as I know it in advance) and then set the scaleType as cropCenter
see the below code for more clearity.
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:layout_height="75dp"
android:scaleType="centerCrop" />

Swapping Image in ImageView

I've been trying to implement simple (I think) thing in my app. I thought it would be simple, but somehow it doesn't work as I would wanted it to.
What I'm trying to do is:
There is an activity with ImageView in center - it has it's own image (well, there's two of them).
I want to press that ImageView (or it could be ImageButton), open the gallery/take picture with camera.
Then, after taking/picking a picture, I want to make that ImageView display the thumbnail of picture.
My layout
<ImageView
android:id="#+id/add_photo_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/add_photo_button" />
<ImageView
android:id="#+id/add_photo_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:adjustViewBounds="true"
android:src="#drawable/add_photo_button" />
</LinearLayout>
My code - after getting picture path
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/5.png",bmpFactoryOptions);
lastClicked.setImageBitmap(bitmap);
//lastClicked.setImageResource(R.drawable.green_circle);
What I wanted to do is to read the resized file (using inSampleSize) from sdcard, and then fill ImageView (or ImageButton) with it.
But after using setImageBitmap function, the view disappears - like it just loaded empty image (commented line works properly - it's just green dot).
Maybe someone has approached similar problem? I would much appreciate any kind of help.
Greetz,
scana
EDIT:
ofc lastClicked:
lastClicked = (ImageView)findViewById(R.id.add_photo_left);
Why don't you try lastClicked.invalidate() ?
I do so in my code at least and it works this way ;)

Categories

Resources