public static Drawable createImageBasedDrawable(byte[] byteArray, int bitmapDensityReference, boolean expectedNinePatch, Resources res) { if (expectedNinePatch) return createNinePatchDrawable(byteArray,bitmapDensityReference,res); Bitmap bitmap = BitmapUtil.createBitmap(byteArray,bitmapDensityReference,res); byte[] chunk = bitmap.getNinePatchChunk(); boolean result = NinePatch.isNinePatchChunk(chunk); if (result) { // Raro pero resulta que es un NinePatch (raro porque lo normal es que se especifique la extensión .9.png) return createNinePatchDrawable(bitmap,res); } else { return new BitmapDrawable(res, bitmap); } }
public static void assertEquals(NinePatchDrawable a,NinePatchDrawable b) { assertEqualsDrawable(a, b); NinePatch a2 = (NinePatch) TestUtil.getField(a, "mNinePatch"); NinePatch b2 = (NinePatch) TestUtil.getField(b, "mNinePatch"); assertEquals((Bitmap) TestUtil.getField(a2, "mBitmap"), (Bitmap) TestUtil.getField(b2, "mBitmap")); Paint a_paint = a.getPaint(); Paint b_paint = b.getPaint(); //assertEquals(a_paint.isAntiAlias(), b_paint.isAntiAlias()); assertEquals(a_paint.isDither(), b_paint.isDither()); // assertEquals(a_paint.isFilterBitmap(), b_paint.isFilterBitmap()); }
private void initPaint(Context context) { this.mBmp = BitmapFactory.decodeResource(context.getResources(), mDrawableRid); if (mBmp != null) { if (mBmp.getNinePatchChunk() != null) { hasNinePatch = true; mNinePatch = new NinePatch(mBmp, mBmp.getNinePatchChunk(), null); } if (mMode == RVItemDecorationConst.MODE_HORIZONTAL) mCurrentThickness = mThickness == 0 ? mBmp.getHeight() : mThickness; if (mMode == RVItemDecorationConst.MODE_VERTICAL) mCurrentThickness = mThickness == 0 ? mBmp.getWidth() : mThickness; } mPaint = new Paint(); mPaint.setColor(mColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(mThickness); }
public void setParams(Context context, Param params) { this.mMode = params.mode; this.mDrawableRid = params.drawableRid; this.mColor = params.color; this.mThickness = params.thickness; this.mDashGap = params.dashGap; this.mDashWidth = params.dashWidth; this.mPaddingStart = params.paddingStart; this.mPaddingEnd = params.paddingEnd; this.mFirstLineVisible = params.firstLineVisible; this.mLastLineVisible = params.lastLineVisible; this.mGridLeftVisible = params.gridLeftVisible; this.mGridRightVisible = params.gridRightVisible; this.mGridTopVisible = params.gridTopVisible; this.mGridBottomVisible = params.gridBottomVisible; this.mGridHorizontalSpacing = params.gridHorizontalSpacing; this.mGridVerticalSpacing = params.gridVerticalSpacing; this.mParent = params.parent; if (mParent != null) compatibleWithLayoutManager(mParent); this.mBmp = BitmapFactory.decodeResource(context.getResources(), mDrawableRid); if (mBmp != null) { if (mBmp.getNinePatchChunk() != null) { hasNinePatch = true; mNinePatch = new NinePatch(mBmp, mBmp.getNinePatchChunk(), null); } if (mMode == MODE_HORIZONTAL) mCurrentThickness = mThickness == 0 ? mBmp.getHeight() : mThickness; if (mMode == MODE_VERTICAL) mCurrentThickness = mThickness == 0 ? mBmp.getWidth() : mThickness; } initPaint(); }
private Drawable a(String str, Context context) { Drawable createFromStream; IOException e; try { InputStream open = context.getApplicationContext().getAssets().open(str); if (open == null) { return null; } if (str.endsWith(".9.png")) { Bitmap decodeStream = BitmapFactory.decodeStream(open); if (decodeStream == null) { return null; } byte[] ninePatchChunk = decodeStream.getNinePatchChunk(); NinePatch.isNinePatchChunk(ninePatchChunk); return new NinePatchDrawable(decodeStream, ninePatchChunk, new Rect(), null); } createFromStream = Drawable.createFromStream(open, str); try { open.close(); return createFromStream; } catch (IOException e2) { e = e2; e.printStackTrace(); return createFromStream; } } catch (IOException e3) { IOException iOException = e3; createFromStream = null; e = iOException; e.printStackTrace(); return createFromStream; } }
public RecyclerViewItemDecoration(int recyclerviewMode, Context context, int drawableRid) { this.recyclerviewMode = recyclerviewMode; this.drawableRid = drawableRid; this.bmp = BitmapFactory.decodeResource(context.getResources(), drawableRid); if (bmp.getNinePatchChunk() != null) { hasNinePatch = true; ninePatch = new NinePatch(bmp, bmp.getNinePatchChunk(), null); } initPaint(); }
private Drawable a(String str, Context context) { IOException e; Drawable createFromStream; try { InputStream open = context.getApplicationContext().getAssets().open(str); if (open == null) { return null; } if (str.endsWith(".9.png")) { Bitmap decodeStream = BitmapFactory.decodeStream(open); if (decodeStream == null) { return null; } byte[] ninePatchChunk = decodeStream.getNinePatchChunk(); NinePatch.isNinePatchChunk(ninePatchChunk); return new NinePatchDrawable(decodeStream, ninePatchChunk, new Rect(), null); } createFromStream = Drawable.createFromStream(open, str); try { open.close(); return createFromStream; } catch (IOException e2) { e = e2; e.printStackTrace(); return createFromStream; } } catch (IOException e3) { IOException iOException = e3; createFromStream = null; e = iOException; e.printStackTrace(); return createFromStream; } }
public static SubjectFactory<NinePatchSubject, NinePatch> type() { return new SubjectFactory<NinePatchSubject, NinePatch>() { @Override public NinePatchSubject getSubject(FailureStrategy fs, NinePatch that) { return new NinePatchSubject(fs, that); } }; }
public static NinePatchDrawable createNinePatchDrawable(Bitmap bitmap,Resources res) { byte[] chunk = bitmap.getNinePatchChunk(); boolean result = NinePatch.isNinePatchChunk(chunk); if (!result) throw new ItsNatDroidException("Expected a 9 patch png, put a valid 9 patch in /res/drawable folder, generate the .apk (/build/outputs/apk in Android Studio), decompress as a zip and copy the png file"); return new NinePatchDrawable(res, bitmap, chunk, new Rect(), "XML 9 patch"); }
public static Drawable[] createDrawablesByImages(Resources res, Bitmap[] bitmaps) { Drawable[] drawables = new Drawable[bitmaps.length]; for (int i = 0; i < bitmaps.length; i++) { byte[] chunk = bitmaps[i].getNinePatchChunk(); boolean result = NinePatch.isNinePatchChunk(chunk); if (result) { drawables[i] = new NinePatchDrawable(res, bitmaps[i], chunk, new Rect(), null); } else { drawables[i] = new BitmapDrawable(res, bitmaps[i]); } } return drawables; }
@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) { method("setNinePatchChunk").withParameterTypes(byte[].class).in(bm).invoke(new byte[0]); } byte[] np = bm.getNinePatchChunk(); if (np == null || !NinePatch.isNinePatchChunk(np)) { np = null; pad = null; } int[] layoutBounds = method("getLayoutBounds").withReturnType(int[].class).in(bm).invoke(); Rect layoutBoundsRect = null; if (layoutBounds != null) { layoutBoundsRect = new Rect(layoutBounds[0], layoutBounds[1], layoutBounds[2], layoutBounds[3]); } if (np != null) { // todo: wrong return new NinePatchDrawable(res, bm, np, pad, /*layoutBoundsRect,*/ srcName); } return new BitmapDrawable(res, bm); } return null; }
public static NinePatchDrawable createNinePatch(Resources res, Bitmap bitmap, PXOffsets insets, String srcName) { byte[] chunk = createNinePatchChunk(insets); NinePatchDrawable drawable = new NinePatchDrawable(res, new NinePatch(bitmap, chunk, srcName)); return drawable; }
@SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Resources res = getResources(); // Bitmap from runtime creation (Not compiled) Bitmap run_bmp = createSampleBitmap(); ((ImageView)findViewById(R.id.run_org)).setImageBitmap(run_bmp); findViewById(R.id.run_bd).setBackgroundDrawable( new BitmapDrawable(res, run_bmp)); findViewById(R.id.run_nd).setBackgroundDrawable( NinePatchDrawableFactory.convertBitmap(res, run_bmp, null)); // Bitmap from resource (Compiled) Bitmap res_bmp = BitmapFactory.decodeResource(res, R.drawable.sample); ((ImageView)findViewById(R.id.res_org)).setImageBitmap(res_bmp); findViewById(R.id.res_bd).setBackgroundDrawable( new BitmapDrawable(res, res_bmp)); findViewById(R.id.res_nd).setBackgroundDrawable( new NinePatchDrawable(res, new NinePatch(res_bmp, res_bmp.getNinePatchChunk(), null))); findViewById(R.id.res_nd_pad).setBackgroundDrawable( NinePatchDrawableFactory.convertBitmap(res, res_bmp, null)); findViewById(R.id.res_nd_rid).setBackgroundResource(R.drawable.sample); }
public ArtworkFactory( Context context, int width, int height) { mContext = context; mRes = mContext.getResources(); mContentResolver = context.getContentResolver(); mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mWidth = width; mHeight = height; mContentLabelFontsize = mRes.getDimension(R.dimen.ContentLabel_fontsize); mBitmapOptions = new BitmapFactory.Options(); // RGB565 is OK for the artwork, 888 will be needed only when we add the shadow // Also no need for dithering. OpenGL will do a good job at displaying it without. //TODO: check if using ARGB888 is not faster since it will be converted after anyway mBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565; mBitmapOptions.inDither = ARTWORK_BITMAP_DITHERING; mBitmapOptions.inSampleSize = 1; // no sub-sampling mBitmapOptions.inJustDecodeBounds = false; // Prepare a painter and enable filtering if showing large bitmaps to avoid ugly rendering of the rescaled bitmap mPaint = new Paint(); mPaint.setFilterBitmap(ARTWORK_BITMAP_FILTERING); mPaint.setDither(ARTWORK_BITMAP_DITHERING); // ======== Now prepare the shadow stuff ========= // Create the destination bitmap and associate a canvas to it // Require ARGB for the shadow effect mShadowBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888 ); mShadowBitmap.eraseColor(Color.TRANSPARENT); mCanvas = new Canvas(mShadowBitmap); // Decode the shadow bitmap int shadowId = ArchosFeatures.isAndroidTV(mContext)|| ArchosFeatures.isLUDO()?R.drawable.cover_shadow_512:(mWidth==128) ? R.drawable.cover_shadow_128 : R.drawable.cover_shadow_256; InputStream is = context.getResources().openRawResource(shadowId); mShadow9patchPadding = new Rect(); // We must use this version of "decode" in order to get the nine-patch padding Bitmap shadowNinePatchBitmap = BitmapFactory.decodeStream(is, mShadow9patchPadding, null); try { is.close(); } catch (IOException e) {} mShadow9patch = new NinePatch(shadowNinePatchBitmap, shadowNinePatchBitmap.getNinePatchChunk(), null); }
public static NinePatch createNinePatch(Resources res, Bitmap bitmap, int top, int left, int bottom, int right, String srcName) { ByteBuffer buffer = getByteBuffer(top, left, bottom, right); NinePatch patch = new NinePatch(bitmap, buffer.array(), srcName); return patch; }
private Observable<String> getBubbleDrawable(String imageUrl, Context context) { return Observable.<String>create(subscriber -> { if (TextUtils.isEmpty(imageUrl)) { subscriber.onError(new NullPointerException()); return; } //先从内存缓存中获取 NinePatchDrawable drawable = mBubbleMap.get(imageUrl); if (null != drawable) { subscriber.onNext("bitmap has cached!"); subscriber.onCompleted(); return; } String filePath = ZjbImageLoader.getQiniuDiskCachePath(imageUrl); File imageFile = new File(filePath); if (imageFile.exists()) { filePath = imageFile.getAbsolutePath(); Rect rect = new Rect(); Bitmap bm = null; InputStream stream = null; try { stream = new FileInputStream(filePath); bm = BitmapFactory.decodeStream(stream, rect, null); byte[] chunk = bm.getNinePatchChunk(); if (NinePatch.isNinePatchChunk(chunk)) { //如果是合法的NinePatchDrawable NinePatchDrawable patchDrawable = new NinePatchDrawable(context.getResources(), bm, chunk, rect, null); mBubbleMap.put(imageUrl, patchDrawable); } subscriber.onNext("complete!"); subscriber.onCompleted(); } catch (FileNotFoundException e) { e.printStackTrace(); subscriber.onError(e); } } else { subscriber.onNext("image not download!"); subscriber.onCompleted(); } }).subscribeOn(Schedulers.io()); }
protected NinePatchSubject(FailureStrategy failureStrategy, NinePatch subject) { super(failureStrategy, subject); }
private NinePatchDrawable createMask(@DrawableRes int res) { final Bitmap maskBitmap = BitmapFactory.decodeResource(getResources(), res); final NinePatch patch = new NinePatch(maskBitmap, maskBitmap.getNinePatchChunk(), "BubbleMask"); return new NinePatchDrawable(getResources(), patch); }
/** * Attempts to decode 9-patch data from a {@link Bitmap}. * @param bitmap The {@link Bitmap} to check. * @return An instance of {@link NinePatchData} representing the 9-patch information * encoded in {@code bitmap} or {@code null} if the {@link Bitmap} wasn't a * 9-patch. */ public static NinePatchData create(Bitmap bitmap) { if (bitmap == null) return null; try { byte[] chunk = bitmap.getNinePatchChunk(); if (chunk == null || !NinePatch.isNinePatchChunk(chunk)) return null; ByteBuffer buffer = ByteBuffer.wrap(chunk).order(ByteOrder.nativeOrder()); // int8_t wasDeserialized if (buffer.get() == 0) return null; // int8_t numXDivs int numDivX = buffer.get(); if (numDivX == 0 || (numDivX & 0x01) != 0) return null; // int8_t numYDivs int numDivY = buffer.get(); if (numDivY == 0 || (numDivY & 0x01) != 0) return null; // int8_t numColors buffer.get(); // uint32_t xDivsOffset buffer.getInt(); // uint32_t yDivsOffset buffer.getInt(); Rect padding = new Rect(); // uint32_t paddingLeft padding.left = buffer.getInt(); // uint32_t paddingRight padding.right = buffer.getInt(); // uint32_t paddingTop padding.top = buffer.getInt(); // uint32_t paddingBottom padding.bottom = buffer.getInt(); // uint32_t colorsOffset buffer.getInt(); // uint32_t uint32_t uint32_t ... int[] divX = new int[numDivX]; for (int i = 0; i < numDivX; i++) divX[i] = buffer.getInt(); // uint32_t uint32_t uint32_t ... int[] divY = new int[numDivY]; for (int i = 0; i < numDivY; i++) divY[i] = buffer.getInt(); return new NinePatchData(bitmap.getWidth(), bitmap.getHeight(), padding, divX, divY); } catch (BufferUnderflowException ex) { return null; } }
public NinePatchAssert(NinePatch actual) { super(actual, NinePatchAssert.class); }
/** * 绘制 9Path * @param c * @param bmp * @param rect */ public void drawNinePath(Canvas c, Bitmap bmp, Rect rect){ NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null); patch.draw(c, rect); }
/** * 绘制 9Path * * @param c * @param bmp * @param rect */ public void drawNinePath(Canvas c, Bitmap bmp, Rect rect) { NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null); patch.draw(c, rect); }