Code Monkey home page Code Monkey logo

Comments (2)

AmosKorir avatar AmosKorir commented on June 18, 2024

At the moment that is not supported,

from avatarimagegenerator.

payal-iroid avatar payal-iroid commented on June 18, 2024

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)

        }

from avatarimagegenerator.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.