欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-15 20:37:59
本文讲述绘制android自定义各种图形效果,为自定义控件的入门篇 相关视频链接: android自定义控件系列 android视频全系列...

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

相关视频链接:

android自定义控件系列

android视频全系列

绘制点–这个控件只需要在布局中引用或者代码中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。

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