Jump to content
Fi8sVrs

Reducing apk size : A quick hack!

Recommended Posts

  • Active Members

1*ykUI6Ahxl9BRamkIzO_H1g.png

There are already many good articles on “How to reduce size of an apk”.

My focus in this article will be on Reuse of resources”. Resources contribute a major chunk in the size of apk.

The techniques that I’ll be mentioning here only takes few lines of changes and will save a great deal of space.

# Use RotateDrawable resources

Many a times, resources are nothing but just a rotated version of some other resource. For e.g. collapse arrow and expand arrow

1*obIYA2Um6wLPjRsoLUYC_A.png

arrow_up.xml can be easily drawn using arrow_down.xml. Create a file named arrow_up.xml in res/drawable folder like this:

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/arrow_down"
    android:fromDegrees="180"
    android:pivotX="50%"
    android:pivotY="50%"
android:toDegrees="180" />

res/drawable/arrow_up.xml

 

# Use setColorFilter for resources with different colour versions

1*cscG6SxKv0ggTjqvw73HPQ.png

 

In this example, three different versions of same icon are used. Instead of using three different icon sets, we can easily use setColorFilter to produce other two icons using the first one.

To achieve this, we can create a custom view that extends ImageView and apply color filter in its constructor by passing the desired color in its xml attributes.

We’ll need:

  • CustomColorIconView.java — Class that extends ImageView
  • attrs.xml — styleable for color attribute
public class CustomColorIconView extends ImageView{
    public CustomColorIconView(Context context) {
        super(context);
    }

    public CustomColorIconView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomColorIconView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomColorIconView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs);
    }
  
    private void init(Context context, AttributeSet attrs){
         
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomColorIconView);

        int color = typedArray.getColor(R.styleable.CustomColorIconView_dciv_color,0);
        setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

        typedArray.recycle();
    }

    public void setImageFilterColor(int color)
    {
        if(color == -1)
        {
            setColorFilter(null);
        }
        else
        {
            setColorFilter(color,PorterDuff.Mode.SRC_ATOP);
        }
    }
}

CustomColorIconView.java

 

<declare-styleable name="CustomColorIconView">
  <attr name="dciv_color" format="color"/>
</declare-styleable>

attrs.xml

 

That’s it. Now to use this in your xml, create a CustomColorIconView and set dciv_color to whatever color you need. In this case, we changed the dark location icon to white.

<CustomColorIconView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/ic_location_dark"
app:dciv_color="@color/white" />

layout.xml

 

That’s all for this post! Will keep posting interesting snippets about Android and other things. Follow me to get updates :)

Thanks for reading this article. Be sure to clap/recommend as much as you can and also share with your friends. It means a lot to me.

Source

  • Like 1
  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...