具體的看一下博文:Android學習筆記進階15之Shader渲染
package xiaosi.BitmapShader;</p><p>import android.app.Activity;<br />import android.os.Bundle;</p><p>public class BitmapShaderActivity extends Activity {<br /> /** Called when the activity is first created. */<br />private BitmapShaders bitmapShaders = null;<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);</p><p> bitmapShaders = new BitmapShaders(this);<br /> setContentView(bitmapShaders);<br /> }<br />}
BitmapShaders.java
package xiaosi.BitmapShader;</p><p>import android.content.Context;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapShader;<br />import android.graphics.Canvas;<br />import android.graphics.Color;<br />import android.graphics.ComposeShader;<br />import android.graphics.LinearGradient;<br />import android.graphics.Paint;<br />import android.graphics.PorterDuff;<br />import android.graphics.RadialGradient;<br />import android.graphics.Shader;<br />import android.graphics.SweepGradient;<br />import android.graphics.drawable.BitmapDrawable;<br />import android.graphics.drawable.ShapeDrawable;<br />import android.graphics.drawable.shapes.OvalShape;<br />import android.view.View;</p><p>public class BitmapShaders extends View<br />{<br />private LinearGradient linearGradient = null;<br />private ComposeShader composeShader = null;<br />private SweepGradient sweepGradient = null;<br />private RadialGradient radialGradient = null;<br />private BitmapShader bitmapShader = null;<br />private Bitmap bitmap = null;<br />private Paint paint = null;<br />private ShapeDrawable shapeDrawable = null;<br />private int BitmapWidth = 0;<br />private int BitmapHeight = 0;</p><p>public BitmapShaders(Context context)<br />{<br />super(context);<br />// 得到映像<br />bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.v))<br />.getBitmap();<br />BitmapWidth = bitmap.getWidth();<br />BitmapHeight = bitmap.getHeight();<br />// 構造渲染器LinearGradient<br />linearGradient = new LinearGradient(0, 0, 100, 100, new int[] {<br />Color.RED, Color.GREEN, Color.BLUE, Color.WHITE }, null,<br />Shader.TileMode.REPEAT);<br />paint = new Paint();<br />}</p><p>@Override<br />protected void onDraw(Canvas canvas)<br />{<br />super.onDraw(canvas);<br />paint.setShader(linearGradient);<br />canvas.drawRect(20, 50, 320, 156, paint);<br />}<br />}<br />