標籤:
1,關於Rect和RectF類的區別以前一直沒有去關注它,剛剛瞭解了一下才知道都是用來確定矩形的地區,不過Rect是int類型的座標而RectF是float類型的座標,所以說RectF要更加精確。現在是要使用paint畫出一個機器人的圖片,如機器人的圖片:
2, 看一下我們做出來的效果
再看看代碼,基本上都是使用的RectF這個類來操作
package com.wangjitao.myview.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.view.View;/** * Created by wangjitao on 2016/10/10 0010. * 主要是對矩形的使用 */public class MyRectFView extends View { public MyRectFView(Context context) { super(context); } public MyRectFView(Context context, AttributeSet attrs) { super(context, attrs); } public MyRectFView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setColor(0xFFA4C739); //繪製機器人的頭部(一個半圓弧) RectF rectF_head = new RectF(10, 10, 100, 100); //設定位移量 rectF_head.offset(100, 20); //繪製曲線,且去掉最中間的圓心 canvas.drawArc(rectF_head, -10, -160, false, paint); //繪製眼睛(眼睛是兩個白色的小圓) paint.setColor(Color.WHITE); canvas.drawCircle(135, 53, 4, paint); canvas.drawCircle(175, 53, 4, paint); //繪製兩根天線(就是兩個直線) paint.setColor(0xFFA4C739); paint.setStrokeWidth(2); canvas.drawLine(120, 15, 135, 35, paint); canvas.drawLine(190, 15, 175, 35, paint); //繪製身體(一個簡單的圓角矩形,使用的是兩個矩形,第一個矩形是正常的矩形,第二個矩形是待圓角的矩形) canvas.drawRect(110, 75, 200, 150, paint); //繪製矩形 RectF rectf_body = new RectF(110, 140, 200, 160); canvas.drawRoundRect(rectf_body, 10, 10, paint); //繪製圓角矩形 //繪製胳膊(帶圓弧的矩形) RectF rectf_arm = new RectF(85, 75, 105, 140); canvas.drawRoundRect(rectf_arm, 10, 10, paint); //繪製左側的胳膊 rectf_arm.offset(120, 0); //設定在X軸上位移120像素 canvas.drawRoundRect(rectf_arm, 10, 10, paint); //繪製右側的胳膊 //繪製兩條腿(也是兩個帶矩形的矩形) RectF rectf_leg = new RectF(125, 150, 145, 200); canvas.drawRoundRect(rectf_leg, 10, 10, paint); //繪製左側的腿 rectf_leg.offset(40, 0); //設定在X軸上位移40像素 canvas.drawRoundRect(rectf_leg, 10, 10, paint); //繪製右側的腿 }}
Android -- 自訂View小Demo,關於Rect繪製Android機器人(一)