当前位置: 首页 > 编程笔记 >

Android自定义控件绘制基本图形基础入门

乐正瑞
2023-03-14
本文向大家介绍Android自定义控件绘制基本图形基础入门,包括了Android自定义控件绘制基本图形基础入门的使用技巧和注意事项,需要的朋友参考一下

本文讲述绘制Android自定义各种图形效果,为自定义控件的入门篇

相关视频链接:

Android自定义控件系列
http://edu.csdn.net/course/detail/3719/65396
Android视频全系列
http://edu.csdn.net/course/detail/2741/43163

绘制点–这个控件只需要在布局中引用或者代码中new 即可,下面几个绘制只展示onDraw方法

package com.example.viewdemo1.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class PointView extends View {

 public PointView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
 }

 public PointView(Context context, AttributeSet attrs) {
  super(context, attrs);
 }

 public PointView(Context context) {
  super(context);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  // 对于画笔
  Paint paint = new Paint();
  // 设置抗锯齿
  paint.setAntiAlias(true);
  // 设置画笔颜色
  paint.setColor(Color.RED);
  // 三种样式
  paint.setStyle(Style.FILL_AND_STROKE);
  paint.setStrokeWidth(5);
  // 阴影
  paint.setShadowLayer(10, 0, 0, Color.CYAN);
  // 点的坐标 x0,y0,x1,y1......
  float[] pts = { 50, 50, 100, 100, 200, 200, 300, 300, 0, 100, 100, 0 };
  canvas.drawPoints(pts, paint);
  // 绘制点的时候,隔着几个点绘制几个,最多不到多少点
  canvas.drawPoints(pts, 1, 6, paint);
 }

}

绘制线

@Override
 protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  // 对于画笔
  Paint paint = new Paint();
  // 设置抗锯齿
  paint.setAntiAlias(true);
  // 设置画笔颜色
  paint.setColor(Color.RED);
  // 三种样式
  paint.setStyle(Style.FILL);
  paint.setStrokeWidth(0.5f);
  // 阴影
  // paint.setShadowLayer(10, 0, 0, Color.CYAN);
  // x0,y0,x1,y1
  float[] pts = { 100, 100, 200, 200, 200, 200, 300, 200, 300, 200, 300,
    400 };
  // 以上是6个点的x,y坐标,两两连成线段
  canvas.drawLines(pts, paint);
  // 画一条线
  canvas.drawLine(0, 0, 100, 100, paint);

  }

绘制圆

//指定圆心坐标,半径就OK
canvas.drawCircle(100, 100, 100, paint);

绘制文字

//设置文字大小
paint.setTextSize(40);
//指定坐标,最好指定文字大小
canvas.drawText("哈", 100, 500, paint);
//将文字设置到指定路径上
Path path = new Path();
paint.setTextSize(50);
path.addCircle(200, 200, 150, Direction.CCW);
canvas.drawTextOnPath("我爱你我的祖国,我爱你我亲爱的姑娘", path, 0, 0, paint);

绘制矩形

// 阴影
paint.setShadowLayer(10, 0, 0, Color.CYAN);
// x y 坐标 及半径值
// canvas.drawCircle(100, 100, 50, paint);
canvas.drawRect(50, 50, 300, 300, paint);

绘制圆弧 

 //指定放置圆弧的矩形
 RectF oval=new RectF(10,10,210,210);
 //绘制圆弧-0是指开始度数,270是指结束度数 false是指不连接圆心,paint是画笔
 canvas.drawArc(oval, 0, 270, false, paint);

绘制椭圆

//指定矩形,指定画笔
canvas.drawOval(oval, paint);

以上就是基本图形的绘制了…very easy。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 绘制一个正方形是非常简单的。只需要使用一行代码,即调用fillRect方法,语法如下: context.fillRect(x, y, width, height); context.fillRect(40, 40, 100, 100); 创建一个矩形需要输入4个参数.前两个参数是正方形原点(左上角)的(x,y)坐标值,其余两个参数是矩形的宽度和高度。矩形宽度是(x,y)位置向右绘制的距离,而矩形高

  • 关于矢量图形 矢量图形(有时称作矢量形状或矢量对象)是由称作矢量的数学对象定义的直线和曲线构成的。矢量根据图像的几何特征对图像进行描述。 您可以任意移动或修改矢量图形,而不会丢失细节或影响清晰度,因为矢量图形是与分辨率无关的,即当调整矢量图形的大小、将矢量图形打印到 PostScript 打印机、在 PDF 文件中保存矢量图形或将矢量图形导入到基于矢量的图形应用程序中时,矢量图形都将保持清晰的边缘

  • 在本章中,我们将研究basic controls 。 Foundation提供基本控件,如buttons , sliders和switches 。 下表列出了Foundation中使用的基本控件。 Sr.No. 基本控制和说明 1 Button Foundation支持许多按钮样式,可根据您的需要进行定制。 2 按钮组 它们是相应动作元素的容器。 当一组操作显示在栏中时,它可以正常工作。 3 关闭

  • 图形设备接口(GDI:Graphics Device Interface)是Windows的子系统,它负责在视讯显示器和打印机上显示图形。正如您所认为的那样,GDI是Windows非常重要的部分。不只您为Windows编写的应用系统在显示视觉信息时使用GDI,就连Windows本身也使用GDI来显示使用者接口对象,诸如菜单、滚动条、图标和鼠标光标。 不幸的是,如果要对GDI进行全面的讲述,将需要一

  • RT-Thread 基本数据类型 宏定义 #define  RT_VERSION   3L   主版本号   #define  RT_SUBVERSION   1L   次版本号   #define  RT_REVISION   1L   修订版本号   #define  RT_TRUE   1   布尔值 真   #define  RT_FALSE   0   布尔值 假   #define 

  • 本文向大家介绍Three.js的使用及绘制基础3D图形详解,包括了Three.js的使用及绘制基础3D图形详解的使用技巧和注意事项,需要的朋友参考一下 一、 前言 Three.js 是一款 webGL(3D绘图标准,在此不赘述)引擎,可以运行于所有支持 webGL 的浏览器。Three.js 封装了 webGL 底层的 API ,为我们提供了高级的开发接口,可以使用简单的代码去实现 3D 渲染。(