android.graphics(一)shapes

这里写图片描述
先看下shapes的结构

PathShape extends Shape
RectShape extends Shape

ArcShape extends RectShape
OvalShape extends RectShape
RoundRectShape extends RectShape

这里写图片描述

其中:

ArcShape:扇形;
OvalShape:椭圆形;
RoundRectShape :圆角矩形;
PathShape:指定曲线图形;

1、ArcShape

ArcShape(float startAngle, float sweepAngle)

startAngel
为直角坐标系中起始位置角度(如45,代表45°角位置)

sweepAngel
为起始位置和椭圆中心连线在直角坐标系中扫过的角度(如-135°,代表连线逆时针扫过135°角)

        ArcShape arcShape = new ArcShape(30, 180);
        ShapeDrawable arcShapeDrawable = new ShapeDrawable(arcShape);
        //指定填充颜色
        arcShapeDrawable.getPaint().setColor(Color.RED);
        // 指定填充模式FILL,STROKE,FILL_AND_STROKE
        arcShapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        findViewById(R.id.textView1).setBackgroundDrawable(arcShapeDrawable);

2、OvalShape

OvalShape是指给定的矩形框的内切椭圆饼

        OvalShape ovalShape = new OvalShape();
        ShapeDrawable ovalShapeDrawable = new ShapeDrawable(ovalShape);
        ovalShapeDrawable.getPaint().setColor(Color.RED);
        ovalShapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        findViewById(R.id.textView2).setBackgroundDrawable(ovalShapeDrawable);

3、RoundRectShape

这里写图片描述

RoundRectShape(float[] outerRadii, RectF inset,float[] innerRadii)

outerRadii:指定一个外部(圆角)矩形
inset:内部矩形与外部矩形的距离
innerRadii:一个可选的 内部(圆角)矩形

一个包含8个弧度值,指定外部圆角矩形的 4个角部的弧度

 new float[] {l, l, t, t, r, r, b, b};

参数: 12代表左上角, 34代表右上角, 56代表右下角, 78代表左下角,如果没弧度的话,传入null即可。
// 外部矩形弧度
float[] outerRadii = new float[]{8, 8, 8, 8, 8, 8, 8, 8};
// 内部矩形与外部矩形的距离
RectF inset = new RectF(50, 20, 50, 50);
// 内部矩形弧度
float[] innerRadii = new float[]{20, 20, 20, 20, 20, 20, 20, 20};

RoundRectShape roundRectShape = new RoundRectShape(outerRadii, inset, innerRadii);
ShapeDrawable roundRectShapeDrawable = new ShapeDrawable(roundRectShape);
        roundRectShapeDrawable.getPaint().setColor(Color.RED);
        roundRectShapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        findViewById(R.id.textView3).setBackgroundDrawable(roundRectShapeDrawable);

4、PathShape

路径绘图

PathShape(Path path, float stdWidth, float stdHeight)

stdWidth:标准宽度
stdHeight:标准高度

        Path path = new Path();
        path.moveTo(50, 0);
        path.lineTo(0, 50);
        path.lineTo(50, 100);
        path.lineTo(100, 50);
        path.lineTo(50, 0);
        PathShape pathShape = new PathShape(path, 200, 100);
        ShapeDrawable drawable = new ShapeDrawable(pathShape);
        drawable.getPaint().setColor(Color.RED);
        drawable.getPaint().setStyle(Paint.Style.FILL);
        findViewById(R.id.textView4).setBackgroundDrawable(drawable);

参考:
http://blog.youkuaiyun.com/vqqyuan/article/details/42921315
http://blog.youkuaiyun.com/zhangphil/article/details/52025152
http://blog.youkuaiyun.com/silk2018/article/details/19192329
http://blog.youkuaiyun.com/jjwwmlp456/article/details/46928859

package com.example.canvasdemo;//声明当前类所在的包路径。 //注意:canvasdemo (文件夹 项目名)包名 需与项目结构致,否则会导致编译错误。 //导入 Android SDK 中的相关类,用于实现画布绘制功能。 import android.content.Context;//提供应用环境信息(如资源、系统服务)。 import android.graphics.Canvas;//画布,用于绘制图形、文本等。 import android.graphics.Color; import android.graphics.Paint;//画笔,用于定义绘制样式(如颜色、粗细)。 import android.util.AttributeSet; import android.view.View;//所有 UI 组件的基类,CustomView 继承自 View。 import androidx.annotation.Nullable; public class CustomView extends View { //定义个自定义视图类,继承自 View。 private Paint paint; public CustomView(Context context) { super(context); initPaint(); } public CustomView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initPaint(); } // 初始化画笔 private void initPaint() { paint = new Paint(); paint.setColor(Color.RED); // 设置颜色 paint.setStyle(Paint.Style.FILL); // 填充模式 paint.setAntiAlias(true); // 开启抗锯齿 } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);//系统传入的画布对象,用于绘制图形。 // 1. 先绘制白色背景(覆盖整个画布) canvas.drawColor(Color.WHITE);//将整个画布填充为白色。 // 2. 绘制红色圆形 paint.setColor(Color.RED);//设置画笔颜色为红色。 canvas.drawCircle(300, 300, 100, paint); //圆心坐标(X, Y)。半径。使用的画笔对象。 // 3. 绘制蓝色矩形 paint.setColor(Color.BLUE);//设置画笔颜色为蓝色。 canvas.drawRect(100, 500, 500, 800, paint); //矩形左上角坐标(X, Y)。矩形右下角坐标(X, Y)。使用的画笔对象。 // 4. 绘制黑色文本 paint.setColor(Color.BLACK); paint.setTextSize(50);//设置文本大小为 50 像素。 canvas.drawText("Hello Canvas!", 100, 200, paint); } //文本内容。文本基线起点坐标(X, Y)。使用的画笔对象。 } 基于以上代码进行修改,添加功能:可以用鼠标点击图形可以识别并显示提示是否需要删除? 并且用鼠标长按图形可以移动该图像。重新要求给我生成个完整的代码,相应包的导入也不能少。还要说明说明配置文件的代码。要有过程,以及最后的两个完整代码。说明文件创建的步骤,并给上面代码加上相应的注释。
03-08
import android.content.Context; import android.graphics.*; import android.util.AttributeSet; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import androidx.appcompat.app.AlertDialog; import java.util.ArrayList; import java.util.List; import java.util.Random; public class DrawingView extends View { private List<Shape> shapes = new ArrayList<>(); private Shape selectedShape; private Paint paint; private GestureDetector gestureDetector; private static final int[] COLORS = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.CYAN}; // 构造函数 public DrawingView(Context context) { super(context); init(); } public DrawingView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); gestureDetector = new GestureDetector(getContext(), new GestureListener()); } // 形状基类 private abstract static class Shape { int color; RectF bounds; abstract boolean contains(float x, float y); } // 具体形状实现 private static class Circle extends Shape { @Override boolean contains(float x, float y) { float cx = bounds.centerX(), cy = bounds.centerY(); return Math.hypot(x - cx, y - cy) <= bounds.width() / 2; } } private static class Rect extends Shape { @Override boolean contains(float x, float y) { return bounds.contains(x, y); } } private static class Triangle extends Shape { @Override boolean contains(float x, float y) { Path path = new Path(); path.moveTo(bounds.left, bounds.bottom); path.lineTo(bounds.centerX(), bounds.top); path.lineTo(bounds.right, bounds.bottom); path.close(); Region region = new Region();
03-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哆啦A梦z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值