1 Star 4 Fork 1

OS-Android / PaletteLib

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

PaletteLib

License API

介绍

画板视图,支持任意画线段的一个视图组件 继承至特定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.3.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
}

使用

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

    public BasePalette palette;

    public TestView(@NonNull Context context) {
        super(context);
        init();
    }

    public TestView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TestView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        palette = BasePalette.create(this);
//        palette.setEnableEndToEnd(true);
//        palette.setEnableOnce(true);

//        //重写为画多边形,此时绘制方法完全由此方法实现 todo 方法二
//        palette.setDraw((canvas, lines) -> {
//            for (PaletteDrawConfig config : lines) {
//                if(config.points.size() <= 1) continue;
//
//                Path path1 = new Path();
//                path1.moveTo(config.points.get(0).x, config.points.get(0).y);
//                for (int i = 1; i < config.points.size(); ++i) {
//                    path1.lineTo(config.points.get(i).x, config.points.get(i).y);
//                }
//                path1.close();//封闭
//                config.paint.setStyle(Paint.Style.STROKE);
//                canvas.drawPath(path1, config.paint);
//            }
//        });
    }

    /**
     * todo 方法一 实现{@link BasePalette.IDraw}接口,则无需手动设置绘制方法,默认进行了重绘
     * <p>
     * 重写为画多边形,此时绘制方法完全由此方法实现
     */
    public void onDraw(Canvas canvas, List<PaletteDrawConfig> lines) {
        for (PaletteDrawConfig config : lines) {
            if (config.points.size() <= 1) continue;

            Path path1 = new Path();
            path1.moveTo(config.points.get(0).x, config.points.get(0).y);
            for (int i = 1; i < config.points.size(); ++i) {
                path1.lineTo(config.points.get(i).x, config.points.get(i).y);
            }
            path1.close();//封闭
            config.paint.setStyle(Paint.Style.STROKE);
            canvas.drawPath(path1, config.paint);
        }
    }

    @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
    PaletteSurfaceView     继承至 SurfaceView
  • layout引入
<com.mjsoftking.palettelib.PaletteImageView
    android:id="@+id/handView"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="@mipmap/ic_launcher" />
  • BasePalette 提供的方法
    /**
     * 设置自定义的绘制方法,自行绘制所有点
     * <p>
     * 默认:以线段的方式绘制
     */
    public void setDraw(IDraw draw);
    /**
     * 设置画笔颜色,HTML形式
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     * <p>
     * 先设置{@link BasePalette#setPaint(Paint)}后在使用此方法会清空paint的设置
     *
     * @param htmlColor 如:#FF000000
     */
    public void setPaintColor(String htmlColor);
    /**
     * 设置只画直线
     * <p>
     * 设置后清除所有线段,后续绘制获取点坐标时只会获得首尾坐标
     */
    public void setEnableLine(boolean enableLine);
    /**
     * 设置画笔颜色,ColorRes资源
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     * <p>
     * 先设置{@link BasePalette#setPaint(Paint)}后在使用此方法会清空paint的设置
     */
    public void setPaintColor(@ColorRes int color);
    /**
     * 设置画笔宽度
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     * <p>
     * 先设置{@link BasePalette#setPaint(Paint)}后在使用此方法会清空paint的设置
     *
     * @param width 单位像素
     */
    public void setStrokeWidth(float width);
    /**
     * 设置画笔宽度
     * <p>
     * 设置后下次落笔生效,先前已画线段不会改变
     * <p>
     * 先设置{@link BasePalette#setPaint(Paint)}后在使用此方法会清空paint的设置
     *
     * @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();
    /**
     * 设置线段点坐标,使用形参内自带的画笔绘制,如未设置画笔,则使用Paint对象默认画笔
     * <p>
     * 坐标是基于所在view的像素点
     * <p>
     * 设置后调用视图的{@link View#invalidate()}方法重绘,跨线程时使用{@link View#postInvalidate()}
     * <p>
     * 此方法不受{@link BasePalette#setEnableEndToEnd(boolean)}和
     * {@link BasePalette#setEnableOnce(boolean)}限制。
     */
    public void setLines(List<PaletteDrawConfig> l);
    /**
     * 设置线段点坐标,使用全局设置的默认画笔(所有画笔统一为最后一次设置的画笔)
     * <p>
     * 坐标是基于所在view的像素点
     * <p>
     * 设置后调用视图的{@link View#invalidate()}方法重绘,跨线程时使用{@link View#postInvalidate()}
     * <p>
     * 此方法不受{@link BasePalette#setEnableEndToEnd(boolean)}和
     * {@link BasePalette#setEnableOnce(boolean)}限制。
     */
    public void setLinesUseDefaultPaint(List<PaletteDrawConfig> l);
    /**
     * 当触摸抬起时,所画线段只是起点坐标或者所有点坐标均相同时,移除此次所画线段,因为他不是线段只是点
     * <p>
     * 默认启用此策略
     */
    public void setRemoveLastNotLine(boolean removeLastNotLine);
    /**
     * 是否响应 onClick 事件
     * <p>
     * 默认:关闭
     * 启用时,触摸抬起时触摸点在控件上时会响应点击事件,反之不响应
     * 关闭时,不响应点击事件
     */
     public void setEnableOnClick(boolean enableOnClick);
    /**
     * 设置是否只能绘制一次,第二次绘制时触摸事件将会向下传递
     * 注:仅在绘制缓存为0时可以设置,否则设置均会失败
     * <p>
     * 默认:关闭,可绘制多次
     *
     * @return true: 设置成功,false: 设置失败
     */
    public boolean setEnableOnce(boolean enableOnce);
    /**
     * 设置是否启用首尾相连
     * <p>
     * 注:仅在至少已有3个坐标点时,首尾相连才可成立
     * <p>
     * 默认:关闭,启用时,最后一次绘制的点与首点形成线段,即将起点加入到列表末尾
     */
    public void setEnableEndToEnd(boolean enableEndToEnd);
    /**
     * 撤销上一次绘制
     */
    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 ;
    /**
     * 以线段的方式绘制,默认绘制方法
     */
    public void drawLine(Canvas canvas, PaletteDrawConfig line);
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public float dipToPx(float dpValue);

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.
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS

简介

画板视图,支持任意画线段的一个视图组件 继承至特定View可以用原View的基本特性 支持视图导出为图片bitmap以及导出到文件 展开 收起
Android
Apache-2.0
取消

发行版 (2)

全部

贡献者

全部

近期动态

加载更多
不能加载更多了
Android
1
https://gitee.com/osard/palettelib.git
git@gitee.com:osard/palettelib.git
osard
palettelib
PaletteLib
master

搜索帮助