Android ApiDemos示例解析(71):Graphics->Patterns
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="/2012/0717/20120717084124727.png"><IMG class="aligncenter size-full wp-image-1595" title=20110603001 height=490 alt="" src="/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
补充:移动开发 , Android ,