<1>簡介
具體的看一下博文:Android學習筆記進階15之Shader渲染
public BitmapShader(Bitmap bitmap,Shader.TileMode tileX,Shader.TileMode tileY)
調用這個方法來產生一個畫有一個位元影像的渲染器(Shader)。
bitmap 在渲染器內使用的位元影像
tileX The tiling mode for x to draw the bitmap in. 在位元影像上X方向花磚模式
tileY The tiling mode for y to draw the bitmap in. 在位元影像上Y方向花磚模式
TileMode:(一共有三種)
CLAMP :如果渲染器超出原始邊界範圍,會複製範圍內邊緣染色。
REPEAT :橫向和縱向的重複渲染器圖片,平鋪。
MIRROR :橫向和縱向的重複渲染器圖片,這個和REPEAT 重複方式不一樣,他是以鏡像方式平鋪。
還是不太明白?那看一下吧!
REPEAT MIRROR
<2>具體實現
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.Paint;<br />import android.graphics.Shader;<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 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;<br />public BitmapShaders(Context context)<br />{<br />super(context);<br />//得到映像<br />bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.h)).getBitmap();<br />BitmapWidth = bitmap.getWidth();<br />BitmapHeight = bitmap.getHeight();<br />//構造渲染器BitmapShader<br />bitmapShader = new BitmapShader(bitmap,Shader.TileMode.MIRROR,Shader.TileMode.REPEAT);<br />}<br />@Override<br />protected void onDraw(Canvas canvas)<br />{<br />super.onDraw(canvas);<br />//將圖片裁剪為橢圓形<br /> //構建ShapeDrawable對象並定義形狀為橢圓<br />shapeDrawable = new ShapeDrawable(new OvalShape());<br />//得到畫筆並設定渲染器<br />shapeDrawable.getPaint().setShader(bitmapShader);<br />//設定顯示地區<br />shapeDrawable.setBounds(20, 20,BitmapWidth-60,BitmapHeight-60);<br />//繪製shapeDrawable<br />shapeDrawable.draw(canvas);<br />}<br />}<br />