@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; }
@Override protected void onDraw(Canvas canvas) { if (getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // use topY if (indicatorBitmap != null) { // canvas.drawBitmap(indicatorBitmap, 0, topY, null); ninePatch.draw(canvas, rectD); } } else { // use topX if (indicatorBitmap != null) { ninePatch.draw(canvas, rectD); // canvas.drawBitmap(indicatorBitmap, new Rect(0,0,indicatorBitmap.getWidth(), // indicatorBitmap.getHeight()), new Rect(leftX, 0, leftX+lp.width, lp.height), null); } } }
public static Drawable getDraw9(final InitPackageResourcesParam resparam, final String nama) { Bitmap bitmap = BitmapFactory.decodeFile(S.DEFAULT_ICON_DIR + "/" + nama + ".png"); final byte[] chunk = bitmap.getNinePatchChunk(); if (NinePatch.isNinePatchChunk(chunk)) return new NinePatchDrawable( resparam.res, bitmap, chunk, Npatch.deserialize(chunk).mPaddings, null); else return Drawable.createFromPath(S.DEFAULT_ICON_DIR + "/" + nama + ".png"); }
private static Drawable getNinePathDrawable(InitPackageResourcesParam resparam, String str) { Bitmap bitmap = BitmapFactory.decodeFile(str); final byte[] chunk = bitmap.getNinePatchChunk(); Drawable d; if (NinePatch.isNinePatchChunk(chunk)) { d = new NinePatchDrawable( resparam.res, bitmap, chunk, Npatch.deserialize(chunk).mPaddings, null); } else { d = Drawable.createFromPath(str); } return d; }
private void computeBitmapSize() { final int sdensity = mNinePatch.getDensity(); final int tdensity = mTargetDensity; if (sdensity == tdensity) { mBitmapWidth = mNinePatch.getWidth(); mBitmapHeight = mNinePatch.getHeight(); mOpticalInsets = mNinePatchState.mOpticalInsets; } else { mBitmapWidth = Bitmap.scaleFromDensity(mNinePatch.getWidth(), sdensity, tdensity); mBitmapHeight = Bitmap.scaleFromDensity(mNinePatch.getHeight(), sdensity, tdensity); if (mNinePatchState.mPadding != null && mPadding != null) { Rect dest = mPadding; Rect src = mNinePatchState.mPadding; if (dest == src) { mPadding = dest = new Rect(src); } dest.left = Bitmap.scaleFromDensity(src.left, sdensity, tdensity); dest.top = Bitmap.scaleFromDensity(src.top, sdensity, tdensity); dest.right = Bitmap.scaleFromDensity(src.right, sdensity, tdensity); dest.bottom = Bitmap.scaleFromDensity(src.bottom, sdensity, tdensity); } mOpticalInsets = scaleFromDensity(mNinePatchState.mOpticalInsets, sdensity, tdensity); } }
/** * Create a drawable from an inputstream, using the given resources and value to determine density * information. */ public static Drawable createFromResourceStream( Resources res, TypedValue value, InputStream is, String srcName, BitmapFactory.Options opts) { if (is == null) { return null; } /* ugh. The decodeStream contract is that we have already allocated the pad rect, but if the bitmap does not had a ninepatch chunk, then the pad will be ignored. If we could change this to lazily alloc/assign the rect, we could avoid the GC churn of making new Rects only to drop them on the floor. */ Rect pad = new Rect(); // Special stuff for compatibility mode: if the target density is not // the same as the display density, but the resource -is- the same as // the display density, then don't scale it down to the target density. // This allows us to load the system's density-correct resources into // an application in compatibility mode, without scaling those down // to the compatibility density only to have them scaled back up when // drawn to the screen. if (opts == null) opts = new BitmapFactory.Options(); opts.inScreenDensity = res != null ? res.getDisplayMetrics().noncompatDensityDpi : DisplayMetrics.DENSITY_DEVICE; Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); if (bm != null) { byte[] np = bm.getNinePatchChunk(); if (np == null || !NinePatch.isNinePatchChunk(np)) { np = null; pad = null; } final Rect opticalInsets = new Rect(); bm.getOpticalInsets(opticalInsets); return drawableFromBitmap(res, bm, np, pad, opticalInsets, srcName); } return null; }
@Override public void draw(Canvas canvas) { final Rect bounds = getBounds(); final boolean clearColorFilter; if (mTintFilter != null && getPaint().getColorFilter() == null) { mPaint.setColorFilter(mTintFilter); clearColorFilter = true; } else { clearColorFilter = false; } final boolean needsMirroring = needsMirroring(); if (needsMirroring) { // Mirror the 9patch canvas.translate(bounds.right - bounds.left, 0); canvas.scale(-1.0f, 1.0f); } final int restoreAlpha; if (mNinePatchState.mBaseAlpha != 1.0f) { restoreAlpha = mPaint.getAlpha(); mPaint.setAlpha((int) (restoreAlpha * mNinePatchState.mBaseAlpha + 0.5f)); } else { restoreAlpha = -1; } mNinePatch.draw(canvas, bounds, mPaint); if (clearColorFilter) { mPaint.setColorFilter(null); } if (restoreAlpha >= 0) { mPaint.setAlpha(restoreAlpha); } }
public void drawNinepath(Canvas c, Rect r1) { NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null); patch.draw(c, r1); }
/** * 手动绘制.9.png图片 * * @param canvas * @param resId * @param rect */ private void drawNinepath(Canvas canvas, int resId, Rect rect) { Bitmap bmp = BitmapFactory.decodeResource(getResources(), resId); NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null); patch.draw(canvas, rect); }
@Override public Bitmap getBitmap() { return mNinePatch.getBitmap(); }
@Override public Region getTransparentRegion() { return mNinePatch.getTransparentRegion(getBounds()); }
/** * Returns a {@link android.graphics.PixelFormat graphics.PixelFormat} value of OPAQUE or * TRANSLUCENT. */ @Override public int getOpacity() { return mNinePatch.hasAlpha() || (mPaint != null && mPaint.getAlpha() < 255) ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE; }