I have ImageView that great scale my file.png with scaleType = "fitCenter" by keeping it rectangular and making smaller/bigger on diffrent screens/rotations. This ImageView is inside LinearLayout with the same background file. The problem is, I cannot get the same result after scaling with background - always source picture gets streched horizantaly or gets bigger.
Here is code i have:
<LinearLayout
android:id="@+id/layout_button_quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/scaling_background">
<ImageView
android:id="@+id/button_quiz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/brain_off"
android:scaleType="fitCenter"/>
</LinearLayout>
Here is solution of backgroud scaling i tried, that doesn't work:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/brain_off"
android:scaleType="fitCenter"/>
To sum it up: I want background of my LinearLayout to scale the same as ImageView that is inside it, that is scaleType=fitCenter.
<item android:drawable="@color/transparent"/>
<item>
<bitmap
android:gravity="center|fill"
android:src="@drawable/ic_share" />
</item>
set this as background of your layout. But this will fill the background and image may stretch. to make it work perfectly set background programmatically
int widthI = bitMapin.getWidth();
int heightI = bitMapin.getHeight();
int widthC = container.getWidth();
int heightC = container.getHeight();
int new_width = widthC;
int new_height = (int) Math.floor((double) heightI *( (double) new_width / (double) widthI ));
Bitmap newbitMap = Bitmap.createScaledBitmap(bitMapin, new_width, new_height, true);
container.setBackground(new BitmapDrawable(getResources(), newbitMap))