歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android開發之Canvas繪圖技術

今天我們來看一下Android中有關繪圖的技巧,這裡我們引用李陽老師非常經典的一個繪圖案例,只做了一些小小改動

先上主要的繪圖類:

/**
 *
 */
package com.qq.canvas;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

/**
 * 這個類主要負責繪圖我們常用繼承View來完成它,當然也可以用SurfaceView,其主要運用動的繪圖
 */
public class MyView extends View
{
 Bitmap bitmap;
 public MyView(Context context, AttributeSet set)
 {
  super(context, set);
  bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background);
 }
 @Override
 // 重寫該方法,進行繪圖
 protected void onDraw(Canvas canvas)
 {
  super.onDraw(canvas);
  // 把整張畫布繪制成白色
  canvas.drawColor(Color.WHITE);
 
  Paint paint = new Paint();
  // 去鋸齒
  paint.setAntiAlias(true);
  paint.setColor(Color.BLUE);
  //畫筆風格
  paint.setStyle(Paint.Style.STROKE);
  //畫筆大小
  paint.setStrokeWidth(3);
  //畫背景圖片,由於畫圖片的時候會導致繪畫的效果不明顯,所以先注釋掉
  //paint.setAlpha(10);//設置圖片透明度
  //canvas.drawBitmap(bitmap, 0, 0, null);
  //paint.setAlpha(255);//還原透明度
  // 繪制圓形
  canvas.drawCircle(40, 40, 30, paint);
  // 繪制正方形
  canvas.drawRect(10, 80, 70, 140, paint);
  // 繪制矩形
  canvas.drawRect(10, 150, 70, 190, paint);
  RectF re1 = new RectF(10, 200, 70, 230);
  // 繪制圓角矩形
  canvas.drawRoundRect(re1, 15, 15, paint);
  RectF re11 = new RectF(10, 240, 70, 270);
  // 繪制橢圓
  canvas.drawOval(re11, paint); 
  // 定義一個Path對象,封閉成一個三角形。
  Path path1 = new Path();
  path1.moveTo(10, 340);
  path1.lineTo(70, 340);
  path1.lineTo(40, 290);
  path1.close();
  // 根據Path進行繪制,繪制三角形
  canvas.drawPath(path1, paint);
  // 定義一個Path對象,封閉成一個五角形。每一個To都是一個點,五個點連起來剛好是一個五角星
  Path path2 = new Path();
  path2.moveTo(26, 360);
  path2.lineTo(54, 360);
  path2.lineTo(70, 392);
  path2.lineTo(40, 420);
  path2.lineTo(10, 392);
  path2.close();
  // 根據Path進行繪制,繪制五角形
  canvas.drawPath(path2, paint);
  // ----------設置填充風格後繪制----------
  paint.setStyle(Paint.Style.FILL);
  paint.setColor(Color.RED);
  canvas.drawCircle(120, 40, 30, paint);
  //繪制正方形
  canvas.drawRect(90, 80, 150, 140, paint);
  //繪制矩形
  canvas.drawRect(90, 150, 150, 190, paint);
  RectF re2 = new RectF(90, 200, 150, 230);
  //繪制圓角矩形
  canvas.drawRoundRect(re2, 15, 15, paint);
  RectF re21 = new RectF(90, 240, 150, 270);
  // 繪制橢圓
  canvas.drawOval(re21, paint); 
  Path path3 = new Path();
  path3.moveTo(90, 340);
  path3.lineTo(150, 340);
  path3.lineTo(120, 290);
  path3.close();
  //繪制三角形
  canvas.drawPath(path3, paint);
  Path path4 = new Path();
  path4.moveTo(106, 360);
  path4.lineTo(134, 360);
  path4.lineTo(150, 392);
  path4.lineTo(120, 420);
  path4.lineTo(90, 392);
  path4.close();
  //繪制五角形
  canvas.drawPath(path4, 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.setShadowLayer(45 , 10 , 10 , Color.GRAY);
  // 繪制圓形
  canvas.drawCircle(200, 40, 30, paint);
  // 繪制正方形
  canvas.drawRect(170, 80, 230, 140, paint);
  // 繪制矩形
  canvas.drawRect(170, 150, 230, 190, paint);
  RectF re3 = new RectF(170, 200, 230, 230);
  // 繪制圓角矩形
  canvas.drawRoundRect(re3, 15, 15, paint);
  RectF re31 = new RectF(170, 240, 230, 270);
  // 繪制橢圓
  canvas.drawOval(re31, paint); 
  Path path5 = new Path();
  path5.moveTo(170, 340);
  path5.lineTo(230, 340);
  path5.lineTo(200, 290);
  path5.close();
  // 根據Path進行繪制,繪制三角形
  canvas.drawPath(path5, paint);
  Path path6 = new Path();
  path6.moveTo(186, 360);
  path6.lineTo(214, 360);
  path6.lineTo(230, 392);
  path6.lineTo(200, 420);
  path6.lineTo(170, 392);
  path6.close();
  // 根據Path進行繪制,繪制五角形
  canvas.drawPath(path6, paint);
  // ----------設置字符大小後繪制----------
  paint.setTextSize(24);
  paint.setShader(null);
  // 繪制7個字符串
  canvas.drawText(getResources().getString(R.string.circle), 240, 50,
   paint);
  canvas.drawText(getResources().getString(R.string.square), 240, 120,
   paint);
  canvas.drawText(getResources().getString(R.string.rect), 240, 175,
   paint);
  canvas.drawText(getResources().getString(R.string.round_rect), 230,
   220, paint);
  canvas.drawText(getResources().getString(R.string.oval), 240,
   260, paint); 
  canvas.drawText(getResources().getString(R.string.triangle), 240, 325,
   paint);
  canvas.drawText(getResources().getString(R.string.pentagon), 240, 390,
   paint);
 }
}

Copyright © Linux教程網 All Rights Reserved