Gradient

Gradient (Linear, Sweep Gradient)

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

Activity file

package com.androidwork.gradientdemo;

import android.graphics.Shader;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.os.Bundle;
import android.view.View;

public class GradientDemoActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);
		setContentView(new ViewWithRedDot(this));

	}
}

class ViewWithRedDot extends View {
	public ViewWithRedDot(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLACK);
		Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		LinearGradient linGrad = new LinearGradient(0, 0, 25, 25,

		Color.RED, Color.GREEN, Shader.TileMode.REPEAT);
		circlePaint.setShader(linGrad);
		canvas.drawCircle(100, 100, 50, circlePaint);

		RadialGradient rg = new RadialGradient(50, 50, 10, Color.GREEN,
				Color.WHITE, Shader.TileMode.REPEAT);
		circlePaint.setShader(rg);
		canvas.drawCircle(150, 150, 50, circlePaint);
		// Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);

		SweepGradient sweepGrad = new SweepGradient(canvas.getWidth() - 175,
				canvas.getHeight() - 175, new int[] { Color.RED, Color.YELLOW,
						Color.GREEN, Color.BLUE, Color.MAGENTA }, null);

		circlePaint.setShader(sweepGrad);
		canvas.drawCircle(canvas.getWidth() - 175, canvas.getHeight() - 175,
				100, circlePaint);

		// Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// SweepGradient sweepGrad = new
		// SweepGradient(canvas.getWidth()-175,
		// canvas.getHeight()-175,
		// new int[] { Color.RED, Color.YELLOW, Color.GREEN,
		// Color.BLUE, Color.MAGENTA }, null);
		// circlePaint.setShader(sweepGrad);
		//
		// canvas.drawCircle(100, 100, 50, circlePaint);
		//
		// circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// SweepGradient sweepGrad = new
		// SweepGradient(canvas.getWidth()-175,
		// canvas.getHeight()-175,
		// new int[] { Color.RED, Color.YELLOW, Color.GREEN,
		// Color.BLUE, Color.MAGENTA }, null);
		// circlePaint.setShader(sweepGrad);
		// canvas.drawCircle(canvas.getWidth()-175,
		// canvas.getHeight()-175, 100,
		// circlePaint);
	}
}

Output

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s