Thursday, February 7, 2013

Dynamic gradient in android




Step One:- create a class
import android.graphics.drawable.GradientDrawable;

public class DrawableGradient extends GradientDrawable {
public DrawableGradient(int[] colors, int cornerRadius) {
super(GradientDrawable.Orientation.BR_TL, colors);

try {
this.setShape(GradientDrawable.RECTANGLE);
this.setGradientType(GradientDrawable.RECTANGLE);
this.setCornerRadius(cornerRadius);
} catch (Exception e) {
e.printStackTrace();
}
}

public DrawableGradient SetTransparency(int transparencyPercent) {
this.setAlpha(255 - ((255 * transparencyPercent) / 100));

return this;
}
}

step 2 : use this
TextView.setBackgroundDrawable(new DrawableGradient(new int[] {
0xFFFF0000, 0xFF800000, 0xFF000000 }, 0).SetTransparency(10));

No comments:

Post a Comment