• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PaletteLib: 画板视图,支持任意画线段的一个视图组件 继承至特定View可以用原View的 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

PaletteLib

开源软件地址:

https://gitee.com/osard/palettelib

开源软件介绍:

PaletteLib

LicenseAPI

介绍

画板视图,支持任意画线段的一个视图组件继承至特定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 mjsoftkingLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap