How to Implement Android Drawable to Maintain Different Resolution of Images

0
Shares
How to Implement Android Drawable to Maintain Different Resolution of Images
4 (80%) 1 vote

Android drawable

Back in the past, before the arrival of Android support library AppCompat, Android developers had to create multiple version of images for different display resolutions. Now, due to this, it took more time to create the extra assets and consumed more size that increased the Apk size.

But, with the release of Android Lollipop, a new component was added named Android drawable.

Android drawables, as the name implies, are vector drawables based on vector graphics. They’re the way of describing graphical elements using geometric shapes. So now, there is no need to design different sizes for mdpi, xhdpi, and etc., because with the help of Android vector drawable, you’ll only need to create one image as an Xml file, and scale it to all dpi for the different devices.

By this, you’ll not only save space, but it will also simplify your app code structure. And, today, in this Android app tutorial, we’ll see the implementation process of Android drawable.

Let’s Get Started

First, open your Android Studio and create a new project.

01

In the next tab, select your target Android device.

02

Now, select Empty Activity and click on next.

03

Lastly, enter your activity name and click on finish.

04

Now that you have successfully created a new project, start code integration.

Build.Gradle

apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.svgdrawablesdemo"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.2.0'
}

To create VectorDrawable from Material Icons

In the project, Right Click on the drawable directory.

Now, go to New => Vector Asset.

05

06

07

Using VectorDrawable

activity_main.xml

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

<android.support.v7.widget.AppCompatImageView
android:id="@+id/ic_logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp"
app:srcCompat="@drawable/ic_android_black_24dp"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
private AppCompatImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (AppCompatImageView) findViewById(R.id.ic_logo);
imgView.setImageResource(R.drawable.ic_android_black_24dp);
imgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
imgView.setColorFilter(color);
}
});
}
}

That’s It!

Android SVG or Android drawable tutorial permits Android developers to quickly make vector graphics without any expensive software. Though, if the implementation process looks tricky for Android vector drawable, consider taking help of an expert or Android app development company before using the SVG format in your app.

This page was last edited on October 4th, 2018, at 7:13.
 
0
Shares
 

Want to Develop Android App From Scratch? Contact Us Now

Get your free consultation now