android canvas 畫圖筆記


android canvas 畫圖筆記

1.PathEffect類

畫虛線

 Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.STROKE);
        p.setColor(Color.WHITE);
        p.setStrokeWidth(1);
        PathEffect effect = new DashPathEffect(new float[]{getDip2px(4), getDip2px(4), getDip2px(4), getDip2px(4)}, 1);
        p.setPathEffect(effect);
        p.setAlpha(125);//透明度50%
        p.setColor(getResources().getColor(R.color.line_chart_dash_line));
        canvas.drawLine(0, ScreenHeight / 2, ScreenWidth, ScreenHeight / 2, p);
        canvas.drawLine(ScreenWidth / 2, 0, ScreenWidth / 2, getBottom() - getDip2px(25), p);

2.android onMeasure

3.android BlurMaskFilter

***Android MaskFilter的基本使用:***

MaskFilter類能夠為Paint分配邊緣效果。
        對MaskFilter的擴展能夠對一個Paint邊緣的alpha通道應用轉換。

Android包括了以下幾種MaskFilter: BlurMaskFilter 指定了一個模糊的樣式和半徑來處理Paint的邊緣。 EmbossMaskFilter 指定了光源的方向和環境光強度來加入浮雕效果。

要應用一個MaskFilter,能夠使用setMaskFilter方法,並傳遞給它一個MaskFilter對象。

以下的樣例是對一個已經存在的Paint應用一個EmbossMaskFilter:

Android MaskFilter的基本使用:

MaskFilter類能夠為Paint分配邊緣效果。

對MaskFilter的擴展能夠對一個Paint邊緣的alpha通道應用轉換。Android包括了以下幾種MaskFilter: BlurMaskFilter 指定了一個模糊的樣式和半徑來處理Paint的邊緣。 EmbossMaskFilter 指定了光源的方向和環境光強度來加入浮雕效果。 要應用一個MaskFilter。能夠使用setMaskFilter方法。並傳遞給它一個MaskFilter對象。以下的樣例是對一個已經存在的Paint應用一個EmbossMaskFilter:

關於設置硬件加速不能用的方法

LAYER_TYPE_NONE

LAYER_TYPE_SOFTWARE

LAYER_TYPE_HARDWARE

View級別

您能夠在執行時用以下的代碼關閉單個view的硬件加速:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
注:您不能在view級別開啟硬件加速
為什么須要這么多級別的控制?
非常明顯。硬件加速能夠帶來性能提升,android為什么要弄出這么多級別的控制,
而不是默認就是全部硬件加速呢?原因是並不是全部的2D畫圖操作支持硬件加速,
假設您的程序中使用了自己定義視圖或者畫圖調用,程序可能會工作不正常。
假設您的程序中僅僅是用了標准的視圖和Drawable,放心大膽的開啟硬件加速吧。
詳細是哪些畫圖操作不支持硬件加速呢?以下是已知不支持硬件加速的畫圖操作:

Canvas
clipPath()
clipRegion()
drawPicture()
drawPosText()
drawTextOnPath()
drawVertices()
Paint
setLinearText()
setMaskFilter()
setRasterizer()

另外另一些畫圖操作,開啟和不開啟硬件加速。效果不一樣:

Canvas
clipRect(): XOR, Difference和ReverseDifference裁剪模式被忽略,3D變換將不會應用在裁剪的矩形上。

drawBitmapMesh():colors數組被忽略 drawLines():反鋸齒不支持 setDrawFilter():能夠設置。但無效果 Paint setDither(): 忽略 setFilterBitmap():過濾永遠開啟 setShadowLayer():僅僅能用在文本上 ComposeShader ComposeShader 僅僅能包括不同類型的shader (比方一個BitmapShader和一個LinearGradient,但不能是兩個BitmapShader實例) ComposeShader不能包括ComposeShader 假設應用程序受到這些影響。您能夠在受影響的部分調用setLayerType(View.LAYER_TYPE_SOFTWARE, null)。這樣在其他地方仍然能夠享受硬件加速帶來的優點


2.Paint(畫筆)類

   要繪制圖形,首先得調整畫筆,依照自己的開發須要設置畫筆的相關屬性。Pain類的經常使用屬性設置方法例如以下:

  setAntiAlias();            //設置畫筆的鋸齒效果

  setColor();                 //設置畫筆的顏色

  setARGB();                 //設置畫筆的A、R、G、B值

  setAlpha();                 //設置畫筆的Alpha值

  setTextSize();             //設置字體的尺寸

  setStyle();                  //設置畫筆的風格(空心或實心)

  setStrokeWidth();        //設置空心邊框的寬度

  getColor();                  //獲取畫筆的顏色

  

 3.Canvas(畫布)類

  畫筆屬性設置好之后。還須要將圖像繪制到畫布上。

Canvas類能夠用來實現各種圖形的繪制工作。如繪制直線、矩形、圓等等。

Canvas繪制經常使用圖形的方法例如以下:   繪制直線:canvas.drawLine(float startX, float startY, float stopX, float stopY, Paint paint);   繪制矩形:canvas.drawRect(float left, float top, float right, float bottom, Paint paint);   繪制圓形:canvas.drawCircle(float cx, float cy, float radius, Paint paint);   繪制字符:canvas.drawText(String text, float x, float y, Paint paint);   繪制圖形:canvas.drawBirmap(Bitmap bitmap, float left, float top, Paint paint);


Shader 漸變色設置

 //設置漸變器后繪制
        //為Paint設置漸變器
        Shader mShasder = new LinearGradient(0, 0, 40, 60, new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW}, null, Shader.TileMode.REPEAT);
        mPaint.setShader(mShasder);
        //設置陰影
        mPaint.setShadowLayer(45, 10, 10, Color.GRAY);

Paint.Style差別

Paint.Style.FILL    :填充內部
Paint.Style.FILL_AND_STROKE  :填充內部和描邊
Paint.Style.STROKE  :僅描邊

Region 推斷是否在路徑范圍內

“onTouchEvent 在自己定義View 中”

@Override  
public boolean onTouchEvent(MotionEvent event) {  
    switch (event.getAction()) {  
    case MotionEvent.ACTION_DOWN:  
        // 按下  
        cx = (int) event.getX();  
        cy = (int) event.getY();  
        // 通知重繪  
        postInvalidate();   //該方法會調用onDraw方法。又一次畫圖  
        break;  
    case MotionEvent.ACTION_MOVE:  
        // 移動  
        cx = (int) event.getX();  
        cy = (int) event.getY();  
        // 通知重繪  
        postInvalidate();  
        break;  
    case MotionEvent.ACTION_UP:  
        // 抬起  
        cx = (int) event.getX();  
        cy = (int) event.getY();  
        // 通知重繪  
        postInvalidate();  
        break;  
    }  

    /* 
     * 備注1:此處一定要將return super.onTouchEvent(event)改動為return true,原因是: 
     * 1)父類的onTouchEvent(event)方法可能沒有做不論什么處理,可是返回了false。 
     * 2)一旦返回false,在該方法中再也不會收到MotionEvent.ACTION_MOVE及MotionEvent.ACTION_UP事件。

*/ //return super.onTouchEvent(event); return true; }


RectF

  • Android Rect和RectF的差別*

    1、精度不一樣,Rect是使用int類型作為數值,RectF是使用float類型作為數值
    2、兩個類型提供的方法也不是全然一致
    
    Rect:
    equals(Object obj)   (for some reason it as it's own implementation of equals)
    exactCenterX()
    exactCenterY()
    flattenToString()
    toShortString()
    unflattenFromString(String str)
    
    RectF:
    round(Rect dst)
    roundOut(Rect dst)
    set(Rect src)
    

Shader

//設置漸變器后繪制
        //為Paint設置漸變器
        Shader mShasder = new LinearGradient(0, 0, 40, 60, new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW}, null, Shader.TileMode.REPEAT);
        mPaint.setShader(mShasder);
        //設置陰影
        mPaint.setShadowLayer(45, 10, 10, Color.GRAY);
Paint p=new Paint();
LinearGradient lg=new LinearGradien(0,0,100,100,Color.RED,Color.BLUE,Shader.TileMode.MIRROR); 
參數一為漸變起初點坐標x位置,
參數二為y軸位置,
參數三和四分辨相應漸變終點。
最后參數為平鋪方式。這里設置為鏡像
Gradient是基於Shader類。所以我們通過Paint的setShader方法來設置這個漸變。代碼例如以下: mPaint.setShader(lg);

canvas.drawCicle(0,0,200,mPaint); //參數3為畫圓的半徑。類型為float型。


它除了定義開始顏色和結束顏色以外還能夠定義,多種顏色組成的分段漸變效果

LinearGradient shader = new LinearGradient(0, 0, endX, endY, new int[]{startColor, midleColor, endColor},new float[]{0 , 0.5f, 1.0f}, TileMode.MIRROR);

當中參數new int[]{startColor, midleColor, endColor}是參與漸變效果的顏色集合。

當中參數new float[]{0 , 0.5f, 1.0f}是定義每一個顏色處於的漸變相對位置,

這個參數能夠為null。假設為null表示全部的顏色按順序均勻的分布

LinearGradient有兩個構造函數;

public LinearGradient(float x0, float y0, float x1, float y1, int[] colors, float[] positions,Shader.TileMode tile)

參數:
float x0: 漸變起始點x坐標

float y0:漸變起始點y坐標

float x1:漸變結束點x坐標

float y1:漸變結束點y坐標

int[] colors:顏色 的int 數組

float[] positions: 相對位置的顏色數組,可為null,  若為null,可為null,顏色沿漸變線均勻分布

Shader.TileMode tile: 渲染器平鋪模式

public LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1,Shader.TileMode tile)
float x0: 漸變起始點x坐標

float y0:漸變起始點y坐標

float x1:漸變結束點x坐標

float y1:漸變結束點y坐標

int color0: 起始漸變色

int color1: 結束漸變色

Shader.TileMode tile: 渲染器平鋪模式


Android 透明度漸變

android 代碼截屏

 /** * Returns the bitmap that represents the chart. * * @return */
    public Bitmap getChartBitmap() {
        // Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
        // Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        // Get the view's background
        Drawable bgDrawable = getBackground();
        if (bgDrawable != null)
            // has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else
            // does not have background drawable, then draw white background on
            // the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        draw(canvas);
        // return the bitmap
        return returnedBitmap;
    }
 /** * Returns the bitmap that represents the chart. * * @return */
    public Bitmap getChartBitmap() {
        // Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
        // Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        // Get the view's background
        Drawable bgDrawable = getBackground();
        if (bgDrawable != null)
            // has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else
            // does not have background drawable, then draw white background on
            // the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        draw(canvas);
        // return the bitmap
        return returnedBitmap;
    }

對canvas的translate()方法的理解

canvas.save();//鎖畫布(為了保存之前的畫布狀態)
        canvas.translate(10, 10);//把當前畫布的原點移到(10,10),后面的操作都以(10,10)作為參照點。默認原點為(0,0)
        drawScene(canvas);
        canvas.restore();//把當前畫布返回(調整)到上一個save()狀態之前

        canvas.save();//鎖畫布(為了保存之前的畫布狀態)
        canvas.translate(160, 10);//把當前畫布的原點移到(160,10),后面的操作都以(160,10)作為參照點。
        canvas.clipRect(10, 10, 90, 90);//這里的真實坐標為左上(170,170)、右下(250,250)
        canvas.clipRect(30, 30, 70, 70, Region.Op.DIFFERENCE);
        drawScene(canvas);
        canvas.restore();

圖片鏡像

public Bitmap convertBmp(Bitmap bmp) {  
        int w = bmp.getWidth();  
        int h = bmp.getHeight();  

        Matrix matrix = new Matrix();  
        matrix.postScale(-1, 1); // 鏡像水平翻轉 
        Bitmap convertBmp = Bitmap.createBitmap(bmp, 0, 0, w, h, matrix, true);  

        return convertBmp;  
    }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM