简体   繁体   中英

Android, how to make a smooth gradient?

I'm trying to make a button background, yet it appears somehow jagged.

在此处输入图片说明

public Bitmap makeButtonBit (int w,int h)
{

    Bitmap bitmapBtn = Bitmap.createBitmap(w, h+2, Bitmap.Config.ARGB_8888);
    Canvas cnvBtn = new Canvas(bitmapBtn);

    Paint pR1 = new Paint();
    pR1.setAntiAlias(true);
    pR1.setAlpha(40);
    ///

    RectF rectShd1 = new RectF(0, 0, w, h+1);
    cnvBtn.drawRoundRect(rectShd1, 8, 8, pR1);

    RectF rectShd2 = new RectF(0, 0, w, h+2);
    cnvBtn.drawRoundRect(rectShd2, 8, 8, pR1);
    ///
    pR1.setAlpha(255);
    pR1.setShader(new LinearGradient(0, 0, 0, h, 0x11000000, 0xCC000000, TileMode.CLAMP));
    RectF rect1 = new RectF(0, 0, w, h);
    cnvBtn.drawRoundRect(rect1, 6, 6, pR1);

    pR1.setShader(new LinearGradient(0, 0, 0, h, 0xFF858990, 0xFF2E3337, TileMode.CLAMP));
    RectF rect2 = new RectF(1, 1, w-1, h-1);
    cnvBtn.drawRoundRect(rect2, 6, 6, pR1);

    pR1.setShader(new LinearGradient(0, 0, 0, h, 0xFF525A63, 0xFF2D343A, TileMode.CLAMP));
    RectF rect3 = new RectF(2, 2, w-2, h-2);
    cnvBtn.drawRoundRect(rect3, 6, 6, pR1);

    return bitmapBtn;
}

Any ideas what am I doing wrong?

Thanks.

为什么不使用九个补丁?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM