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 ;)
Related
I am using Picasso to resize my background images on my Android interface.
I am stretching the width of the image to fill the width, but I cannot adjust the height, as the background image needs to line up with the circular image in the middle.
Problem is that on different devices, the height appears to adjust as the background is not positioned consistently. I would post an example but I can't as I dont have 10 reputation yet.
Here is my java
ImageView imgBackground = (ImageView) v.findViewById(R.id.imgBackground);
aParent = (MainActivity) getActivity();
//Picasso.with(getActivity()).load(R.drawable.morn_blur_bg).fetch();
Picasso.with(aParent.getApplicationContext())
.load(R.drawable.morn_blur_bg)
.resize(aParent.getBackgroundWidth(), aParent.getBackgroundHeight())
.into(imgBackground);
And this is my XML
<ImageView
android:id="#+id/imgBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_alignParentTop="true" />
Any help is greatly appreciated.
It turns out that the imageview was stretching the image after picasso did its thing.
I tried to set the imageview height, but the OS overrode me.
In the end I set imageView.scaletype="matrix", and now it does not stretch my image.
hope that helps someone else, i've spent a lot of time on this.
In my case, I put android:scaleType="centerCrop" on my ImageView and then no need to apply centerCrop() through Picasso.
I have an Image of size 1000px*1200px and i want it to be displayed without being scaled!.
I have searched in stackover flow and google i am not getting good one.
I want full image without scaled,
I have tried like this
Step1:-
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
Step2:-
I have tried with scaleType also
add a scaleType to your ImageView
<ImageView
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
But this is also not working,
In sclate type i have tried with all possible properties (matrix,fitXY like this).
is there any possibility there for doing to dispaly original image in android?
Plz help me on this
You can try this approach,first get image from drawable and create a bitmap of it.Then get width and height of bitmap, resize your image view and then display image in it.It will create imageview with your original image size.
See below code -
BitmapDrawable bitmap = (BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int bitmapHeight= bitmap .getBitmap().getHeight();
int bitmapWidth = bitmap .getBitmap().getWidth();
Now you have dimension of your original image.
Re-size image view -
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.getLayoutParams().height = bitmapHeight;
imageView.getLayoutParams().width = bitmapWidth;
and in last step set bitmap to your image view -
imageView.setImageBitmap(bitmap);
Hope it will help you.
EDITED
Use below code after resize image view -
replace - `imageView.setImageBitmap(bitmap);`
by
imageView.setImageResource(R.drawable.icon);
By your comments, I think what you need is a library like PhotoView.Hope it can help you
Although I would highly condemn such UX but whatever works for you.
1) Use the following link to make a two dimensional scroll.
Two dimensional scrolling... a suggestion that needs feedback
2) Add an imageview to the same and set your image dimensions for the imageview in px at runtime.
as far as i understand your question is that u want natural size of image without ImageView tag modifying it. This worked for me
<LinearLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/image_here"
android:orientation="horizontal|vertical"/>
let me know in the comments.
Make your image layout width and height to wrap content. Make scaleType centre. Check your device width and height through device manager then give image height and width in %
After many trails i have find a solution for above question.
Here is the code:
MainActivity:-
public class MainActivity extends ActionBarActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) this.findViewById(R.id.imageview);
}
}
activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="#+id/ScrollView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView android:id="#+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/imageview"
android:src="#drawable/desert"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:adjustViewBounds="true">
</ImageView>
</HorizontalScrollView>
</ScrollView>
This is working perfectly for my requirement.
Hope this helps to others.
I am loading an image from the web with a AsyncTask class and set it to a imageview in my code.
// loading of the image into the ImageView
new DownloadImageTask(MyImageView).execute("ImageURL");
the problem is, every time I load an image, the height of it is different, even though the width and height of the ImageView is warp_content.
I added sort of a border line with the Background and padding to see the actual height of it.
Here is the xml
<ImageView
android:id="#+id/ivBigRecipeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/ingredientsTitle"
android:background="#52D017"
android:contentDescription="food"
android:padding="1dp" />
in here are the pics
sometimes this happens:
and sometimes this happens:
Any ideas? I want to just have it exactly the actual size of it
thanks!
Try to add android:adjustViewBounds="true" to your ImageView, and it should work.
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" />
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 ?