Active Members Fi8sVrs Posted October 22, 2017 Active Members Report Posted October 22, 2017 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 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 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 1 1 Quote