예제 #1
0
        public static Bitmap DrawTextToBitmap2(global::Android.Content.Context context, List <GifarooTextview> textList, int DrawableId, int gifDisplayDimensions)
        {
            float scale = context.Resources.DisplayMetrics.Density;
            //Bitmap bitmap = BitmapFactory.DecodeResource (context.Resources, DrawableId);
            Bitmap bitmap = Bitmap.CreateBitmap(gifDisplayDimensions, gifDisplayDimensions, Bitmap.Config.Argb8888);            //remove if fawefawe

            Bitmap.Config bitmapConfig = bitmap.GetConfig();
            if (bitmapConfig == null)
            {
                bitmapConfig = Bitmap.Config.Argb8888;
            }
            bitmap = bitmap.Copy(bitmapConfig, true);
            Canvas canvas = new Canvas(bitmap);
            Paint  paint  = new Paint(PaintFlags.AntiAlias);

            //Rect newRect = canvas.ClipBounds;
            //newRect.Inset (-600,-600);
            //canvas.ClipRect (newRect, Region.Op.Replace);
            foreach (GifarooTextview text in textList)
            {
                paint.Color = new global::Android.Graphics.Color(text.CurrentTextColor);
                //get the correct percentage multiplier (will be multiplied by the size of the drawable in which the text is drawn)
                float sizePercentage = 0f;
                switch (text.sizeChoiceString)
                {
                case "Small":
                    sizePercentage = 8f;
                    break;

                case "Medium":
                    sizePercentage = 13f;
                    break;

                case "Large":
                    sizePercentage = 21f;
                    break;

                case "Largest":
                    sizePercentage = 34f;
                    break;
                }
                float second = (sizePercentage / 100f);
                //float textSize = (second * 600f)*scale;
                float    textSize = second * gifDisplayDimensions;
                Typeface tf       = GifarooTextview.CreateTypeface(context, text.FontNameString);
                paint.SetTypeface(tf);
                paint.TextSize = text.TextSize;
                //paint.ElegantTextHeight = true;	//Throws exception because this method does not exist or something.

                Rect bounds = new Rect();
                paint.GetTextBounds(text.Text, 0, text.Length(), bounds);

                //x axis positioning
                float canvasSize    = (float)(context.Resources.DisplayMetrics.HeightPixels / 2);                     //The size of the relativelayout and the canvas
                float xs            = (float)text.Left + (float)text.PaddingLeft;                                     //number of pixels to the left of the text view
                float xResultsa     = xs / canvasSize;                                                                //
                float textViewWidth = ((float)text.Width / (float)canvasSize) * (float)bitmap.Width;
                float otherWidth    = (float)bounds.Width();
                float leftPadding   = (textViewWidth - otherWidth) / 2f;
                //float leftPadding = (otherWidth - textViewWidth) / 2f;
                float x = (float)((float)xResultsa * (float)bitmap.Width);                                                              //removed -leftpadding

                //y axis positioning
                float ys        = (float)text.Bottom;                                                                           //the amount of pixels from the bottom of the textview, to the bottom of the relativelayout.
                float yResultsa = (float)ys / (float)canvasSize;                                                                //divide the above 2

                //bounds.Height(); returns the height of the text to be drawm ignoring the padding on the top and bottom of the text.
                //float bottomPadding = ((float)text.Height - (float)bounds.Height()) / 2f;		//TODO: this is where the y position problem comes from
                //bottomPadding = 0; //TODO: test, remove
                float y = (float)((float)yResultsa * (float)bitmap.Height) - text.PaddingBottom;

                //global::Android.Views.ViewGroup.MarginLayoutParams parameters =
                //	(global::Android.Views.ViewGroup.MarginLayoutParams) text.LayoutParameters;
                //bottomPadding = parameters.BottomMargin;
                //float fff = (float)text.LineHeight/2f;
                //var RealDescent = text.Height - paint.Ascent() - bounds.Height();
                y = text.Bottom - paint.Descent() - text.PaddingBottom;
                canvas.DrawText(text.Text, x, y, paint);
            }

            return(bitmap);
        }
예제 #2
0
        /// <summary>
        /// Draws the text to bitmap wich is then saved as a png in the internal directory.
        /// </summary>
        /// <returns>The text to bitmap.</returns>
        /// <param name="context">Context.</param>
        /// <param name="textList">GifarooTextView list.</param>
        /// <param name="DrawableId">Path to a drawable (invisible in this case).</param>
        public static Bitmap DrawTextToBitmap(global::Android.Content.Context context, List <GifarooTextview> textList, int DrawableId, int gifDisplayDimensions)
        {
            float scale = context.Resources.DisplayMetrics.Density;
            //Bitmap bitmap = BitmapFactory.DecodeResource (context.Resources, DrawableId);
            Bitmap bitmap = Bitmap.CreateBitmap(gifDisplayDimensions, gifDisplayDimensions, Bitmap.Config.Argb8888);            //remove if fawefawe

            Bitmap.Config bitmapConfig = bitmap.GetConfig();
            if (bitmapConfig == null)
            {
                bitmapConfig = Bitmap.Config.Argb8888;
            }
            bitmap = bitmap.Copy(bitmapConfig, true);
            Canvas canvas = new Canvas(bitmap);
            Paint  paint  = new Paint(PaintFlags.AntiAlias);

            //Rect newRect = canvas.ClipBounds;
            //newRect.Inset (-600,-600);
            //canvas.ClipRect (newRect, Region.Op.Replace);
            foreach (GifarooTextview text in textList)
            {
                paint.Color = new global::Android.Graphics.Color(text.CurrentTextColor);
                //get the correct percentage multiplier (will be multiplied by the size of the drawable in which the text is drawn)
                float sizePercentage = 0f;
                switch (text.sizeChoiceString)
                {
                case "Small":
                    sizePercentage = 8f;
                    break;

                case "Medium":
                    sizePercentage = 13f;
                    break;

                case "Large":
                    sizePercentage = 21f;
                    break;

                case "Largest":
                    sizePercentage = 34f;
                    break;
                }
                float second = (sizePercentage / 100f);
                //float textSize = (second * 600f)*scale;
                float    textSize = second * gifDisplayDimensions;
                Typeface tf       = GifarooTextview.CreateTypeface(context, text.FontNameString);
                paint.SetTypeface(tf);
                paint.TextSize = textSize;


                Rect bounds = new Rect();
                paint.GetTextBounds(text.Text, 0, text.Length(), bounds);

                //x axis positioning
                float xDim          = (float)(context.Resources.DisplayMetrics.HeightPixels / 2);
                float xs            = (float)text.Left;
                float xResultsa     = xs / xDim;
                float textViewWidth = ((float)text.Width / (float)xDim) * (float)bitmap.Width;
                float otherWidth    = (float)bounds.Width();
                float leftPadding   = (textViewWidth - otherWidth) / 2f;
                //float leftPadding = (otherWidth - textViewWidth) / 2f;
                float x = (float)((float)xResultsa * (float)bitmap.Width);                //removed -leftpadding

                //y axis positioning
                float yDim           = (float)(context.Resources.DisplayMetrics.HeightPixels / 2f);           //the height of the gifdispay
                float ys             = (float)text.Bottom;                                                    //the amount fo pixels from the bottom to the top
                float yResultsa      = (float)ys / (float)yDim;                                               //divide the above 2
                float textViewHeight = ((float)text.Height / (float)yDim) * (float)bitmap.Height;             //((heightoftext/heigthofgifdisplay)*heigthof"canvas") what the height of the text should be on the new image
                float otherHeight    = (float)bounds.Height();                                                //heigth of rectbounds
                float bottomPadding  = (textViewHeight - otherHeight) / 2f;                                   //
                float y = (float)((float)yResultsa * (float)bitmap.Height) - bottomPadding;

                canvas.DrawText(text.Text, x, y, paint);
            }

            return(bitmap);
        }