I'm trying to set drawable at ImageView where I'm generation drawable by image bitmap of intrinsicWidth and height 160 and imageview width is 36dp but still image not setting in 36dp imageview.
Image is looking more smaller than imageview.
Please help me.
<ImageView
android:id="#+id/img_doctor"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_centerVertical="true"
android:scaleType="centerCrop" />
Drawable drawable11 = new BitmapDrawable(getResources(),myBitmap);
Log.e("", ""+drawable11.getIntrinsicWidth()+"::"+drawable11.getIntrinsicHeight());
imgView.setImageDrawable(drawable11);
Here I'm getting 160::160 in logcat.
if you have bitmap already then use imgView.setImageBitmap(myBitmap) instead of converting bitmap to drawable.
Related
I use TextView and drawableRight:
<TextView
android:id="#+id/text1_2"
android:drawableRight="#drawable/star"
android:layout_width="match_parent"
android:layout_height="20dp"
android:textColor="#color/grey_500"
android:textSize="#dimen/category_bottom_text_size" />
But the image does not scale according to the textView's height and just crops. I tried different solutions, such as using a bitmap file for this image and making the image .9, but the result was the same.
Try to used.. textview in image scale
android:scaleX="0.5"
android:scaleY="0.5"
I'm trying to display an image in Android's ImageView , but for an unknown reason some images are not displayed. The images that are working are relatively small and the ones not working are larger (1280x720) or so. I even tried to limit the image's size, by adding maxWidth or maxHeight in .xml file or even by code, but still no result.
Here's my .xml code for ImageView;
<ImageView
android:id="#+id/imageImageView"
android:layout_width="fill_parent"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:layout_height="wrap_content"
android:contentDescription="#string/empty"
android:gravity="center"
android:src="#drawable/test3" />
And for my code:
ImageView imageView = (ImageView) view
.findViewById(R.id.imageImageView);
imageView.requestLayout();
imageView.getLayoutParams().height = 100;
imageView.getLayoutParams().width = 100;
Any ideas / suggestions ? Thank you.
Try resizing your image like :
private Drawable resize(Drawable image) {
Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap,
(int) (bitmap.getWidth() * 0.5), (int) (bitmap.getHeight() * 0.5), false);
return new BitmapDrawable(getResources(), bitmapResized);
}
And in your onCreateView() of your fragment:
ImageView imageView = (ImageView) view
.findViewById(R.id.imageImageView);
imageView.setImageResource(0);
Drawable draw = getResources().getDrawable(R.drawable.test3);
draw = resize(draw);
imageView.setImageDrawable(draw);
i had same problem sometime ago, my solution was checking the image naming convention example("-" to "_",no space within names, ...) you can check the java naming convention for more information.
Try this:
Add these two lines in xml code of your image view:
android:scaleType="fitXY"
android:adjustViewBounds="true"
<ImageView
android:id="#+id/imageImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="#string/empty"
android:src="#drawable/test3" />
Try this :)
For me i was getting this error
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (2250x4386, max=4096x4096)
my image size was actually 750x1462 but because it was sitting in the drawable-mdpi res bucket i think behind the scenes it was being scaled up, so i moved it to xxhdpi and then it loaded the image without a problem.
This might have come a little late. I think the filename could also be a problem. I just renamed mine from sebastian-staines-mfzdRsWsiRA-unsplash.jpg to sebastian_staines.jpg in my drawable folder in Android Studio and Boom, The Imageview Displayed it.
Hope this helps someone else
I follow this http://www.androidhub4you.com/2012/07/how-to-crop-image-from-camera-and.html to crop image.
I have 4-5 ImageView in a collage frame.In All Imageview i have set a default image.On click on particular imageview, I am selecting Image from gallery then crop the Image. Now I have to set the cropped Image in my ImageView. So after setting the cropped Image my ImageView get re-size, and because of it other ImageView's size also differ. I want while i am setting the cropped image in Imageview my ImageView must not re-size.
The size of resulted cropped Image may different so I took the ImageView Size before cropping, And after cropping I am scaling result bitmap as the same size of my Image view.
Coding sample is given blow.
I am getting height and width of my Imageview like this
viewhight = tempView.getMeasuredHeight();
viewWidth = tempView.getMeasuredWidth();
After cropping Image I scaled Image according to height and width of my view like this
Bitmap photobitmap1 = Bitmap.createScaledBitmap(photobitmap, viewWidth,viewhight, true);
After this i changed it into drawable like this and set it on my Imageview.
Drawable drawble = new BitmapDrawable(getResources(),photobitmap1);
tempView.setBackground(drawble);
but on setting the hight and width of my view get resized and because of this near Image view also get resized why I don't know.because i converted my imageview in the same size of view.
I also tried this to scale bitmap :
Bitmap photobitmap1 = Bitmap.createScaledBitmap(photobitmap, (viewWidth-30), (viewhight-30), true);
and this to set bitmap :
tempView.setImageBitmap(photobitmap1);
tempView.setBackgroundColor(Color.TRANSPARENT);
but nothing worked.
my xml code is like this:
<LinearLayout
android:id="#+id/fst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/frame"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="85dp"
android:paddingRight="85dp"
android:paddingTop="10dp"
>
<ImageView
android:id="#+id/viw_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="2.5dp"
android:layout_weight="1"
android:background="#drawable/rec"
/>
<ImageView
android:id="#+id/viw_2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="2.5dp"
android:background="#drawable/rec"
/>
</LinearLayout>
can any one help me please .....
Are you setting a fixed height and width for the imageview in the XML ?
Example:
<ImageView
android:layoutWidth="60dp"
android:layoutHeight="60dp" />
I have downloaded a Bitmap from a WebService, and set it to an ImageView. The ImageView has a set height of 120dip and a width set to FILL_PARENT, so it fills the complete width of the screen. Now i want to stretch the Bitmap to fit the width of the ImageView, but the if the height of the Bitmap is greater then the height of the ImageView, i want the remainder of the image to flow off the top and bottom of the ImageView. So in way, like i have a frame infront of the image and the only portion of the image visible is the contents of the frame. How can i do this ?
See Android Developers on ImageView ScaleType
You are interested in the scale_type enum. Use CENTER_CROP.
Here is complete example:
<ImageView android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/eureka"
android:scaleType="centerCrop">
</ImageView>
Here is also a cool visualization of all possible options for ImageView.
I'm trying to set an image of an ImageButton at runtime. It should fill the width of the screen and use as much space in height as it needs to scale correctly.
The ImageButton is declared as followed:
<ImageButton
android:id="#+id/map_plan_topview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:onClick="onMapImageClicked"
android:background="#0000"
android:scaleType="fitXY" />
Now I'm trying to set the image with:
ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview);
Drawable d = Drawable.createFromPath(path);
topView.setImageDrawable(d);
The Images width is scaled with fill_parent, but is not correctly stretched in height.
If I use
topView.setImageResource(R.drawable.button_where_citymap); instead, everything works fine, but I do want to set the source from a filepath at runtime.
What am I doing wrong?
fitXY stretches the image without respecting proportions. If proportions matter, then use center_crop, or center_inside
You can set the ScaleType in code as well:
ImageButton topView = (ImageButton) findViewById(R.id.map_plan_topview);
Drawable d = Drawable.createFromPath(path);
topView.setScaleType(ScaleType.CENTER_CROP);
topView.setImageDrawable(d);
You should use "SetImageResource" in place of "setImage Drawable"...!