Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android ApiDemos示例解析(71):Graphics->Patterns

Android ApiDemos示例解析(71):Graphics->Patterns

編輯:關於Android編程

Patterns指可以使用位圖(材質)來填充某個圖形。本例使用兩個位圖來填充屏幕:

一是外藍裡紅的矩形

[java] 
private static Bitmap makeBitmap1() { 
 Bitmap bm = Bitmap.createBitmap(40, 40, 
 Bitmap.Config.RGB_565); 
 Canvas c = new Canvas(bm); 
 c.drawColor(Color.RED); 
 Paint p = new Paint(); 
 p.setColor(Color.BLUE); 
 c.drawRect(5, 5, 35, 35, p); 
 return bm; 

private static Bitmap makeBitmap1() {
 Bitmap bm = Bitmap.createBitmap(40, 40,
 Bitmap.Config.RGB_565);
 Canvas c = new Canvas(bm);
 c.drawColor(Color.RED);
 Paint p = new Paint();
 p.setColor(Color.BLUE);
 c.drawRect(5, 5, 35, 35, p);
 return bm;
}另外一個為綠圓:

[java]

private static Bitmap makeBitmap2() { 
 Bitmap bm = Bitmap.createBitmap(64, 64, 
 Bitmap.Config.ARGB_8888); 
 Canvas c = new Canvas(bm); 
 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); 
 p.setColor(Color.GREEN); 
 p.setAlpha(0xCC); 
 c.drawCircle(32, 32, 27, p); 
 return bm; 

private static Bitmap makeBitmap2() {
 Bitmap bm = Bitmap.createBitmap(64, 64,
 Bitmap.Config.ARGB_8888);
 Canvas c = new Canvas(bm);
 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
 p.setColor(Color.GREEN);
 p.setAlpha(0xCC);
 c.drawCircle(32, 32, 27, p);
 return bm;
}然後創建Shader(類似於GDI中的TextureBrush)

[java]

mShader1 = new BitmapShader(makeBitmap1(), 
 Shader.TileMode.REPEAT, 
 Shader.TileMode.REPEAT); 
 mShader2 = new BitmapShader(makeBitmap2(), 
 Shader.TileMode.REPEAT, 
 Shader.TileMode.REPEAT); 

mShader1 = new BitmapShader(makeBitmap1(),
 Shader.TileMode.REPEAT,
 Shader.TileMode.REPEAT);
 mShader2 = new BitmapShader(makeBitmap2(),
 Shader.TileMode.REPEAT,
 Shader.TileMode.REPEAT);public BitmapShader(Bitmap bitmap, Shader.TileMode tileX, Shader.TileMode tileY)

bitmap: 構造Shader的Bitmap對象。
tileX: X 方向的tile 模式: 重復REPEAT, 鏡像MIRROR ,緊靠邊EDGE
tileY: Y 方向的tile 模式: 重復REPEAT, 鏡像MIRROR ,緊靠邊EDGE
本例創建的兩個Shader都是選用的重復平鋪模式。當Shader2旋轉了30度,所有圓的序列看起來用個角度

[java]

Matrix m = new Matrix(); 
m.setRotate(30); 
mShader2.setLocalMatrix(m); 

Matrix m = new Matrix();
m.setRotate(30);
mShader2.setLocalMatrix(m);[java] view plaincopyprint?
此外,為了達到加速繪圖,使用PaintFlagsDrawFilter關閉了DITHER_FLAG和FILTER_BITMAP_FLAG。 

此外,為了達到加速繪圖,使用PaintFlagsDrawFilter關閉了DITHER_FLAG和FILTER_BITMAP_FLAG。[java] view plaincopyprint?
<PRE class=java name="code">mFastDF = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG | 
 Paint.DITHER_FLAG, 
 0);</PRE><BR> 
<PRE></PRE> 
<PRE class=java name="code"> </PRE> 
<P>最後看看onDraw</P> 
<PRE class=java name="code">canvas.setDrawFilter(mDF); 
  
mPaint.setShader(mShader1); 
canvas.drawPaint(mPaint); 
  
canvas.translate(mTouchCurrX - mTouchStartX, 
 mTouchCurrY - mTouchStartY); 
  
mPaint.setShader(mShader2); 
canvas.drawPaint(mPaint);</PRE> 
<P>使用Patterns填充是設置Paint的shader屬性,</P> 
<P>canvas.translate(mTouchCurrX – mTouchStartX,mTouchCurrY – mTouchStartY); 在繪制Shader2( 綠圓)時平移觸摸屏移動的距離,效果就是綠圓跟著手指動。</P> 
<P><A href="http://up.2cto.com/2012/0717/20120717084124727.png"><IMG class="aligncenter size-full wp-image-1595" title=20110603001 height=490 alt="" src="http://up.2cto.com/2012/0717/20120717084124727.png" width=328></A><BR> 
</P> 
<P> </P> 
<P><BR> 
 </P> 
<P><BR> 
 </P> 
<P><BR> 
 </P> 
<P><BR> 
 </P> 
<P> </P> 

[java]
mFastDF = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG |   Paint.DITHER_FLAG,   0);  mFastDF = new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG |
 Paint.DITHER_FLAG,
 0);

[java]
  
最後看看onDraw
[java]
canvas.setDrawFilter(mDF);     mPaint.setShader(mShader1);  canvas.drawPaint(mPaint);     canvas.translate(mTouchCurrX - mTouchStartX,   mTouchCurrY - mTouchStartY);     mPaint.setShader(mShader2);  canvas.drawPaint(mPaint);  canvas.setDrawFilter(mDF);
 
mPaint.setShader(mShader1);
canvas.drawPaint(mPaint);
 
canvas.translate(mTouchCurrX - mTouchStartX,
 mTouchCurrY - mTouchStartY);
 
mPaint.setShader(mShader2);
canvas.drawPaint(mPaint);
使用Patterns填充是設置Paint的shader屬性,
canvas.translate(mTouchCurrX – mTouchStartX,mTouchCurrY – mTouchStartY); 在繪制Shader2( 綠圓)時平移觸摸屏移動的距離,效果就是綠圓跟著手指動。

 

 作者:mapdigit
 

 
 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved