Reputation: 11337
I'm downloading a bitmap from internet, then I cast this bitmap as a Drawable to set it as a background in my LinearLayout.
I see that unfortunately the image is scaled to fill the view, how can I scale the image until the smallest size reach the border (so it's not stretched)? (And possibly centered)
I've tried something like this but without any success. Am I on the way?
LinearLayout layout = (LinearLayout) context.findViewById(R.id.main);
Drawable picture = new BitmapDrawable(context.getResources(), bitmap);
Display display = context.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
picture.setBounds(0, 0, size.x, size.y);
layout.setBackgroundDrawable(picture);
Upvotes: 0
Views: 1715
Reputation: 21
I think,It is not advisable use scaled images on an app. It's a best practice use a repeated pattern instead. So, I would try something like that:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background-image-repeatable-pattern.png"
android:tileMode="repeat" />
Save this to drawable/background.xml file (for example)
Upvotes: 1
Reputation: 290
I think you need something like this?
setScaleType(ScaleType.CENTER_CROP);
See http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Upvotes: 1