绘图
android的绘图应该继承View组件,并重写onDraw(Canvas canvas)方法即可。
重写onDraw(Canvas canvas)方法时涉及一个绘图API:Canvas,Canvas代表了“依附”于指定View的画布,其提供了如下图所示的方法——用于绘制各种图像:
注意:在Canvas提供的绘制方法中还用到了一个API:Path,Path代表任意多条直线连接而成的任意图形,当Canvas根据Path绘制时,可以绘制出任意的形状。
实例如下:
布局文件==》 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <com.example.mycanvas.MyView android:id="@+id/myView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> 代码实现==》 package com.example.mycanvas; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Shader; import android.util.AttributeSet; import android.view.View; public class MyView extends View { public MyView(Context context, AttributeSet set) { super(context, set); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 把整张布绘制成白色 canvas.drawColor(Color.WHITE); Paint p = new Paint(); // 去除锯齿 p.setAntiAlias(true); p.setColor(Color.BLUE); p.setStyle(Paint.Style.STROKE); p.setStrokeWidth(3); // 绘制圆形 canvas.drawCircle(40, 40, 30, p); // 绘制正方形 canvas.drawRect(10, 80, 70, 140, p); // 绘制矩形 canvas.drawRect(10, 150, 70, 190, p); RectF rect = new RectF(10, 200, 70, 230); // 绘制圆角矩形 canvas.drawRoundRect(rect, 15, 15, p); RectF rect1 = new RectF(10, 240, 70, 270); // 绘制椭圆 canvas.drawOval(rect1, p); paintTriangle(canvas, p); paintFivePointStar(canvas, p); paintGraph(canvas, p); paintGraph2(canvas, p); paintGraph3(canvas, p); } /* 设置字符大小后绘制 */ private void paintGraph3(Canvas canvas, Paint paint) { paint.setTextSize(26); paint.setShader(null); // 绘制字符串 canvas.drawText("圆形", 260, 50, paint); // canvas.drawText("方形", 260, 120, paint); // canvas.drawText("长方形", 240, 170, paint); // canvas.drawText("圆角矩形", 230, 220, paint); // canvas.drawText("圆形", 240, 260, paint); // canvas.drawText("三角形", 240, 325, paint); // canvas.drawText("五角形", 240, 390, paint); } private void paintGraph2(Canvas canvas, Paint paint) { // 设置渐变器后绘制 Shader shader = new LinearGradient(0, 0, 40, 60, new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }, null, Shader.TileMode.REPEAT); paint.setShader(shader); // 设置阴影 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 r = new RectF(170, 200, 230, 230); canvas.drawRect(r, paint); // 绘制椭圆 RectF rel2 = new RectF(170, 240, 230, 270); canvas.drawOval(rel2, paint); // 绘制三角形 Path path = new Path(); path.moveTo(170, 340); path.lineTo(230, 340); path.lineTo(200, 290); path.close(); // 根据Path进行绘制,绘制三角形 canvas.drawPath(path, paint); // 定义一个Path对象,封闭成一个三角形 Path p1 = new Path(); p1.moveTo(186, 360); p1.lineTo(214, 360); p1.lineTo(230, 392); p1.lineTo(200, 420); p1.lineTo(170, 392); p1.close(); // 根据Path进行绘制,绘制三角形 canvas.drawPath(p1, paint); } private void paintGraph(Canvas canvas, Paint p) { // 设置填充风格后绘制 p.setStyle(Paint.Style.FILL); p.setColor(Color.RED); canvas.drawCircle(120, 40, 30, p); // 绘制正方形 canvas.drawRect(90, 80, 150, 140, p); // 绘制矩形 canvas.drawRect(90, 150, 150, 190, p); RectF re2 = new RectF(90, 200, 150, 230); // 绘制圆角矩形 canvas.drawRoundRect(re2, 15, 15, p); RectF rel2 = new RectF(90, 240, 150, 270); // 绘制椭圆 canvas.drawOval(rel2, p); // 绘制三角形 Path path = new Path(); path.moveTo(90, 340); path.lineTo(150, 340); path.lineTo(120, 290); path.close(); // 根据Path进行绘制,绘制三角形 canvas.drawPath(path, p); // 定义一个Path对象,封闭成一个三角形 Path p1 = new Path(); p1.moveTo(106, 360); p1.lineTo(134, 360); p1.lineTo(150, 390); p1.lineTo(120, 420); p1.lineTo(90, 392); p1.close(); // 根据Path进行绘制,绘制三角形 canvas.drawPath(p1, p); } private void paintFivePointStar(Canvas canvas, Paint p) { // 定义一个Path对象,封闭成一个五角星 Path p2 = new Path(); p2.moveTo(26, 360); p2.lineTo(54, 360); p2.lineTo(70, 392); p2.lineTo(40, 420); p2.lineTo(10, 392); p2.close(); // 根据Path进行绘制,绘制五角形 canvas.drawPath(p2, p); } private void paintTriangle(Canvas canvas, Paint p) { // 定义一个Path对象,封闭成一个三角形 Path p1 = new Path(); p1.moveTo(10, 340); p1.lineTo(70, 340); p1.lineTo(40, 290); p1.close(); // 根据Path进行绘制,绘制三角形 canvas.drawPath(p1, p); } }
注:将自定义组件拖放到布局文件,即可看到效果,如下图所示: