編輯:關於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
?? 上一篇Android研究之屬性動畫(Property Animation)完全解析詳解上已經基本展示了屬性動畫的核心用法: ObjectAnimator實現動畫,V
當我們在移動端閱讀文章時,總會有朋友發了一條微信給你。這時候,我是繼續看文章還是回微信呢?MIUI8的微信多窗口功能讓你徹底擺脫這個煩惱,多窗口模式能夠讓你
手機購買的時候自身帶了很多軟件,能刪除嗎?下面安下網小編教大家幾招用來刪除手機預裝軟件。 1、首先百度搜索360手機助手,然後下載安裝
一.listView的二級目錄且選中實現:記錄下來,以便以後可能會用到,直接上貼源碼:先上效果圖: 主界面:public class MainActivity exten