I have a ImageButton with default drawable image. From the code I set up the ImageButton layout size so after this modification the image doesn't cover the whole button because of centerInside scaleType. In the OnClick event I use this code to replace the ImageButton's image.
button.setImageBitmap(img);
button.invalidate();
The new image is smaller than the old one. After replace the new image appears but the old image parts can be see around the new. How can I force to clear the old image or simply redraw the whole ImageButton to show only the new image?
<ImageButton
android:id="#+id/btnPhotoFront"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_weight="1"
android:background="#android:color/transparent"
android:contentDescription="#string/front"
android:scaleType="centerInside"
android:src="#drawable/camera" />
I tried with android:background="#00000000" but has no effect.
The repaint works only if I don't set up background and leave the default gray button layer.
Try
android:background="#null"
Also note that you are setting two different properties. setImageBitmap is not the corresponding method for android:background
b.setBackground(drawable)
b.setImageBitmap(bm);
I bet your
android:src="#drawable/camera"
image file does not have a transparent background. Use Photoshop or GIMP or something similar, cut out your image and put it on a transparent background.
Related
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"
I use two ways to set image for imageView, by xml and by code setBackgroundResource
eg. img.setBackgroundResource(R.drawable.task_learn_lock);
OR xml
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"
android:src="#drawable/task_learn_lock"
android:contentDescription="#string/desc"
android:scaleType="centerInside" />
But the result is different, the image created by code is streched vertically. Anyone know why? I need to set a new image when the app is running. I tried to post a screen shot, but i do not have enough reputation to do so. Thanks.
Most likely it is because the default content (defined by xml) of the imageView's are different for these two approaches.
In your second method that sets the background image programmatically, I guess you should have set a default drawable image that is different from #drawable/task_learn_lock in size and aspect ratio.
Since the layout_height and layout_width of your ImageView are defined as wrap_content, the aspect ratio of your ImageView will be the same as your default src image. Therefore when you set the background image by img.setBackgroundResource(R.drawable.task_learn_lock), the image will be stretched to fit the current ImageView's dimension.
It appears in your case that you want to set the content to be #drawable/task_learn_lock but not the background of your ImageView, you should use setImageResource instead of setBackgroundResource.
I am trying to set bitmap to imagebutton but image size change
My Image xml is
<ImageButton
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/choose_pic"
android:tag="PIC1" />
i want to set image size accoring to the "#drawable/choose_pic" size
after choosing image from gallery i am convertin it to bitmap and it change the imagebutton size please give me some solution ?
//for that use ImageView with src
<ImageView
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/choose_pic"
android:tag="PIC1" />
As you are using ImageButton you need to set your image in property android:src="" not as background of your ImageButton.
So set your image in src property and remove background as below:
<ImageView
android:id="#+id/button_ChoosePicFIrst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#drawable/choose_pic"
android:tag="PIC1" />
EDITED:
Difference between android:background" and android:src"
android:background exists for all the view. As the name suggests this is what is going to be there in the background.
android:src exists for ImageViews and its subclasses. You can think of this as the foreground. Because ImageView is a subclass of View you even have android:background for that.
If you set an image to be the background of your ImageView, then the image will scale to whatever size the ImageView is. Other than that, src is a foreground image and background is a background image.
The src to an ImageView has additional features:
different scaling types
adjustViewBounds for setting bounds to match image dimensions
some transformations such as alpha-setting
And more that you may find in the docs.
In my new android app I need to give border to imageview by using something like .9.png.
The border size should change with respect to image which I have given to imageview and If possible I need to give a background image to imageview as I'm going to apply a transparent png image to imageview.
Should I create a new custom view for this?
I think you should create a custom view. Do a LinearLayout with the background as the border and have the ImageView centered inside the LinearLayout. Use 9-patch to get a correct stretch and create a content area so that the border is showing (use draw9patch in android SDK/Tools).
Example:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
Then 9-patch your border (border.9.png in this example). You can have the same stretching as content area.
I trying to create a full screen Imagebutton. But i kept getting grey border ard it.
<ImageButton
android:id="#+id/StartButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/startscreen" />
I have try stretch the pic to full screen on eclipse but there still show a grey border around the picture.
I was wondering is that due to the resolution of my setting or the picture resolution. I am working on a galaxy note.
this should remove the grey border
<ImageButton
android:id="#+id/StartButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/startscreen"
android:background="#000000"/>
The idea of #Proxy32 is probably the best one. Alternatively don't use the android:src attribute but the android:background. You can also use a simple Button instead with the background set to your drawable and leave the text out.