How to display a bitmap as background of view without stretching android - android

I want to set a bitmap as a view background without stretching and i dnt want to change the view width and height as wrap-content. I want to keep the width and height of view as match-parent. I have set background of view like below, where mDrawingView is a custom view which extends View.
Bitmap tempBitmap = BitmapFactory.decodeFile(copy.getAbsolutePath());
mDrawingView.setBackground(new BitmapDrawable(getResources(), tempBitmap));
and my layout file looks like below,
<MyPackageName.DrawingView
android:id="#+id/drawing_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
And please note I'm not using imageview.
Please suggest me any idea?

I have changed my DrawingView which extends Imageview instead of View and then set the bitmap like below
Bitmap bitmap = BitmapFactory.decodeFile(copy.getAbsolutePath());
mDrawingView.setImageBitmap(bitmap);
Now bitmap displays on imageview without stretching and works perfect.
Thanks for your valuable comments and answers.

if your are using image view then you can do like below
ImageView.ScaleType
which can be CenterCrop,fit etc you can read about it here it's android native support
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

You can do
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.res_id);
where res_id is the resource id in drawable folder.

Related

How to set opaque picture as layout background

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

ImageView with setBackgroundResource and setImageBitmap

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.

Drawing a large number of ImageViews with the same Bitmap but different size

I am trying to draw multiple ImageViews inside a single LinearLayout.
All the ImageViews need to have a single bitmap.
The ImageViews will only vary in size.
The single bitmap will not be resized.
The simple way is to create one Bitmap per ImageView. But, this runs out of memory quickly.
final Bitmap placeholderBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
final Canvas canvas = new Canvas(placeholderBitmap);
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.placeholder_image), 0, 0, null);
imageView.setImageBitmap(placeholderBitmap);
linearLayout.addView(imageView);
I also tried setting the max and min height and width, no effect. The images remain the same size.
imageView.setImageResource(R.drawable.ic_no_image);
imageView.setMaxHeight(imageViewInfo.height);
imageView.setMaxWidth(imageViewInfo.width);
I believe working with Drawables is the right "Android" way to do it, but I can't find a way to dynamically create a Drawable with the right side and layering the shared bitmap into it.
Just use one Bitmap -- as you say they will all use the same Bitmap, so no duplication is necessary.
If you want the ImageViews to have different sizes and have the Bitmap scale itself to the size of the ImageView then use the setScaleType or android:scaleType attribute to set the scaling of the ImageView. For instance FIT_START maintains the aspect ratio of your image and tries to fill the ImageView starting from the top-left corner.
I don't see any need to create a Drawable, though if you need to you can just create one from a Bitmap using:
Drawable d = new BitmapDrawable(bitmap);
ImageView actually does this automatically when you call setImageBitmap(...).
ImageView will always resize the bitmap to it need proportions.
If you want to save memory, resize the bitmaps manually and set them into the ImageViews this should stop the ImageView from resizing it internally - to make sure, you can set the layout_width and layout_hieght of the image view to 'wrap_content'.
I think may be looking for a ClipDrawable.
You would set the android:drawable property of the clip XML to your bitmap
That or you can do it with the Java code also given in the example

What is the difference between ImageView.setBackgroundResource and ImageView.setImageResource?

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

ImageView image not appearing in ViewGroup

I have an ImageView as a child of a ViewGroup that is part of a GridView. The drawable of the ImageView does not appear, however I can see the background color, and if I use the drawable in setBackgroundDrawable instead, it is displayed. I've tried changing every property and calling invalidate() but none have made the drawable appear.
The Log.i prints
android.graphics.drawable.BitmapDrawable#407bd4c0
setImageResource(int) doesn't work either.
I'm using Honeycomb.
public MyViewGroup(Context context) {
super(context);
myImageView = new ImageView(context);
myImageView.setBackgroundColor(Color.RED);
myImageView.setScaleType(ImageView.ScaleType.CENTER);
Drawable drawable = this.getResources().getDrawable(R.drawable.myimage);
myImageView.setImageDrawable(drawable);
Log.i("",drawable+"");
//myImageView.setImageResource(R.drawable.myimage);
addView(myImageView);
}
I omitted the onLayout code where I call setLeft() setTop() setRight() and setBottom().
Any help is much appreciated. Thanks.
Update:
Also doesn't work with android.R.drawable images. I'm stumped.
Maybe the problem is, that you don't set the layout parameters. Try:
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, WRAP_CONTENT);
myImageView .setLayoutParams(params);
Try setting getting the image as bitmap, then setting it as drawable.
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.myimage)
Drawable drawable = new BitmapDrawable(bmp);"
myImageView.setImageDrawable(drawable);
Just out of curiosity: Why you do all of this in your constructor?
If the parent layout has width/height of fill_parent, changing it to wrap_content might do the trick.
I was having this problem in Honeycomb using an ImageView with a drawable in a layout - the image was there judging from the width of the dialog I had it appearing in, but I couldn't see the image itself. The ImageView's parent was a LinearLayout with fill_parent. Switching to wrap_content fixed it right up.

Categories

Resources