0

I am trying to set a square shaped bitmap as a background image for linear layout after scaling. My bitmap's are of size around 500*500 after scaling this am giving as background image of my screen size 1080*1920. I can see image is been stretched. How can i avoid this

Here is my code

Bitmap alteredBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
 Canvas canvas = new Canvas(alteredBitmap);
 Paint paint = new Paint();
 paint.setFilterBitmap(true);
 Matrix matrix = new Matrix();
 matrix.setScale(1.5f, 1.5f,original.getWidth()/2,original.getHeight()/2);
 canvas.drawBitmap(original, matrix, paint);
 Drawable d = new BitmapDrawable(context.getResources(), alteredBitmap);

LinearLayout mRootLayout = (LinearLayout)findViewById(R.id.main);
mRootLayout.setBackground(d);

and layout is

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusable="false" >
srujana
  • 1
  • 2
  • try this: http://stackoverflow.com/a/8501398/1529129 – Rahul Tiwari Oct 11 '15 at 07:27
  • Possible duplicate of [How to implement an android:background that doesn't stretch?](http://stackoverflow.com/questions/5902230/how-to-implement-an-androidbackground-that-doesnt-stretch) – Rahul Tiwari Oct 11 '15 at 07:28
  • it is very simple: create a custom `Drawable` class and override its `draw(Canvas)` method like [here](http://codeshare.io/tLIwl) – pskink Oct 12 '15 at 06:13
  • why are you calling `matrix.setScale(1.5f, 1.5f` ? what is `1.5f` magic number? – pskink Oct 12 '15 at 09:07

0 Answers0