源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2020-10-27 14:25 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android自定义控件绘制基本图形基础入门
本文讲述绘制Android自定义各种图形效果,为自定义控件的入门篇 [b]相关视频链接: [/b] Android自定义控件系列 [url=http://edu.csdn.net/course/detail/3719/65396]http://edu.csdn.net/course/detail/3719/65396[/url] Android视频全系列 [url=http://edu.csdn.net/course/detail/2741/43163]http://edu.csdn.net/course/detail/2741/43163[/url] [b]绘制点[/b]–这个控件只需要在布局中引用或者代码中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);
 }

}

[b]绘制线 [/b]
@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);

  }

[b]绘制圆[/b]
//指定圆心坐标,半径就OK
canvas.drawCircle(100, 100, 100, paint);
[b]绘制文字 [/b]
//设置文字大小
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);
[b]绘制矩形 [/b]
// 阴影
paint.setShadowLayer(10, 0, 0, Color.CYAN);
// x y 坐标 及半径值
// canvas.drawCircle(100, 100, 50, paint);
canvas.drawRect(50, 50, 300, 300, paint);
[b]绘制圆弧[/b] 
 //指定放置圆弧的矩形
 RectF oval=new RectF(10,10,210,210);
 //绘制圆弧-0是指开始度数,270是指结束度数 false是指不连接圆心,paint是画笔
 canvas.drawArc(oval, 0, 270, false, paint);
[b]绘制椭圆 [/b]
//指定矩形,指定画笔
canvas.drawOval(oval, paint);
以上就是基本图形的绘制了…very easy。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部