iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android自定义View实现简易画板功能
  • 937
分享到

Android自定义View实现简易画板功能

2024-04-02 19:04:59 937人浏览 泡泡鱼
摘要

本文实例为大家分享了Android自定义View实现简易画板的具体代码,供大家参考,具体内容如下 自定义VIew实现简易画板效果,功能包括清空、选择颜色,选择大小,效果如下 画板布

本文实例为大家分享了Android自定义View实现简易画板的具体代码,供大家参考,具体内容如下

自定义VIew实现简易画板效果,功能包括清空、选择颜色,选择大小,效果如下

画板布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!-- 这是一个自定义组合控件,包含涂鸦板及右边三个按钮 -->
    <com.android.mytest.GraffitiBroadLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

自定义view GraffitiBroadLayout 布局文件 view_graffiti.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:baselineAligned="false">
    <!-- 自定义涂鸦板View -->
    <com.android.mytest.GraffitiView
        android:id="@+id/graffiti_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:visibility="Gone"
        android:layout_gravity="right"
        android:layout_marginRight="100dp"
        android:layout_width="80dp"
        android:layout_height="wrap_content"/>
    <!-- 右侧三个按钮 清空、颜色、粗细 -->
    <LinearLayout
        android:layout_gravity="right"
        android:orientation="vertical"
        android:layout_width="100dp"
        android:layout_height="match_parent">
        <Button
            android:id="@+id/clear_all"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/select_color"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/select_size"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</FrameLayout>

画板布局中包括一个 自定义涂鸦view,recyclerView用于显示选择颜色、大小,三个按钮,分别是:
清空、选择颜色、选择大小

// 继承LinearLayout 
public class GraffitiBroadLayout extends LinearLayout {
    
    private final int[] colors = {R.color.red,R.color.green,R.color.blue};// 颜色数组
    private final int[] sizes = {5,10,15,20};// 画笔size数组

    private Context mContext;
    private View mView;
    private GraffitiView mGraffitiView;
    private RecyclerView mRecyclerView;

    public GraffitiBroadLayout(Context context) {
        super(context);
    }

    public GraffitiBroadLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        // 获取布局View
        mView = LayoutInflater.from(context).inflate(R.layout.view_graffiti, this,true);
        initView();// 初始化并设置点击事件
    }

    private void initView() {
        Button clearAllBtn = mView.findViewById(R.id.clear_all);
        Button selectColorBtn = mView.findViewById(R.id.select_color);
        Button selectSizeBtn = mView.findViewById(R.id.select_size);
        mGraffitiView = mView.findViewById(R.id.graffiti_view);
        mRecyclerView = mView.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        // 点击清空 画板
        clearAllBtn.setOnClickListener(v -> mGraffitiView.clearAllPath());
        // 选择画笔颜色
        selectColorBtn.setOnClickListener(v -> {
            GraffitiAdapter adapter = new GraffitiAdapter(mContext,colors,1);
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.setVisibility(VISIBLE);
        });
        // 选择画笔大小
        selectSizeBtn.setOnClickListener(v -> {
            GraffitiAdapter adapter = new GraffitiAdapter(mContext,sizes,2);
            mRecyclerView.setAdapter(adapter);
            mRecyclerView.setVisibility(VISIBLE);
        });

    }
    // 自定义adapter
    class GraffitiAdapter extends RecyclerView.Adapter<GraffitiViewHolder>{

        Context mContext;
        int[] cs;
        int type;// 用于判断显示颜色 还是 选择大小

        public GraffitiAdapter(Context mContext, int[] cs,int type) {
            this.mContext = mContext;
            this.cs = cs;
            this.type = type;
        }

        @NonNull
        @Override
        public GraffitiViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            // 获取 item 布局
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_recycler_graffiti,parent,false);
            return new GraffitiViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull GraffitiViewHolder holder, int position) {
            if(type == 1){// 颜色
                holder.view.setBackgroundResource(cs[position]);
                holder.click.setOnClickListener(v -> {
                    // 设置画笔颜色
                    mGraffitiView.setPaintColor(cs[position]);
                    mRecyclerView.setVisibility(GONE);
                });
            }else if(type == 2){// size
                ViewGroup.LayoutParams lp = holder.view.getLayoutParams();
                lp.height = sizes[position]*2;
                holder.view.setLayoutParams(lp);
                holder.view.setBackgroundResource(R.color.black);
                holder.click.setOnClickListener(v -> {
                    // 设置画笔size
                    mGraffitiView.setPaintSize(sizes[position]);
                    mRecyclerView.setVisibility(GONE);
                });
            }

        }

        @Override
        public int getItemCount() {
            return cs.length;
        }
    }

    static class GraffitiViewHolder extends RecyclerView.ViewHolder{
        View view;
        LinearLayout click;
        public GraffitiViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.item_view);
            click = itemView.findViewById(R.id.click_view);
        }
    }

}

item_recycler_graffiti.xml 布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_marginTop="10dp"
    android:layout_height="50dp"
    android:gravity="center">
    <LinearLayout
        android:id="@+id/click_view"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:ignore="UselessParent">
        <View
            android:id="@+id/item_view"
            android:layout_width="match_parent"
            android:layout_height="40dp"/>
    </LinearLayout>

</LinearLayout>

自定义涂鸦View:

public class GraffitiView extends View {

    private final Context mContext;
    private canvas mCanvas;// 
    private Bitmap mBitmap;// 用于保存绘制过的路径的 bitmap
    private Paint mPaint;// 画笔
    private Path mPath;// 触摸时的路径

    private int width,height;

    public GraffitiView(Context context) {
        this(context,null);
    }

    public GraffitiView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    private void init() {
        // 初始化 画笔
        mPaint = new Paint();
        mPaint.setColor(mContext.getColor(R.color.green));//画笔颜色
        mPaint.setAntiAlias(true);// 抗锯齿
        mPaint.setDither(true);// 抖动处理
        mPaint.setStrokeJoin(Paint.Join.ROUND);//画笔连接处 圆弧
        mPaint.setStrokeCap(Paint.Cap.ROUND);//画笔拐弯处风格 圆弧
        mPaint.setStyle(Paint.Style.STROKE);//画笔格式
        mPaint.setStrokeWidth(10f);//画笔宽度

        mPath = new Path();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        if(mBitmap == null){
            // 初始化 bitmap
            mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_4444);
        }
        if(mCanvas == null){
            mCanvas = new Canvas(mBitmap);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制路径 
        // 因为每次触摸都会生成一条新的路径,直接绘制会使原路径消失,因此
        mCanvas.drawPath(mPath,mPaint);// 先将路径绘制到 bitmap 上,再绘制到当前画布中
        canvas.drawBitmap(mBitmap, 0,0,mPaint);// 将bitmap绘制到当前画布中
    }

    
    public void clearAllPath(){
        mBitmap = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_4444);
        mCanvas = new Canvas(mBitmap);
        mPath.reset();
        invalidate();
    }

    
    public void setPaintColor(int resource){
        mPaint.setColor(mContext.getColor(resource));
    }

    
    public void setPaintSize(int size){
        mPaint.setStrokeWidth(size);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        float x = event.getX();
        float y = event.getY();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                mPath = new Path();// 每次触摸 生成一条新的路径
                mPath.moveTo(x,y);
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(x,y);
                invalidate();
                break;
        }
        return true;
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android自定义View实现简易画板功能

本文链接: https://www.lsjlt.com/news/149010.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android自定义View实现简易画板功能
    本文实例为大家分享了Android自定义View实现简易画板的具体代码,供大家参考,具体内容如下 自定义VIew实现简易画板效果,功能包括清空、选择颜色,选择大小,效果如下 画板布...
    99+
    2022-11-13
  • Android怎么自定义View实现简易画板功能
    这篇文章主要介绍“Android怎么自定义View实现简易画板功能”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Android怎么自定义View实现简易画板功能”文章能帮助大家解决问题。自定义VIe...
    99+
    2023-06-30
  • Android自定义SurfaceView实现画板功能
    接触了这么久的View,总不能一直停留在View里,现在开始呢,就要学习一个新的知识点:SurfaceView,实际上SurfaceView与View的原理都差不多,只是效率和...
    99+
    2022-06-06
    surfaceview 画板 Android
  • Android自定义view-电子签名画板
       电子签名作为用户的电子凭证,在很多业务中都有用到! 一.自定义电子签名画板 package com.kxf.androidtestdemo.view; import a...
    99+
    2022-06-06
    view 电子签名 电子 画板 Android
  • Android自定义View实现时钟功能
    最近在练习自定义view, 想起之前面试的时候笔试有道题是写出自定义一个时钟的关键代码. 今天就来实现一下. 步骤依然是先分析, 再上代码. 实现效果 View分析 时钟主要分为五...
    99+
    2022-11-13
  • Android自定义View实现自动吸附功能
    本文实例为大家分享了Android实现自动吸附功能的具体代码,供大家参考,具体内容如下 1.简述 最近开发app过程中要实现拖动view后要可以自动吸附功能,所以需要自定义vi...
    99+
    2022-06-06
    自动 view Android
  • Android自定义控件实现简单写字板功能
    先来看看效果图 就是简单的根据手指写下的轨迹去画出内容 一、实现 之前一篇文章里提到了android官方给出的自定义控件需要考虑以下几点: 创建View 处理View的布局...
    99+
    2022-06-06
    写字板 Android
  • Android自定义View实现气泡动画
    本文实例为大家分享了Android自定义View实现气泡动画的具体代码,供大家参考,具体内容如下 一、前言 最近有需求制作一个水壶的气泡动画,首先在网上查找了一番,找到了一个文章:A...
    99+
    2022-11-12
  • Android自定义View简易折线图控件(二)
    继续练习自定义View,这次带来的是简易折线图,支持坐标点点击监听,效果如下: 画坐标轴、画刻度、画点、连线。。x、y轴的数据范围是写死的 1 <= x <= 7...
    99+
    2022-06-06
    折线图 view Android
  • Android自定义View绘图实现拖影动画
    前几天在“Android绘图之渐隐动画”一文中通过画线实现了渐隐动画,但里面有个问题,画笔较粗(大于1)时线段之间会有裂隙,我又改进了一下。这次效果好多了。 先看效果吧: 然...
    99+
    2022-06-06
    view 动画 Android
  • Android自定义View绘图实现渐隐动画
    实现了一个有趣的小东西:使用自定义View绘图,一边画线,画出的线条渐渐变淡,直到消失。效果如下图所示: 用属性动画或者渐变填充(Shader)可以做到一笔一笔的变化,但要想...
    99+
    2022-06-06
    view 动画 Android
  • Android自定义View实现动画效果详解
    目录帧动画补间动画属性动画帧动画 帧动画就是给定一个完整动画的所有关键帧,由大脑想象中间的变化过程的一种动画。 <xml version="1.0" encoding="utf...
    99+
    2023-02-02
    Android自定义View实现动画 Android 动画 Android自定义View
  • Android如何实现自定义view画圆效果
    这篇文章主要介绍了Android如何实现自定义view画圆效果,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。看图代码:package sjx.com.custonv...
    99+
    2023-05-30
    android view
  • Android如何实现自定义View展开菜单功能
    这篇文章主要为大家展示了“Android如何实现自定义View展开菜单功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Android如何实现自定义View展开菜单功能”这篇文章吧。效果图思路下...
    99+
    2023-05-31
    android view
  • Android中怎么通过自定义View实现画圆
    Android中怎么通过自定义View实现画圆,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。引入布局<xml version="1.0...
    99+
    2023-05-30
    android
  • Android 自定义view实现水波纹动画效果
    在实际的开发中,很多时候还会遇到相对比较复杂的需求,比如产品妹纸或UI妹纸在哪看了个让人兴奋的效果,兴致高昂的来找你,看了之后目的很明确,当然就是希望你能给她;在这样的关键时候,身子板就一定得硬了,可千万别说不行,爷们儿怎么能说不行呢;好了...
    99+
    2023-05-31
    android 水波纹 roi
  • Android自定义View实现水波纹引导动画
    一、实现效果图 关于贝塞尔曲线 二、实现代码 1.自定义view package com.czhappy.showintroduce.view; import and...
    99+
    2022-06-06
    view 动画 Android
  • Android自定义View实现loading动画加载效果
     项目开发中对Loading的处理是比较常见的,安卓系统提供的不太美观,引入第三发又太麻烦,这时候自己定义View来实现这个效果,并且进行封装抽取给项目提供统一的lo...
    99+
    2022-06-06
    view loading Android
  • Android自定义view实现电影票在线选座功能
    先看看电影票在线选座功能实现的效果图: 界面比较粗糙,主要看原理。 这个界面主要包括以下几部分 1、座位 2、左边的排数 3、左上方的缩略图 4、缩略图中的红色...
    99+
    2022-06-06
    电影 view Android
  • android自定义View实现简单五子棋游戏
    做一个五子棋练练手,没什么特别的,再复习一下自定义View的知识,onMeasure,MeasureSpec , onDraw以及OnTouchEvent方法等。 效果图 代码如下...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作