Android Paint類介紹以及浮雕和陰影製作效果的設定

來源:互聯網
上載者:User

標籤:android   paint   映像   

Paint類介紹

Paint即畫筆,在繪製文本和圖形用它來設定圖形顏色, 樣式等繪製資訊。

1.圖形繪製

setARGB(int a,int r,int g,int b);

設定繪製的顏色,a代表透明度,r,g,b代表顏色值。

setAlpha(int a);

設定繪製圖形的透明度。

setColor(int color);

設定繪製的顏色,使用顏色值來表示,該顏色值包括透明度和RGB顏色。

setAntiAlias(boolean aa);

設定是否使用消除鋸齒功能,會消耗較大資源,繪製圖形速度會變慢。

setDither(boolean dither);

設定是否使用映像抖動處理,會使繪製出來的圖片顏色更加平滑和飽滿,映像更加清晰

setFilterBitmap(boolean filter);

如果該項設定為true,則映像在動畫進行中會濾掉對Bitmap映像的最佳化操作,加快顯示

速度,本設定項依賴於dither和xfermode的設定

setMaskFilter(MaskFilter maskfilter);

設定MaskFilter,可以用不同的MaskFilter實現濾鏡的效果,如濾化,立體等

setColorFilter(ColorFilter colorfilter);

設定顏色過濾器,可以在繪製顏色時實現不用顏色的變換效果

setPathEffect(PathEffect effect);

設定繪製路徑的效果,如點畫線等

setShader(Shader shader);

設定映像效果,使用Shader可以繪製出各種漸層效果

setShadowLayer(float radius ,float dx,float dy,int color);

在圖形下面設定陰影層,產生陰影製作效果,radius為陰影的角度,dx和dy為陰影在x軸和y軸上的距離,color為陰影的顏色

setStyle(Paint.Style style);

設定畫筆的樣式,為FILL,FILL_OR_STROKE,或STROKE

setStrokeCap(Paint.Cap cap);

當畫筆樣式為STROKE或FILL_OR_STROKE時,設定筆刷的圖形樣式,如圓形樣式

Cap.ROUND,或方形樣式Cap.SQUARE

setSrokeJoin(Paint.Join join);

設定繪製時各圖形的結合方式,如凹凸貼圖等

setStrokeWidth(float width);

當畫筆樣式為STROKE或FILL_OR_STROKE時,設定筆刷的粗細度

setXfermode(Xfermode xfermode);

設定圖形重疊時的處理方式,如合并,取交集或並集,經常用來製作橡皮的擦除效果

2.文本繪製

setFakeBoldText(boolean fakeBoldText);

類比實現粗體文字,設定在小字型上效果會非常差

setSubpixelText(boolean subpixelText);

設定該項為true,將有助於文本在LCD螢幕上的顯示效果

setTextAlign(Paint.Align align);

設定繪製文字的對齊方向

setTextScaleX(float scaleX);

設定繪製文字x軸的縮放比例,可以實現文字的展開的效果

setTextSize(float textSize);

設定繪製文字的字型大小大小

setTextSkewX(float skewX);

設定斜體文字,skewX為傾斜弧度

setTypeface(Typeface typeface);

設定Typeface對象,即字型風格,包括粗體,斜體以及襯線體,非襯線體等

setUnderlineText(boolean underlineText);

設定帶有底線的文字效果

setStrikeThruText(boolean strikeThruText);

設定帶有刪除線的效果

浮雕和陰影製作效果的實現

需要用到Paint.setMaskFilter方法。//設定MaskFilter,可以用不同的MaskFilter實現濾鏡的效果,如濾化,立體等
setMaskFilter(MaskFilter maskfilter);
MaskFilter類可以為Paint分配邊緣效果。
對MaskFilter的擴充可以對一個Paint邊緣的alpha通道應用轉換。Android包含了下面幾種MaskFilter:
BlurMaskFilter 指定了一個模糊的樣式和半徑來處理Paint的邊緣。
EmbossMaskFilter 指定了光源的方向和環境光線強度來添加浮雕效果。
要應用一個MaskFilter,可以使用setMaskFilter方法,並傳遞給它一個MaskFilter對象。下面的例子是對一個已經存在的Paint應用一個EmbossMaskFilter:

public class MainActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        ImageView iv = new ImageView(this);        setContentView(iv);        Bitmap originImg = BitmapFactory.decodeResource(getResources(), R.drawable.painter);        Bitmap grayImg = Bitmap.createBitmap(originImg.getWidth(), originImg.getHeight(), Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(grayImg);        Paint paint = new Paint();        //ColorMatrix colorMatrix = new ColorMatrix();        //colorMatrix.setSaturation(0);        //ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);        //paint.setColorFilter(colorMatrixFilter);        float[] direction = new float[]{ 1, 1, 1 };      //設定環境光線亮度        float light = 0.4f;      // 選擇要應用的反射等級        float specular = 6;      // 向mask應用一定層級的模糊        float blur = 3.5f;        EmbossMaskFilter emboss=new EmbossMaskFilter(direction,light,specular,blur);        paint.setMaskFilter(emboss);        canvas.drawBitmap(originImg, 0, 0, paint);        //grayImg = addWaterMark(grayImg, "hello world", this);        iv.setImageBitmap(grayImg);    }



再看下面使用BlurMaskFilter:
//前面一個控制陰影的寬度,後面一個參數控制陰影製作效果
maskFilter = new BlurMaskFilter(10, BlurMaskFilter.Blur.SOLID);
用Paint.setMaskFilter方法進行替換就好了


MaskFilter是對一個Paint的alpha通道的轉換,效果不太明顯而ColorFilter則是對每一個RGB通道應用轉換,上一篇部落格已經介紹了。所有由ColorFilter所派生的類在執行它們的轉換時,都會忽略alpha通道。

Android Paint類介紹以及浮雕和陰影製作效果的設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.