Last active
May 8, 2016 15:42
-
-
Save fabioranieri/e6700932bcbb4898d780a6e551d11bff to your computer and use it in GitHub Desktop.
Utility to be used with images. Load nine patch through code, method to apply greyscale on ImageView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.fingerlinks.mobile.android.utils; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.NinePatch; | |
import android.graphics.Rect; | |
import android.graphics.drawable.BitmapDrawable; | |
import android.graphics.drawable.Drawable; | |
import android.graphics.drawable.NinePatchDrawable; | |
import android.renderscript.Allocation; | |
import android.renderscript.Element; | |
import android.renderscript.RenderScript; | |
import android.renderscript.ScriptIntrinsicBlur; | |
/** | |
* Created by fabio on 30/03/16. | |
*/ | |
public class ImageUtils { | |
public static final int COMPRESSION_QUALITY = 85; | |
@TargetApi(18) | |
public static Bitmap blurRenderScript(Context ctx, Bitmap smallBitmap, int radius) { | |
try { | |
smallBitmap = RGB565toARGB888(smallBitmap); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
Bitmap bitmap = Bitmap.createBitmap( | |
smallBitmap.getWidth(), smallBitmap.getHeight(), | |
Bitmap.Config.ARGB_8888); | |
RenderScript renderScript = RenderScript.create(ctx); | |
Allocation blurInput = Allocation.createFromBitmap(renderScript, smallBitmap); | |
Allocation blurOutput = Allocation.createFromBitmap(renderScript, bitmap); | |
ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(renderScript, | |
Element.U8_4(renderScript)); | |
blur.setInput(blurInput); | |
blur.setRadius(radius); // radius must be 0 < r <= 25 | |
blur.forEach(blurOutput); | |
blurOutput.copyTo(bitmap); | |
renderScript.destroy(); | |
return bitmap; | |
} | |
@TargetApi(18) | |
private static Bitmap RGB565toARGB888(Bitmap img) throws Exception { | |
int numPixels = img.getWidth() * img.getHeight(); | |
int[] pixels = new int[numPixels]; | |
//Get JPEG pixels. Each int is the color values for one pixel. | |
img.getPixels(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight()); | |
//Create a Bitmap of the appropriate format. | |
Bitmap result = Bitmap.createBitmap(img.getWidth(), img.getHeight(), Bitmap.Config.ARGB_8888); | |
//Set RGB pixels. | |
result.setPixels(pixels, 0, result.getWidth(), 0, 0, result.getWidth(), result.getHeight()); | |
return result; | |
} | |
/** | |
* @param resource | |
* @param context | |
* @return | |
*/ | |
public static Drawable loadNinePatch(int resource, Context context) { | |
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resource); | |
byte[] chunk = bitmap.getNinePatchChunk(); | |
if (NinePatch.isNinePatchChunk(chunk)) { | |
return new NinePatchDrawable(context.getResources(), bitmap, chunk, new Rect(), null); | |
} else return new BitmapDrawable(bitmap); | |
} | |
/** | |
* @param path | |
* @param context | |
* @return | |
*/ | |
public static Drawable loadNinePatch(String path, Context context) { | |
Bitmap bitmap = BitmapFactory.decodeFile(path); | |
byte[] chunk = bitmap.getNinePatchChunk(); | |
if (NinePatch.isNinePatchChunk(chunk)) { | |
return new NinePatchDrawable(context.getResources(), bitmap, chunk, new Rect(), null); | |
} else return new BitmapDrawable(bitmap); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment