@Implementation // todo: this sucks, it's all just so we can detect 9-patches protected static Drawable createFromResourceStream( Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) { if (is == null) { return null; } Rect pad = new Rect(); if (opts == null) opts = new BitmapFactory.Options(); opts.inScreenDensity = DisplayMetrics.DENSITY_DEFAULT; Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); if (bm != null) { boolean isNinePatch = srcName != null && srcName.contains(".9."); if (isNinePatch) { ReflectionHelpers.callInstanceMethod(bm, "setNinePatchChunk", ClassParameter.from(byte[].class, new byte[0])); } byte[] np = bm.getNinePatchChunk(); if (np == null || !NinePatch.isNinePatchChunk(np)) { np = null; pad = null; } if (np != null) { // todo: wrong return new NinePatchDrawable(res, bm, np, pad, srcName); } return new BitmapDrawable(res, bm); } return null; }
public static NinePatchDrawable createNinePatchWithCapInsets(Resources res, Bitmap bitmap, List<Range> rangeListX, List<Range> rangeListY, String srcName) { ByteBuffer buffer = getByteBuffer(rangeListX, rangeListY); NinePatchDrawable drawable = new NinePatchDrawable(res, bitmap, buffer.array(), new Rect(), srcName); return drawable; }
Drawable drawableButtonMessageUp = new NinePatchDrawable(new NinePatch(new Texture("data/message_my_g.9.png"), 1, 1, 1, 1));
Drawable nineDrawable = new NinePatchDrawable(view.getContext().getResources(), bitmap, chunk, new Rect(), null);
@Override public void finish(boolean succeed, String name, Bitmap bmp) { Drawable drawable = null; if( !succeed ){ return; } byte[] chunk = bmp.getNinePatchChunk(); if(chunk != null && NinePatch.isNinePatchChunk(chunk)){ drawable = new NinePatchDrawable(fView.getContext().getResources(), bmp, chunk, new Rect(), null); } else{ drawable = new BitmapDrawable(bmp); } fView.setBackgroundDrawable(drawable); } });
@Override public void finish(boolean succeed, String name, Bitmap bmp) { byte[] chunk; if (!succeed || bmp == null || bmp.isRecycled()) { return; } chunk = bmp.getNinePatchChunk(); if ( chunk != null && NinePatch.isNinePatchChunk(chunk) ) { Drawable drawable; drawable = new NinePatchDrawable(imageView.getContext().getResources(), bmp, chunk, new Rect(), null); imageView.setImageDrawable(drawable); return; } imageView.setImageBitmap(bmp); } });
/** * 获取asset下面的.9 png * @param imagename 图片名 * @param context 上下文对象 * */ public static NinePatchDrawable get9png(String imagename, Context context){ Bitmap toast_bitmap; try { toast_bitmap = BitmapFactory.decodeStream(context.getAssets().open("image/" + imagename+".9.png")); byte[] temp = toast_bitmap.getNinePatchChunk(); boolean is_nine = NinePatch.isNinePatchChunk(temp); if (is_nine) { NinePatchDrawable nine_draw = new NinePatchDrawable(context.getResources(), toast_bitmap, temp, new Rect(),null); return nine_draw; } } catch (IOException e) { e.printStackTrace(); } return null; }
/** * 获取asset下面的.9 png * * @param imagename 图片名 * @param context 上下文对象 */ public static NinePatchDrawable get9png(String imagename, Context context) { Bitmap toast_bitmap; try { toast_bitmap = BitmapFactory.decodeStream(context.getAssets().open("image/" + imagename + ".9.png")); byte[] temp = toast_bitmap.getNinePatchChunk(); boolean is_nine = NinePatch.isNinePatchChunk(temp); if (is_nine) { NinePatchDrawable nine_draw = new NinePatchDrawable(context.getResources(), toast_bitmap, temp, new Rect(), null); return nine_draw; } } catch (IOException e) { e.printStackTrace(); } return null; }
@Override public void finish(boolean succeed, String name, Bitmap bmp) { Drawable drawable = null; if( !succeed ){ return; } byte[] chunk = bmp.getNinePatchChunk(); if(chunk != null && NinePatch.isNinePatchChunk(chunk)){ drawable = new NinePatchDrawable(fBar.getContext().getResources(), bmp, chunk, new Rect(), null); } else{ drawable = new BitmapDrawable(bmp); } fBar.setProgressDrawable(drawable); } });
private void init() { mMatrix = new Matrix(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); if (mMaskResId <= 0) { return; } mMaskBmp = BitmapFactory.decodeResource(getResources(), mMaskResId); byte[] ninePatchChunk = mMaskBmp.getNinePatchChunk(); if (ninePatchChunk != null && NinePatch.isNinePatchChunk(ninePatchChunk)) { mMaskDrawable = new NinePatchDrawable(getResources(), mMaskBmp, ninePatchChunk, new Rect(), null); } internalSetImage(); }
@Implementation // todo: this sucks, it's all just so we can detect 9-patches public static Drawable createFromResourceStream(Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) { if (is == null) { return null; } Rect pad = new Rect(); if (opts == null) opts = new BitmapFactory.Options(); opts.inScreenDensity = DisplayMetrics.DENSITY_DEFAULT; Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); if (bm != null) { boolean isNinePatch = srcName != null && srcName.contains(".9."); if (isNinePatch) { ReflectionHelpers.callInstanceMethod(bm, "setNinePatchChunk", ClassParameter.from(byte[].class, new byte[0])); } byte[] np = bm.getNinePatchChunk(); if (np == null || !NinePatch.isNinePatchChunk(np)) { np = null; pad = null; } if (np != null) { // todo: wrong return new NinePatchDrawable(res, bm, np, pad, srcName); } return new BitmapDrawable(res, bm); } return null; }
@Implementation // todo: this sucks, it's all just so we can detect 9-patches public static Drawable createFromResourceStream(Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) { if (is == null) { return null; } Rect pad = new Rect(); if (opts == null) opts = new BitmapFactory.Options(); opts.inScreenDensity = DisplayMetrics.DENSITY_DEFAULT; Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); if (bm != null) { boolean isNinePatch = srcName != null && srcName.contains(".9."); if (isNinePatch) { ReflectionHelpers.callInstanceMethod(bm, "setNinePatchChunk", ClassParameter.from(byte[].class, new byte[0])); } byte[] np = bm.getNinePatchChunk(); if (np == null || !NinePatch.isNinePatchChunk(np)) { np = null; pad = null; } if (np != null) { // todo: wrong return new NinePatchDrawable(res, bm, np, pad, srcName); } return new BitmapDrawable(res, bm); } return null; }
@Implementation // todo: this sucks, it's all just so we can detect 9-patches public static Drawable createFromResourceStream(Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) { if (is == null) { return null; } Rect pad = new Rect(); if (opts == null) opts = new BitmapFactory.Options(); opts.inScreenDensity = DisplayMetrics.DENSITY_DEFAULT; Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); if (bm != null) { boolean isNinePatch = srcName != null && srcName.contains(".9."); if (isNinePatch) { ReflectionHelpers.callInstanceMethod(bm, "setNinePatchChunk", ClassParameter.from(byte[].class, new byte[0])); } byte[] np = bm.getNinePatchChunk(); if (np == null || !NinePatch.isNinePatchChunk(np)) { np = null; pad = null; } if (np != null) { // todo: wrong return new NinePatchDrawable(res, bm, np, pad, srcName); } return new BitmapDrawable(res, bm); } return null; }
@Implementation // todo: this sucks, it's all just so we can detect 9-patches protected static Drawable createFromResourceStream( Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) { if (is == null) { return null; } Rect pad = new Rect(); if (opts == null) opts = new BitmapFactory.Options(); opts.inScreenDensity = DisplayMetrics.DENSITY_DEFAULT; Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); if (bm != null) { boolean isNinePatch = srcName != null && srcName.contains(".9."); if (isNinePatch) { ReflectionHelpers.callInstanceMethod(bm, "setNinePatchChunk", ClassParameter.from(byte[].class, new byte[0])); } byte[] np = bm.getNinePatchChunk(); if (np == null || !NinePatch.isNinePatchChunk(np)) { np = null; pad = null; } if (np != null) { // todo: wrong return new NinePatchDrawable(res, bm, np, pad, srcName); } return new BitmapDrawable(res, bm); } return null; }
@Implementation public Drawable getDrawable(int drawableResourceId) throws Resources.NotFoundException { ResourceLoader resLoader = Robolectric.shadowOf(Robolectric.application).getResourceLoader(); Drawable xmlDrawable = resLoader.getXmlDrawable(drawableResourceId); if (xmlDrawable != null) { return xmlDrawable; } Drawable animDrawable = resLoader.getAnimDrawable(drawableResourceId); if (animDrawable != null) { return animDrawable; } Drawable colorDrawable = resLoader.getColorDrawable(drawableResourceId); if (colorDrawable != null) { return colorDrawable; } if (resLoader.isNinePatchDrawable(drawableResourceId)) { return new NinePatchDrawable(realResources, null); } return new BitmapDrawable(BitmapFactory.decodeResource(realResources, drawableResourceId)); }