Gradient In Android



Gradient are drawn in android using GradientDrawable.

The code is,


GradientDrawable grad = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{0xffffffff, 0xffff00ff, 0xffffff00,
0xff0000ff, 0xf0f0f0f0, 0xfefefefe});
grad.setBounds(0, 0, 320, 480);


then, in onDraw(), just call

grad.draw(canvas);

The entire source code is,


public class AndGradient extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewGradient(this));
}

class ViewGradient extends View {

GradientDrawable grad;
public ViewGradient(Context context) {
super(context);
grad = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[]{0xffffffff, 0xffff00ff, 0xffffff00,
0xff0000ff, 0xf0f0f0f0, 0xfefefefe});
grad.setBounds(0, 0, 320, 480);
}

public void onDraw(Canvas canvas) {
super.onDraw(canvas);
grad.draw(canvas);
}
}
}

Comments

Popular posts from this blog

AlarmManager and Notification in Android

Swipe Action and ViewFlipper in Android

Open Gallery and get the selected image in appplication