Home » Android » Scale a drawable resource in XML (Android)

Scale a drawable resource in XML (Android)

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to scale a drawable to the double of its original size.I’m trying to use this: Drawable Resouces. My current code is:

Drawable:

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_launcher"
    android:scaleHeight="80%"
    android:scaleWidth="80%" >

</scale>

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/scale2" />

</LinearLayout>

But nothing is shown in the second ImageView. How can I fix it?

How to&Answers:

Drawable

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/logo"
    android:scaleGravity="center_vertical|center_horizontal"
    android:scaleHeight="80%"
    android:scaleWidth="80%" />

layout

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/scale" />

try like this or you may check this link.

http://developer.android.com/guide/topics/resources/drawable-resource.html

Answer:

Once i had this problem i found the solution by using inset instead of scale.
Create a new xml drawable file in drawable folder and add you code like this:

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/buttonright"
android:insetTop="4dp"
android:insetLeft="4dp"
android:insetRight="4dp"
android:insetBottom="4dp"
/>

use it like this

   <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/scale" />