It seems background images are automatically shrunk in Android, for example, I use setBackgroundDrawable to set background of a view:
Drawable background = getBackground();
myView.setBackgroundDrawable(background);
Instead of shrinking, I want the image to be cropped to fit the screen size. How to do it? Thanks.
Finally I fixed this problem. I use BitmapDrawable instead of Drawable, since BitmapDrawable has method setGravity(int) which can set the gravity to position/stretch the object.
Related
I am trying to hide a part of an image so that the user does not see it. Initially I copied the Bitmap pixels on another Bitmap, without copying only the pixels that I needed and making the second bitmap the correct size at creation. That worked, but I have many large images and that results in OOMs unfortunately. So instead of doing that I thought on using a ClipDrawable to draw the image, and making the pixels that I don't need invisible.
The code is as follows
ClipDrawable clipDrawable = new ClipDrawable(new BitmapDrawable(resources, bitmap), gravity, orientation);
clipDrawable.setLevel(level);
// Cannot use as the imageview source. Must use background or else we don't get anything on the screen.
picture.setBackground(clipDrawable);
// This is super important. Do not modify this! Without it you will not get the fullscreen image working and the ImageView will be deleted
// from the parent layout.
picture.setImageResource(android.R.color.transparent);
The idea is that I calculate the level based on the image size so that I hide the pixels that I don't need. And it's working. Except I don't understand why I need to use
picture.setBackground(clipDrawable);
picture.setImageResource(android.R.color.transparent);
instead of the more normal:
picture.setImageDrawable(clipDrawable);
If I do the second more normal example then I don't get anything in the ImageView, but if I set it as a background and put a transparent image over it, then it works. Since I want to further manipulate the ImageView using a zooming class that needs the picture set as the src and not as background, I cannot have both, either I get the ClipDrawable showing or I get to have zoom on the image.
Any help would be appreciated!
picture.setImageDrawable(new
ClipDrawable(new BitmapDrawable(resources, bitmap), gravity, orientation
));
ClipDrawable clipDrawable = (ClipDrawable) picture.getDrawable();
clipDrawable.setLevel(level);
I have a full screen layout and I want to use an opaque blurry version of image or bitmap as the background. So I need to know:
1) how to set a image/bitmap as background of a layout like LinearLayout or RelativeLayout. Is there a different approach for bitmap vs. image?
2) How to make it look blurry.
RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative1);
Drawable dr = new BitmapDrawable(bit);
(view).setBackgroundDrawable(drawable);
There is no difference between bitmap and image .. just go through this link for more details ..
Android - ImageView: setImageBitmap VS setImageDrawable
I have an image in a resource file.
Drawable draw = getResources().getDrawable(R.drawable.my_icon);
The image has a transparent background.
Is there a way to programmatically set the background color to the Drawable before using the end product further in my code?
I think Drawing with PorterduffXferMode may help you in your case. This way you can merge two images (your image and a overlay completly in your color you want to replace the transparent pixels with) in many different ways.
Different porterduff modes explaned:
http://www.ibm.com/developerworks/java/library/j-mer0918/
Android example:
http://www.vogella.com/code/ApiDemos/src/com/example/android/apis/graphics/Xfermodes.html
This way you draw the result inside a new Bitmap. (SRC_OVER should work in your case if your image is the src and the background is used as the dst)
setColorFilter() with Porterduff SRC will break the transparent of drawable.
I used this in my code, and it work
disabledIcon = ContextCompat.getDrawable(getContext(), resId);
disabledIcon = DrawableCompat.wrap(disabledIcon);
disabledIcon.mutate(); // to not share its state with any other drawable
DrawableCompat.setTint(disabledIcon, ContextCompat.getColor(getContext(), R.color.button_text_disabled));
I have Imageview which i am creating it dynamically,now i am setting
imageView.setImageBitmap(bitmap);
imageView.setBackgroundResource(R.drawable.a);
What is happening is the setBackgroundResource image is stretching,as the bitmap size increases.
Why is it so?Is there any workaround?
A Background resource is designed to fill up the entire View, which is why it stretched when the contents of the View are increased in size.
A possible workaround would be to use a 9-patch drawable, which only stretches are specified by you.
Another workaround would be to scale your background as your normal View Contents increase, and reset it to the new background.
I have seen these different approaches in setting images but I don't get the difference.
Why there two methods?
setBackgroundResource is for setting the background of an ImageView.
setImageResource is for setting the src image of the ImageView.
Given:
ImageView iv = new ImageView(this);
Then:
iv.setBackgroundResource(R.drawable.imagedata);
Will fit the image for the entire background. That means it will stretch the image to fill that background entirely even if the image size is too small.
imageView.setImageResource(R.drawable.imagedata);
Will occupy only the size of the image in ImageView.
For that you want to also set
android:layout_width="wrap_content"
android:layout_height="wrap_content"
for your ImageView. If the size of the image is smaller than the ImageView the remaining border will be left blank and the background will be shown.
SetBackdroundResource is for a drawable or color you want to set at the background of the imageview and your setImageResource is like to display on it.
so setImageResource is for add any resource to your imageview's front side. try this example and look at the difference. Android Gallery, ImageView Example
. This is a two layer effect,backside (setBackgroundResource) and frontside (setImageResource).
The method setBackgroundResource() belongs to all Views. The method setImageResource() only belongs to ImageView. You can set them both:
imageView.setBackgroundResource(R.drawable.sky);
imageView.setImageResource(R.drawable.balloons);
The setBackgroundResource() method will cause the image's width and height will be stretched to fill the size of the view. The setImageResource() method will let its image keep its aspect ratio.
My fuller answer is here.
setBackgroundResource sets the background image of an ImageView. The XML attribute is: android:background
setImageResource sets the image displayed in an ImageView. The XML attribute is: android:src