Android使用Canvas畫圖

1.參考:http://blog.csdn.net/rhljiayou/article/details/7212620/canvas

2.經常使用方法:

  1.Canvas類ide

 drawArc 繪製弧spa

 drawBitmap 繪製位圖.net

 drawCircle 繪製圓形code

 drawLine 繪製線blog

 drawOval 繪製橢圓圖片

 drawPath 繪製路徑字符串

 drawPoint 繪製一個點get

 drawPoints 繪製多個點it

 drawRect 繪製矩形

 drawRoundRect 繪製圓角矩形

 drawText 繪製字符串

 drawTextOnPath 沿着路徑繪製字符串

  2.Paint類:

setARGB/setColor 設置顏色

setAlpha 設置透明度

setAntiAlias 設置是否抗鋸齒

setShader 設置畫筆的填充效果

setShadowLayer 設置陰影

setStyle 設置畫筆風格

setStrokeWidth 設置空心邊框的寬度

setTextSize 設置繪製文本時文字的大小

setStrokeCap(Paint.Cap.ROUND) 設置爲圓角

3.代碼以下:

public class DrawView extends View {
  
    public DrawView(Context context) {
        super(context);  
    }

    public DrawView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);  
        /* 
         * 方法 說明 drawRect 繪製矩形 drawCircle 繪製圓形 drawOval 繪製橢圓 drawPath 繪製任意多邊形 
         * drawLine 繪製直線 drawPoin 繪製點 
         */


        //設置畫筆
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);



        //畫文本
        canvas.drawText("畫圓",20,30,paint);
        //畫圓
        canvas.drawCircle(100,30,30,paint);

        //畫線
        paint.setColor(Color.BLACK);
        canvas.drawText("畫線及圓弧",10,80,paint);
        canvas.drawLine(10,90,310,90,paint);//直線
        canvas.drawLine(330,90,430,150,paint);//斜線


        //畫圓弧及扇形
        RectF oval1=new RectF(10,160,110,260);
        paint.setColor(Color.GREEN);

        canvas.drawArc(oval1,180,180,false,paint);
        //第一個參數:範圍  第二個參數:開始角度  第三個參數:圓弧度數
        //第四個參數:false---圓弧   true---扇形
        //第五個參數:畫筆

        oval1.set( 120,160,220,260);
        canvas.drawArc(oval1,20,60,true,paint);

        oval1.set(230,160,330,260);
        canvas.drawArc(oval1,200,240,true,paint);


        //畫矩形
        paint.setColor(Color.BLUE);
        canvas.drawRect(10,280,210,380,paint);

        //繪製圓角矩形
        RectF re1=new RectF(250,280,450,380);
        canvas.drawRoundRect(re1,15,15,paint);


        //畫橢圓
        RectF re2=new RectF(10,400,210,500);
        canvas.drawOval(re2, paint);

        //畫三角形
        paint.setColor(Color.DKGRAY);
        Path path=new Path();
        path.moveTo(70,520);
        path.lineTo(10,580);
        path.lineTo(130,580);
        path.close();
        canvas.drawPath(path,paint);



        //畫點
        paint.setStyle(Paint.Style.FILL);
        canvas.drawPoint(10,600,paint);//一個點
        canvas.drawPoints(new float[]{20,600,30,600,40,600},paint);//多個點


        //畫貝塞爾曲線
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.RED);
        Path path1=new Path();
        path1.moveTo(100,620);
        path1.quadTo(150,550,170,700);
        canvas.drawPath(path1,paint);



        //畫圖片
        Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.pic6);
//        canvas.drawBitmap(bitmap,100,610,paint);//不限定圖片大小  只指定左上角座標
        RectF rectF=new RectF(100,710,400,1010);
        canvas.drawBitmap(bitmap,null,rectF,paint);//限定圖片顯示範圍



        //-----------------設置漸變後繪製------------------

        //Paint設置漸變器
        Shader mShader=new LinearGradient(0,0,40,60,
                new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},
                null,Shader.TileMode.REPEAT);
        paint.setShader(mShader);
        paint.setStyle(Paint.Style.FILL);
        //設置陰影
        paint.setShadowLayer(45,10,10,Color.BLACK);

        canvas.drawRect(440,710,640,910,paint);

    }  
}

4.效果圖:

相關文章
相關標籤/搜索