Ejemplo n.º 1
0
        public override void inflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser
                                     parser, android.util.AttributeSet attrs)
        {
            base.inflate(r, parser, attrs);
            android.content.res.TypedArray a = r.obtainAttributes(attrs, [email protected].
                                                                  styleable.NinePatchDrawable);
            int id = a.getResourceId([email protected]_src, 0);

            if (id == 0)
            {
                throw new org.xmlpull.v1.XmlPullParserException(parser.getPositionDescription() +
                                                                ": <nine-patch> requires a valid src attribute");
            }
            bool dither = a.getBoolean([email protected]_dither
                                       , DEFAULT_DITHER);

            android.graphics.BitmapFactory.Options options = new android.graphics.BitmapFactory
                                                             .Options();
            if (dither)
            {
                options.inDither = false;
            }
            options.inScreenDensity = android.util.DisplayMetrics.DENSITY_DEVICE;
            android.graphics.Rect   padding = new android.graphics.Rect();
            android.graphics.Bitmap bitmap  = null;
            try
            {
                android.util.TypedValue value = new android.util.TypedValue();
                java.io.InputStream     @is   = r.openRawResource(id, value);
                bitmap = android.graphics.BitmapFactory.decodeResourceStream(r, value, @is, padding
                                                                             , options);
                @is.close();
            }
            catch (System.IO.IOException)
            {
            }
            // Ignore
            if (bitmap == null)
            {
                throw new org.xmlpull.v1.XmlPullParserException(parser.getPositionDescription() +
                                                                ": <nine-patch> requires a valid src attribute");
            }
            else
            {
                if (bitmap.getNinePatchChunk() == null)
                {
                    throw new org.xmlpull.v1.XmlPullParserException(parser.getPositionDescription() +
                                                                    ": <nine-patch> requires a valid 9-patch source image");
                }
            }
            setNinePatchState(new android.graphics.drawable.NinePatchDrawable.NinePatchState(
                                  new android.graphics.NinePatch(bitmap, bitmap.getNinePatchChunk(), "XML 9-patch"
                                                                 ), padding, dither), r);
            mNinePatchState.mTargetDensity = mTargetDensity;
            a.recycle();
        }
Ejemplo n.º 2
0
        private static android.graphics.Bitmap finishDecode(android.graphics.Bitmap bm, android.graphics.Rect
                                                            outPadding, android.graphics.BitmapFactory.Options opts)
        {
            if (bm == null || opts == null)
            {
                return(bm);
            }
            int density = opts.inDensity;

            if (density == 0)
            {
                return(bm);
            }
            bm.setDensity(density);
            int targetDensity = opts.inTargetDensity;

            if (targetDensity == 0 || density == targetDensity || density == opts.inScreenDensity)
            {
                return(bm);
            }
            byte[] np          = bm.getNinePatchChunk();
            bool   isNinePatch = np != null && android.graphics.NinePatch.isNinePatchChunk(np);

            if (opts.inScaled || isNinePatch)
            {
                float scale = targetDensity / (float)density;
                // TODO: This is very inefficient and should be done in native by Skia
                android.graphics.Bitmap oldBitmap = bm;
                bm = android.graphics.Bitmap.createScaledBitmap(oldBitmap, (int)(bm.getWidth() *
                                                                                 scale + 0.5f), (int)(bm.getHeight() * scale + 0.5f), true);
                oldBitmap.recycle();
                if (isNinePatch)
                {
                    np = nativeScaleNinePatch(np, scale, outPadding);
                    bm.setNinePatchChunk(np);
                }
                bm.setDensity(targetDensity);
            }
            return(bm);
        }