笔迹动画的存与取--另类的特效动画

来源:互联网 发布:艾腾软件招聘 编辑:程序博客网 时间:2024/05/01 09:21

上一篇文章Path动画--书写文字的动画实现介绍了一些笔迹动画的一个思路:记录笔尖经过的每一个点坐标然后那这些点重新拼接成path一步一步绘制,达到一个让手机自动绘制出我们想要的图形效果,这个是把路径的信息放在内存中了现在做一个持久层的实现。

先看下效果,上一把我们是手指画出字来再让手机画的,现在这个是手机自动绘制出我们的文字,当然了这个绘制文字的资源我们要事先准备好。


说说原理:这里涉及到Android的数据存储,常用的5种方法我就不赘述了,这里选择最简单的保存到文件。

先看下一个关键的工具类:

/** * Created by Wood on 2015/2/8. */public class PathHelper {    public static void writePath( ArrayList<ArrayList<PathPoint>> path,String filaName){        OutputStream out= null;        try {            out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(),filaName));            ObjectOutputStream oos = new ObjectOutputStream(out);            oos.writeObject(path);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            try {                out.close();            } catch (IOException e) {                e.printStackTrace();            }        }        Log.i("info","保存成功");    }    public static ArrayList<ArrayList<PathPoint>> read(int rawId,Context context){        InputStream is = context.getResources().openRawResource(rawId);        ArrayList<ArrayList<PathPoint>> lines=null;        try {            ObjectInputStream ois = new ObjectInputStream(is);          lines = (ArrayList<ArrayList<PathPoint>>) ois.readObject();        } catch (IOException e) {            e.printStackTrace();        } catch (ClassNotFoundException e) {            e.printStackTrace();        }finally {            try {                is.close();            } catch (IOException e) {                e.printStackTrace();            }        }        Log.i("info","读取成功");        return lines;    }}

使用了对象输入输出流,注意我们自己封装的PathPoint需要实现Serializable并且不能是个内部类。

至于绘制path的方法参看上一把那个文章。保存得到的文件放到工程的raw资源目录,通过这个工具类将其输入内存,绘制。

仅当一个Android玩法介绍,如果启发了你的某些思路,请给我一个yes。


1 0
原创粉丝点击