package com.xt.iconcolorchanger; import android.graphics.ColorMatrix; import android.graphics.ColorMatrixColorFilter; import android.widget.ImageView; import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.AndroidViewComponent; import com.google.appinventor.components.runtime.ComponentContainer; public class IconColorChanger extends AndroidNonvisibleComponent { public IconColorChanger(ComponentContainer container) { super(container.$form()); } @SimpleFunction(description = "Applies a color filter to the icons") public void ApplyColorFilter(AndroidViewComponent imageComponent, int color) { if (imageComponent.getView() instanceof ImageView) { final ImageView imageView = (ImageView) imageComponent.getView(); imageView.post(new Runnable() { @Override public void run() { float alpha = ((color >> 24) & 0xff) / 255f; float red = ((color >> 16) & 0xff) / 255f; float green = ((color >> 8) & 0xff) / 255f; float blue = (color & 0xff) / 255f; float[] matrix = { 0, 0, 0, 0, red * 255, 0, 0, 0, 0, green * 255, 0, 0, 0, 0, blue * 255, 0, 0, 0, alpha, 0 }; ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(new ColorMatrix(matrix)); imageView.setColorFilter(colorFilter); } }); } } @SimpleFunction(description = "Removes the color filter from the Image component") public void RemoveColorFilter(AndroidViewComponent imageComponent) { if (imageComponent.getView() instanceof ImageView) { final ImageView imageView = (ImageView) imageComponent.getView(); imageView.post(new Runnable() { @Override public void run() { imageView.clearColorFilter(); } }); } } }