安卓 基于AppCompatImageView的画板视图,任意画线

PaletteLib

介绍

画板视图,支持任意画线段的一个视图组件
继承至特定View可以用原View的基本特性
支持视图导出为图片bitmap以及导出到文件

依赖引入

工程的build.gradle文件添加

allprojects {
    repositories {
        google()
        mavenCentral()

        //jitpack 仓库
        maven { url 'https://jitpack.io' }
    }
}

APP的build.gradle文件添加

dependencies {
    ...
    implementation 'com.gitee.osard:palettelib:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
}

使用

  • 基础画板View
    /**
     * 用途:画板视图,自定义继承控件后,使用此类即可简单集成画板
     * <p>
     * 注:布局和部分控件需要设置背景后才能绘制线段
     *
     * <p>
     * 作者:MJSoftKing
     */
    public class BasePalette {}
  • 自定义任意View添加画板能力
    /*
     布局控件需要设置背景后才能绘制线段
    */
    public class TestView extends RelativeLayout {

        //此对象拥有画板操作权
        public BasePalette palette;

        public TestView(@NonNull Context context) {
            super(context);
            palette = BasePalette.create(this);
        }

        public TestView(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            palette = BasePalette.create(this);
        }

        public TestView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            palette = BasePalette.create(this);
        }

        @Override
        @SuppressLint("ClickableViewAccessibility")
        public boolean onTouchEvent(MotionEvent event) {
            return palette.onTouchEvent(event) || super.onTouchEvent(event);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            palette.onDraw(canvas);
        }
    }
  • 已提供的View
    PaletteImageView   继承至 AppCompatImageView
    PaletteTextView    继承至 AppCompatTextView
    PaletteView        继承至 View
  • layout引入
<com.mjsoftking.palettelib.PaletteImageView
    android:id="@+id/handView"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@mipmap/ic_launcher" />
  • 组件提供的方法
    /**
     * 设置画笔颜色,HTML形式
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     *
     * @param htmlColor 如:#000000
     */
    public void setPaintColor(String htmlColor);
    /**
     * 设置画笔颜色,ColorRes资源
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     */
    public void setPaintColor(@ColorRes int color);
    /**
     * 设置画笔宽度
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     *
     * @param width 单位像素
     */
    public void setStrokeWidth(float width);
    /**
     * 设置画笔宽度
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     *
     * @param dbWidth 单位db
     */
    public void setStrokeDbWidth(int dbWidth);
    /**
      * 设置是否启用绘制能力,关闭后和普通组件无区别
      * <p>
      * 默认:启用
      */
     public void setEnableDraw(boolean enableDraw);
    /**
     * 设置自定义画笔,优先级高于单属性控制
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     *
     * @param paint 自定义画笔对象
     */
    public void setPaint(Paint paint);
    /**
     * 获取所有线段
     * <p>
     * 每个线段下都有一组点的列表
     */
    public List<List<PalettePoint>> getLines();
    /**
     * 当触摸抬起时,所画线段只是起点坐标或者所有点坐标均相同时,移除此次所画线段,因为他不是线段只是点
     * <p>
     * 默认启用此策略
     */
    public void setRemoveLastNotLine(boolean removeLastNotLine);
    /**
     * 是否响应 onClick 事件
     * <p>
     * 默认:关闭
     * 启用时,触摸抬起时触摸点在控件上时会响应点击事件,反之不响应
     * 关闭时,不响应点击事件
     */
     public void setEnableOnClick(boolean enableOnClick);
    /**
     * 撤销上一次绘制
     */
    public void revocation();
    /**
     * 清空绘制
     */
    public void clear();
    /**
     * 获取视图的截图
     */
    public Bitmap screenShot();
    /**
     * 将视图的截图保存到指定的文件
     *
     * @param fileName 全路径携带文件名,绝对路径
     * @param format   格式,参考{@link Bitmap.CompressFormat}
     * @param quality  压缩质量,参考{@link Bitmap.CompressFormat}
     */
    public void saveBitmap(String fileName, Bitmap.CompressFormat format, int quality) throws IOException ;
    /**
     * 将视图的截图保存到指定的文件
     * <p>
     * 默认保存到:存储-Android-data-包名-files-images文件夹下
     * 文件格式 png
     * png格式质量字段被忽略
     */
    public String saveBitmap() throws IOException ;

License

Copyright 2021 mjsoftking

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智识家

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

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

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

打赏作者

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

抵扣说明:

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

余额充值