Android 用Canvas画textview、bitmap、矩形(裁剪)、椭圆、线、点、弧

初始化对象

private Paint mPaint;//画笔
	private int count;//点击次数
	private Rect rect;//矩形
	public CounstomView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		//初始化画笔
		mPaint = new Paint();
		rect = new Rect();
		setOnClickListener(this);
	}

1.画textview。并且设置可点击。//渲染文本,Canvas类除了上面的还可以描绘文字,参数一是String类型的文本,参数二x轴,参数三y轴,参数四是Paint对象。

String text = null;
	@SuppressLint("DrawAllocation") @Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//设置paint
		mPaint.setStyle(Style.FILL);//实心
		//mPaint.setColor(Color.BLUE);//黄色
		//mPaint.setAlpha(50);//透明度15
		canvas.drawColor(Color.YELLOW);
		canvas.drawRect(0, 0, getHeight(), getWidth(), mPaint);//画矩形

		mPaint.setColor(Color.RED);
		mPaint.setTextSize(25.0f);
		 text = String.valueOf(count);
		mPaint.getTextBounds(text, 0, text.length(), rect);//将内容的长和宽。添加到rect矩形中
		float width = rect.width();//获取宽
		float height = rect.height();
		canvas.drawText(text, getWidth() / 2  - width / 2, getHeight() / 2 + height/2, mPaint);//设置文本位置
//		

	}
	@Override
	public void onClick(View arg0) {
		count++;
		invalidate();//重绘
	}

2.画简单的bitmap。参数一就是我们常规的Bitmap对象,参数二是源区域(这里是bitmap),参数三是目标区域(应该在canvas的位置和大小),参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。

protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//偏左和偏上的位置
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.login_backgroud);
		canvas.drawBitmap(bitmap,0,0, mPaint);

	}

3.利用canvas.drawBitmap(bitmap, src, dst, paint)画裁剪过的bitmap//参数一就是我们常规的Bitmap对象,参数二是源区域(这里是bitmap),参数三是目标区域(应该在canvas的位置和大小),参数四是Paint画刷对象,因为用到了缩放和拉伸的可能,当原始Rect不等于目标Rect时性能将会有大幅损失。

protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 偏左和偏上的位置
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.login_backgroud);

		Paint paint = new Paint();
		canvas.save();
		// left----距离屏幕左侧距离。。。
		// top------距离顶部距离。。。
		// right-----矩形的宽度
		// buttom-----矩形的高度
		Rect rect = new Rect(10, 10, 500, 500);
		canvas.clipRect(rect); // 设置裁剪区域
		canvas.drawColor(Color.BLUE);// 裁剪区域的rect变为蓝色
		Rect rec = new Rect(20, 20, 300, 300);
		canvas.drawBitmap(bitmap, rect, rec, paint);

		canvas.restore();
	}

4:利用canvas.drawOval画椭圆。画椭圆,参数一是扫描区域,参数二为paint对象;

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 偏左和偏上的位置
		Paint paint = new Paint();
		canvas.save();
		// left----距离屏幕左侧距离。。。A
		// top------距离顶部距离。。。B
		// right-----椭圆的宽度.....C
		// buttom-----椭圆的高度......D
		RectF dst = new RectF(10,10,300, 400);//2a=100-30,2b=310-260
		paint.setColor(Color.YELLOW);
		canvas.drawColor(Color.RED);
		canvas.drawOval(dst, paint);
		canvas.restore();
	}

5.利用canvas画点。//画点,参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。

@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		canvas.save();
		canvas.drawColor(Color.RED);//有背景先画背景
		canvas.restore();

		canvas.save();
		paint.setColor(Color.BLACK);
		paint.setTextSize(20.0f);
		canvas.drawText("画点:", 10, 90, paint);//文本
		canvas.restore();

		canvas.save();
		paint.setColor(Color.GREEN);
		paint.setStrokeWidth(20.0f);//设置点的大小
		canvas.drawPoint(120, 90, paint);//参数一水平x轴,参数二垂直y轴,第三个参数为Paint对象。
//		canvas.drawPoints(new float[]{60,400,65,400,70,40}, paint);//画多个点
		canvas.restore();

		canvas.save();
		paint.setColor(Color.BLACK);
		paint.setStyle(Style.FILL);
		canvas.drawText("这样可以当圆点用么:", 10, 130, paint);

		paint.setAntiAlias(true);//去除抗锯齿
		paint.setColor(Color.YELLOW);
		canvas.drawCircle(120, 130, 10, paint);//参数一水平x轴,参数二垂直y轴,参数三圆半径,参数四Paint对象
		canvas.restore();

	}

6:画线。drawLine(float startX, float startY, float stopX, float stopY, Paintpaint)//参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,最后一个参数为Paint
画刷对象。

protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		canvas.drawColor(Color.YELLOW);
		paint.setAntiAlias(true);//抗锯齿
		paint.setDither(true);//抖动,让图形看起来没有毛边
		paint.setColor(Color.RED);
		paint.setStrokeWidth(3);//设置线的粗细
		canvas.drawLine(20, 50, 200, 100, paint);//参数一起始点的x轴位置,参数二起始点的y轴位置,参数三终点的x轴水平位置,参数四y轴垂直位置,最后一个参数为Paint 画刷对象。

	}

7:画弧线:

参数一是RectF对象,一个矩形区域椭圆形的界限用于定义在形状、大小、电弧,参数二是起始角(度)在电弧的开始,

参数三扫描角(度)开始顺时针测量的,参数四是如果这是真的话,包括椭圆中心的电弧,并关闭它,如果它是假这将是一个弧线,参数五是Paint对象;

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Paint paint = new Paint();
		canvas.drawColor(Color.BLUE);
		//画弧
		paint.setStyle(Style.STROKE);//设置空心
		paint.setColor(Color.RED);//设置颜色
		paint.setStrokeWidth(3);//设置粗细
		RectF oval = new RectF();
		oval.set(50, 50, 200, 200);
//		oval.contains(100, 55, 205, 205);
//		canvas.drawArc(oval, 180, 180, true, paint);
		canvas.drawArc(oval, 180, 180, false, paint);

	}

当为true时

canvas.drawArc(oval, 180, 180, true, paint);

当为false时

canvas.drawArc(oval, 180, 180, false, paint);

时间: 2024-08-03 20:00:10

Android 用Canvas画textview、bitmap、矩形(裁剪)、椭圆、线、点、弧的相关文章

Android利用canvas画各种图形

canvas通俗的说就是一张画布,我们可以使用画笔paint,在其上面画任意的图形. 原理: 可以把canvas视为Surface的替身或者接口,图形便是绘制在Surface上的.Canvas封装了所有的绘制调用.通过Canvas, 绘制到Surface上的内容首先存储到一个内存区域(也就是对应的Bitmapz中),该Bitmap最终会呈现到窗口上. 使用: 1.Canvas可以直接new Canvas(): 2.在View中重写OnDraw()方法,里面有一个Canvas,今天讨论的内容. 方

Android利用canvas画画板

首先新建一个项目工程,建立文件,如下图所示 首先配置页面布局文件activity_main.xml,如下图所示: 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 a

Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

1.首先说一下canvas类: Class OverviewThe Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path,

(转)Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

来自:http://blog.csdn.net/rhljiayou/article/details/7212620 1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writi

Android利用canvas画各种图形 及Paint用法 .

引自:http://blog.csdn.net/carlfan/article/details/8139984 1.首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing

android 使用Canvas画箭头

public class MyCanvas extends View{        private Canvas myCanvas;    private Paint myPaint=new Paint();        public MyCanvas(Context context) {        super(context);        // TODO Auto-generated constructor stub    }     public MyCanvas(Context

Android之canvas详解

首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, t

Android:使用Canvas合并Bitmap

关键点 canvas.drawBitmap(bitmap, srcRect, dstRect, null); 将bitmap的srcRect区域绘制到canvas的dstRect区域 Demo main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&qu

android在设计界面 drawable,canvas,和Bitmap

一.drawable  图形对象,可以转载常用格式的图像,可能是(位图)Bitmapdrawable,或者shapedrawable(图形),还可能是多种其他图片格式GIF,PNG,JEPG 二.Bitmap 就是位图,用于图片的处理 三.Canvas 意为画布,就是绘画的目标区域,用来管理Bitmp或者path路径 下面就是讲到drawable转换成Bitmap的方法 我所知有两种方法(两种方法谁好谁坏,得看情况) (1)常见的一种方法就是创建个Bitmap出来,再用画布绑定这个位图,将dra