Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

gradient color in background #16

Open
kalpeshat20 opened this issue Jul 14, 2021 · 2 comments
Open

gradient color in background #16

kalpeshat20 opened this issue Jul 14, 2021 · 2 comments

Comments

@kalpeshat20
Copy link

is there any way to add a gradient color in background?

@AmosKorir
Copy link
Owner

At the moment that is not supported,

@payal-iroid
Copy link

payal-iroid commented Dec 6, 2023

I have modified the avatarImageGenerate() function to display gradient background and it is working fine.
Hope this helps @kalpeshat20

   private fun avatarImageGenerate(context: Context, size: Int, shape: Int, name: String, colorStr: String): BitmapDrawable {
            uiContext = context
            texSize = calTextSize(size)
            val label = firstCharacter(name)
            val textPaint = textPainter()
            val painter = painter()
            painter.isAntiAlias = true
            val areaRect = Rect(0, 0, size, size)

            if (shape == 0) {
                val firstLetter = firstCharacter(name)
//                val r = firstLetter[0]
                //Random rnd = new Random();
                //int color = generateRandomColor();// Color.argb(255, rnd.nextInt(100), rnd.nextInt(150), rnd.nextInt(256));
                val color = if (!colorStr.isNullOrEmpty()) Color.parseColor(colorStr) else RandomColors().getColor()

                painter.color = color
                painter.shader = LinearGradient(size.toFloat() / 3, size.toFloat() / 4, 0F, size.toFloat(), color, Color.WHITE, Shader.TileMode.CLAMP)
            } else {
                painter.color = Color.TRANSPARENT
            }

            val bitmap = Bitmap.createBitmap(size, size, ARGB_8888)
            val canvas = Canvas(bitmap)
            canvas.drawRect(areaRect, painter)

            //reset painter
            if (shape == 0) {
                painter.color = Color.TRANSPARENT
            } else {
                val firstLetter = firstCharacter(name)
                val color = if (!colorStr.isNullOrEmpty()) Color.parseColor(colorStr) else RandomColors().getColor()
                painter.color = color;
                painter.shader = LinearGradient(size.toFloat() / 3, size.toFloat() / 4, 0F, size.toFloat(), color, Color.WHITE, Shader.TileMode.CLAMP);
            }

         
            val stringWidth = textPaint.measureText(label);
            val xPosition = (canvas.clipBounds.width() / 2f) - (stringWidth / 2f);
            val yPosition = (canvas.clipBounds.height() / 2f) - ((textPaint.ascent() + textPaint.descent()) / 2f);
            canvas.drawCircle(size.toFloat() / 2, size.toFloat() / 2, size.toFloat() / 2, painter);
            canvas.drawText(label, xPosition, yPosition, textPaint);
            return BitmapDrawable(uiContext.resources, bitmap)

        }

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants