コード例 #1
0
        /// <summary>
        /// Helper method to provide the corresponding Icon for a station
        /// </summary>
        /// <param name=""></param>
        /// <returns></returns>
        public BitmapDescriptor CreateStationIcon(Station station)
        {
            Bitmap bitmap = null;
            var value = _settingsService.Settings.IsBikeMode ? station.AvailableBikes : station.AvailableBikeStands;
            var printedValue = value.HasValue ? value.Value.ToString() : string.Empty;
            if (!station.Loaded)
            {
                printedValue = string.Empty;
                bitmap = _iconGreyLowAlpha;
                bitmap = bitmap.Copy(bitmap.GetConfig(), true);
            }
            else if (station.Status == false)
            {
                printedValue = "!";
                bitmap = _iconGrey;
                bitmap = bitmap.Copy(bitmap.GetConfig(), true);
            }
            else if (station.ImageAvailable != null || station.ImageDocks != null)
            {
                if (_metrics == null)
                {
                    _metrics = new DisplayMetrics();
                    (CrossCurrentActivity.Current.Activity as MainActivity).WindowManager.DefaultDisplay.GetMetrics(_metrics);
                }

                bitmap = _iconGrey;
                bitmap = bitmap.Copy(bitmap.GetConfig(), true);
                try
                {
                    var data = (byte[])(_settingsService.Settings.IsBikeMode ? station.ImageAvailable : station.ImageDocks);
                    var gifDecoder = new GifDecoder();
                    gifDecoder.read(data);
                    if (gifDecoder.getFrameCount() != 0)
                    {
                        gifDecoder.advance();
                        var bmp = gifDecoder.getNextFrame();
                        var canvas = new Canvas(bitmap);

                        int width = bmp.Width;
                        int height = bmp.Height;
                        float scaleWidth = _metrics.ScaledDensity;
                        float scaleHeight = _metrics.ScaledDensity;
                        // create a matrix for the scaling manipulation
                        Matrix matrix = new Matrix();
                        // resize the bitmap
                        matrix.PostScale(scaleWidth, scaleHeight);

                        // recreate the new Bitmap
                        var resizedBitmap = Bitmap.CreateBitmap(bmp, 0, 0, width, height, matrix, true);

                        int xPos = canvas.Width / 2 - resizedBitmap.Width / 2;
                        int yPos = canvas.Height / 2 - resizedBitmap.Height;
                        canvas.DrawBitmap(resizedBitmap, xPos, yPos, null);
                    }

                }
                catch (System.Exception e)
                {
                }

            }
            else {
                if (value == 0)
                {
                    bitmap = _iconRed;
                }
                else if (value < 5)
                {
                    bitmap = _iconOrange;
                }
                else if (value >= 5)
                {
                    bitmap = _iconGreen;
                }
                else
                {
                    printedValue = "?";
                    bitmap = _iconGrey;
                }
            }

            if (printedValue != string.Empty)
            {
                bitmap = bitmap.Copy(bitmap.GetConfig(), true);
                Canvas canvas = new Canvas(bitmap);
                int xPos = (canvas.Width / 2);
                int yPos = (int)((canvas.Height / 2) - ((_textPaint.Descent() + _textPaint.Ascent()) / 2));
                canvas.DrawText(printedValue, xPos + 1, yPos - LayoutHelper.ConvertDpToPixel(6), _textPaint);
            }

            var icon = BitmapDescriptorFactory.FromBitmap(bitmap);
            bitmap.Recycle();
            return icon;
        }