上一节讨论了手写功能中的删除、恢复和清空功能,那么,画板也就是涂鸦怎么能没有撤销、恢复与清空的功能呢,今天就来实现下。
终于会做gif图了,看下面的动态图,是不是和QQ白板功能很像。
之前就简单的只实现了在画板上绘图的功能,所以当时将自定义view直接写在了activity中,这一节由于要实现撤销、恢复及清空的功能,所以将分离出来,单独写成了一个java文件PaintView.java,在该自定义view中实现画板的基本操作。
因为将自定义view单独分离出来,所以需要改到activity的布局文件:如下
activity_paint.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.notes.PaintView
android:id="@+id/paint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
></com.example.notes.PaintView>
<GridView
android:id="@+id/paintBottomMenu"
android:layout_width="match_parent"
android:layout_height="45dp"
android:numColumns="auto_fit"
android:background="@drawable/navigationbar_bg"
android:horizontalSpacing="10dp"
android:layout_alignParentBottom="true"
></GridView>
</RelativeLayout>
其中com.example.notes.PaintView为自定义view
这节要实现的操作有撤销,恢复,清空和保存,下面分别讨论三个操作的主要思想:
要实现撤销与恢复,这里有个前提,就是要将每次绘制的路径存入栈中,这里是存入List中。
1. 撤销功能:
前提:将每次绘制的路径存入List中,即存入savePath中
步骤:
1) 将画布清空,这里可以使用画成的初始化操作
2) 将savePath中的最后一个路径保存到另一个List中,即deletePath(用于恢复),并且将此路径从savePath中删除
3) 取出savePath中的所有的路径,重绘在画布上面
2. 恢复功能:
前提:将每次撤销的路径存入List中,即存入deletePath中
步骤:
1) 取出deletePath中的最后一个路径,并保存到savePath中