Exemple #1
0
        private static void SetupPadding(Bitmap bitmap, NinePatchChunk chunck)
        {
            int maxXPixels = bitmap.Width - 2;
            int maxYPixels = bitmap.Height - 2;
            var xPaddings  = GetXDivs(bitmap, bitmap.Height - 1);

            if (xPaddings.Count > 1)
            {
                throw new System.Exception("Raw padding is wrong. Should be only one horizontal padding region");
            }
            var yPaddings = GetYDivs(bitmap, bitmap.Width - 1);

            if (yPaddings.Count > 1)
            {
                throw new System.Exception("Column padding is wrong. Should be only one vertical padding region");
            }
            if (xPaddings.Count == 0)
            {
                xPaddings.Add(chunck.xDivs[0]);
            }
            if (yPaddings.Count == 0)
            {
                yPaddings.Add(chunck.yDivs[0]);
            }
            chunck.padding        = new Rect();
            chunck.padding.Left   = xPaddings[0].start;
            chunck.padding.Right  = maxXPixels - xPaddings[0].stop;
            chunck.padding.Top    = yPaddings[0].start;
            chunck.padding.Bottom = maxYPixels - yPaddings[0].stop;
        }
Exemple #2
0
        /// <summary>
        /// Setup colors.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="chunck"></param>
        private static void SetupColors(Bitmap bitmap, NinePatchChunk chunck)
        {
            int bitmapWidth  = bitmap.Width - 2;
            int bitmapHeight = bitmap.Height - 2;
            var xRegions     = GetRegions(chunck.xDivs, bitmapWidth);
            var yRegions     = GetRegions(chunck.yDivs, bitmapHeight);

            chunck.colors = new int[xRegions.Count * yRegions.Count];

            int colorIndex = 0;

            foreach (Div yDiv in yRegions)
            {
                foreach (Div xDiv in xRegions)
                {
                    int startX = xDiv.start + 1;
                    int startY = yDiv.start + 1;
                    if (HasSameColor(bitmap, startX, xDiv.stop + 1, startY, yDiv.stop + 1))
                    {
                        int pixel = bitmap.GetPixel(startX, startY);
                        if (isTransparent(pixel))
                        {
                            pixel = TRANSPARENT_COLOR;
                        }
                        chunck.colors[colorIndex] = pixel;
                    }
                    else
                    {
                        chunck.colors[colorIndex] = NO_COLOR;
                    }
                    colorIndex++;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates a ninepatch chunkc from a raw image.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static NinePatchChunk CreateChunkFromRawBitmap(Bitmap bitmap)
        {
            var chunck = new NinePatchChunk();

            SetupStretchableRegions(bitmap, chunck);
            SetupPadding(bitmap, chunck);
            SetupColors(bitmap, chunck);
            return(chunck);
        }
Exemple #4
0
 private static void SetupStretchableRegions(Bitmap bitmap, NinePatchChunk chunck)
 {
     chunck.xDivs = GetXDivs(bitmap, 0);
     if (chunck.xDivs.Count == 0)
     {
         throw new System.Exception("must be at least one horizontal stretchable region");
     }
     chunck.yDivs = GetYDivs(bitmap, 0);
     if (chunck.yDivs.Count == 0)
     {
         throw new System.Exception("must be at least one vertical stretchable region");
     }
 }
Exemple #5
0
        protected static Bitmap ModifyBitmap(Resources resources, Bitmap bitmap, NinePatchChunk chunk)
        {
            Bitmap content = Bitmap.CreateBitmap(bitmap, 1, 1, bitmap.Width - 2, bitmap.Height - 2);
            int targetDensity = (int)resources.DisplayMetrics.DensityDpi;
            float densityChange = (float)targetDensity / bitmap.Density;
            if (densityChange != 1f)
            {
                int dstWidth =(int)System.Math.Round(content.Width * densityChange);
                int dstHeight = (int)System.Math.Round(content.Height * densityChange);
                content = Bitmap.CreateScaledBitmap(content, dstWidth, dstHeight, true);
                content.Density = targetDensity;
                chunk.padding = new Rect((int)System.Math.Round(chunk.padding.Left * densityChange),
                        (int)System.Math.Round(chunk.padding.Top * densityChange),
                        (int)System.Math.Round(chunk.padding.Right * densityChange),
                        (int)System.Math.Round(chunk.padding.Bottom * densityChange));

                recalculateDivs(densityChange, chunk.xDivs);
                recalculateDivs(densityChange, chunk.yDivs);
            }
            bitmap = content;
            return content;
        }
Exemple #6
0
        protected static Bitmap ModifyBitmap(Resources resources, Bitmap bitmap, NinePatchChunk chunk)
        {
            Bitmap content       = Bitmap.CreateBitmap(bitmap, 1, 1, bitmap.Width - 2, bitmap.Height - 2);
            int    targetDensity = (int)resources.DisplayMetrics.DensityDpi;
            float  densityChange = (float)targetDensity / bitmap.Density;

            if (densityChange != 1f)
            {
                int dstWidth  = (int)System.Math.Round(content.Width * densityChange);
                int dstHeight = (int)System.Math.Round(content.Height * densityChange);
                content         = Bitmap.CreateScaledBitmap(content, dstWidth, dstHeight, true);
                content.Density = targetDensity;
                chunk.padding   = new Rect((int)System.Math.Round(chunk.padding.Left * densityChange),
                                           (int)System.Math.Round(chunk.padding.Top * densityChange),
                                           (int)System.Math.Round(chunk.padding.Right * densityChange),
                                           (int)System.Math.Round(chunk.padding.Bottom * densityChange));

                recalculateDivs(densityChange, chunk.xDivs);
                recalculateDivs(densityChange, chunk.yDivs);
            }
            bitmap = content;
            return(content);
        }
Exemple #7
0
        private static void SetupStretchableRegions(Bitmap bitmap, NinePatchChunk chunck)
        {
	        chunck.xDivs = GetXDivs(bitmap, 0);
		    if (chunck.xDivs.Count == 0)
                throw new System.Exception("must be at least one horizontal stretchable region");
		    chunck.yDivs = GetYDivs(bitmap, 0);
		    if (chunck.yDivs.Count == 0)
                throw new System.Exception("must be at least one vertical stretchable region");
        }
Exemple #8
0
        private static void SetupPadding(Bitmap bitmap, NinePatchChunk chunck)
        {
		    int maxXPixels = bitmap.Width - 2;
		    int maxYPixels = bitmap.Height - 2;
            var xPaddings = GetXDivs(bitmap, bitmap.Height - 1);
		    if (xPaddings.Count > 1)
			    throw new System.Exception("Raw padding is wrong. Should be only one horizontal padding region");
		    var yPaddings = GetYDivs(bitmap, bitmap.Width - 1);
		    if (yPaddings.Count > 1)
                throw new System.Exception("Column padding is wrong. Should be only one vertical padding region");
		    if (xPaddings.Count == 0) xPaddings.Add(chunck.xDivs[0]);
		    if (yPaddings.Count == 0) yPaddings.Add(chunck.yDivs[0]);
		    chunck.padding = new Rect();
		    chunck.padding.Left = xPaddings[0].start;
		    chunck.padding.Right = maxXPixels - xPaddings[0].stop;
		    chunck.padding.Top = yPaddings[0].start;
		    chunck.padding.Bottom = maxYPixels - yPaddings[0].stop;
        }
Exemple #9
0
        /// <summary>
        /// Setup colors.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="chunck"></param>
        private static void SetupColors(Bitmap bitmap, NinePatchChunk chunck)
        {            		
		    int bitmapWidth = bitmap.Width - 2;
		    int bitmapHeight = bitmap.Height - 2;
		    var xRegions = GetRegions(chunck.xDivs, bitmapWidth);
		    var yRegions = GetRegions(chunck.yDivs, bitmapHeight);
		    chunck.colors = new int[xRegions.Count * yRegions.Count];

		    int colorIndex = 0;
		    foreach (Div yDiv in yRegions) {
			    foreach (Div xDiv in xRegions) {
				    int startX = xDiv.start + 1;
				    int startY = yDiv.start + 1;
				    if (HasSameColor(bitmap, startX, xDiv.stop + 1, startY, yDiv.stop + 1)) {
					    int pixel = bitmap.GetPixel(startX, startY);
					    if (isTransparent(pixel))
						    pixel = TRANSPARENT_COLOR;
					    chunck.colors[colorIndex] = pixel;
				    } else {
                        chunck.colors[colorIndex] = NO_COLOR;
				    }
				    colorIndex++;
			    }
		    }
        }
Exemple #10
0
        /// <summary>
        /// Creates a ninepatch chunkc from a raw image.
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static NinePatchChunk CreateChunkFromRawBitmap(Bitmap bitmap)
        {
		    var chunck = new NinePatchChunk();
		    SetupStretchableRegions(bitmap, chunck);
		    SetupPadding(bitmap, chunck);
		    SetupColors(bitmap, chunck);
            return chunck;
	    }
Exemple #11
0
        /// <summary>
        /// Creates a ninepatch drawable.
        /// </summary>
        /// <param name="resources"></param>
        /// <param name="bitmap"></param>
        /// <param name="srcName"></param>
        /// <returns></returns>
        public static NinePatchDrawable createNinePatchDrawable(Resources resources, Bitmap bitmap, string srcName)
        {
            NinePatchChunk chunk = CreateChunkFromRawBitmap(bitmap);

            return(new NinePatchDrawable(resources, ModifyBitmap(resources, bitmap, chunk), chunk.toBytes(), chunk.padding, srcName));
        }