首先说明,阅读器 翻页效果是下载别人源码的。 我仅仅是增加一些功能;
http://blog.csdn.net/hmg25/article/details/6419694
自定义View那个类,说实话我没咋看(可以说没看~)。那啥曲线原理,也不懂。。嘿嘿;
BookPageFactory 只是看这个工具类
书签是以xml格式存放, 代码中生存xml文件,及读取;
书架是使用gridView 实现,效果还不是很好,至少有个样子(也是网上找的...以及eoe里一个哥们提供的,(因为我下载的源码 gridView属性没设置好,所以不好看。))
使用这个源码。 首先的在SD卡下面 放一个test.txt文件。大家随便下个小说改个名字就是;
MainReader.java
package com.zeng.reader; import static com.zeng.reader.MarkManager.single; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlSerializer; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class MainReader extends Activity { /** Called when the activity is first created. */ private PageWidget mPageWidget; Bitmap mCurPageBitmap, mNextPageBitmap; Canvas mCurPageCanvas, mNextPageCanvas; BookPageFactory pagefactory; // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); mPageWidget = new PageWidget(this); setContentView(mPageWidget); // 若是要修改分辨率的话, 请自己手动该 480 800 两个值。 mCurPageBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888); mNextPageBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888); // // 两画布 mCurPageCanvas = new Canvas(mCurPageBitmap); mNextPageCanvas = new Canvas(mNextPageBitmap); pagefactory = new BookPageFactory(480, 800); // 设置一张背景图片 pagefactory.setBgBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.bg)); // try { pagefactory.openbook(bookfillPath);// 打开文件 获取到一个缓存 pagefactory.onDraw(mCurPageCanvas);// } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); Toast.makeText(this, "电子书不存在,请将《test.txt》放在SD卡根目录下", Toast.LENGTH_SHORT).show(); } mPageWidget.setBitmaps(mCurPageBitmap, mCurPageBitmap); mPageWidget.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent e) { // TODO Auto-generated method stub boolean ret = false; if (v == mPageWidget) { if (e.getAction() == MotionEvent.ACTION_DOWN) { mPageWidget.abortAnimation(); mPageWidget.calcCornerXY(e.getX(), e.getY()); pagefactory.onDraw(mCurPageCanvas); if (mPageWidget.DragToRight()) {// 右边点击的时候为false; 前一页 try { pagefactory.prePage(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (pagefactory.isfirstPage()) return false; pagefactory.onDraw(mNextPageCanvas); } else { try { pagefactory.nextPage(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (pagefactory.islastPage()) return false; pagefactory.onDraw(mNextPageCanvas); } mPageWidget.setBitmaps(mCurPageBitmap, mNextPageBitmap); } ret = mPageWidget.doTouchEvent(e); return ret; } return false; } }); } // 菜单 @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "添加书签"); menu.add(0, 1, 0, "查看书签"); menu.add(0, 2, 0, "字体放大"); menu.add(0, 3, 0, "字体缩小"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case 0: addTag(); break; case 1: checkTag(); break; case 2: // 这种方式调字体,不太好; int size = pagefactory.getM_fontSize(); int newsize = size + 2; pagefactory.setM_fontSize(newsize); // int begin = pagefactory.getM_mbBufBegin(); // pagefactory.setM_mbBufEnd(begin); try { pagefactory.nextPage(); pagefactory.onDraw(mCurPageCanvas); pagefactory.onDraw(mNextPageCanvas); mPageWidget.invalidate(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // pagefactory.clearM_lines(); // pagefactory.onDraw(mCurPageCanvas);// // pagefactory.onDraw(mNextPageCanvas);// // mPageWidget.invalidate(); break; case 3: int size1 = pagefactory.getM_fontSize(); int newsize1 = size1 - 2; pagefactory.setM_fontSize(newsize1); int begin1 = pagefactory.getM_mbBufBegin(); // pagefactory.setM_mbBufEnd(begin1); pagefactory.clearM_lines();// pagefactory.onDraw(mCurPageCanvas);// pagefactory.onDraw(mNextPageCanvas);// mPageWidget.invalidate(); break; } return super.onOptionsItemSelected(item); } //添加书签 private void addTag() { // 获取3个值 int begin = pagefactory.getM_mbBufBegin(); int end = pagefactory.getM_mbBufEnd(); String firstLine = ((pagefactory.getM_lines()).elementAt(0)).toString(); String percent = pagefactory.getStrPercent();// String currentTime = sdf.format(new Date()); // 创建一个书签 Items item = new Items(begin, end, firstLine, percent, currentTime); int isExist = single.isExist(bookName); if (isExist >= 0) { single.get(isExist).list.add(item); producXML(); Toast.makeText(MainReader.this, "添加书签成功", Toast.LENGTH_SHORT).show(); return; } BookMark bm = new BookMark(bookName, item); single.add(bm); producXML(); Toast.makeText(MainReader.this, "添加书签成功", Toast.LENGTH_SHORT).show(); } private void checkTag() { try { XmlParser.parseXml(bookMarkPath); } catch (XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } final AlertDialog adb = new AlertDialog.Builder(this).create(); adb.setIcon(R.drawable.face248); adb.setTitle("书签"); getData(); LayoutInflater inflater = getLayoutInflater().from(this); View view = inflater.inflate(R.layout.listview, null); final ListView listView = (ListView) view.findViewById(R.id.listview); final TextView textView = (TextView) view.findViewById(R.id.listView_text); if (data.isEmpty()) { textView.setVisibility(View.VISIBLE); listView.setVisibility(View.GONE); adb.setView(view); adb.show(); return; } adb.setView(view); adapter = new SimpleAdapter(MainReader.this, data, R.layout.list_item, new String[] { "content", "percent", "time" }, new int[] { R.id.textView1, R.id.textView2, R.id.textView3 }); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub int pos = single.isExist(bookName); BookMark bm = single.get(pos); Items item = bm.getList().get(position); // 这里我加一个注释, onDraw方法中 当m_lines是空的时候 ,执行pageDown() 该方法是以结尾点的 // 字节点为起点去下载下一页,(好像是的...),所以设置的时候开始点,结尾点,都设置为 开始点 pagefactory.setM_mbBufEnd((int) item.getBegin()); pagefactory.setM_mbBufBegin((int) item.getBegin()); pagefactory.clearM_lines(); pagefactory.onDraw(mCurPageCanvas);// pagefactory.onDraw(mNextPageCanvas);// adb.dismiss(); mPageWidget.invalidate(); } }); listView.setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { int pos = single.isExist(bookName); final BookMark bm = single.get(pos); Items item = bm.getList().get(position); final int position1 = position;// 下面使用 AlertDialog.Builder ad = new AlertDialog.Builder(MainReader.this); ad.setTitle("删除该书签").setMessage("确定删除?"); ad.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { bm.getList().remove(position1);// 删除该数据 // 再写文件。。 producXML(); getData(); adb.cancel(); if (data.isEmpty()) { textView.setVisibility(View.VISIBLE); listView.setVisibility(View.GONE); adb.show(); return; } adapter.notifyDataSetChanged(); adapter = new SimpleAdapter(MainReader.this, data, R.layout.list_item, new String[] { "content", "percent", "time" }, new int[] { R.id.textView1, R.id.textView2, R.id.textView3 }); listView.setAdapter(adapter); adb.show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).show(); return false; } }); adb.show(); } private SimpleAdapter adapter; ArrayList<Map<String, Object>> data; private void getData() { data = new ArrayList<Map<String, Object>>(); if (single.count() == 0) return; int pos = single.isExist(bookName); BookMark bm = single.get(pos); for (int i = 0; i < bm.getList().size(); i++) { Map map = new HashMap<String, Object>(); map.put("percent", bm.getList().get(i).getPercent()); map.put("time", bm.getList().get(i).getTime()); map.put("content", bm.getList().get(i).getContent()); data.add(map); } } // private String bookName = "test"; private long begin_pos; private long end_pos; private String content;// 只取第一行; private String bookfillPath = "/sdcard/test.txt"; private String bookMarkPath = "/sdcard/BookMark/bookmark.xml"; /** * @return 该方法是否要返回值 */ private boolean producXML() { // // try { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); XmlSerializer xmlSerializer = factory.newSerializer(); // // xmlSerializer.setOutput(stringWriter); String path = "/sdcard/BookMark/"; File file = new File(path); // file.mkdirs(); // if(!file.isDirectory()){ if (!file.exists()) { file.mkdirs(); } File f = new File(path + "bookmark.xml"); if (!f.exists()) { f = new File(path + "bookmark.xml"); } FileOutputStream fos = new FileOutputStream(f); xmlSerializer.setOutput(fos, "utf-8"); xmlSerializer.startDocument("utf-8", true); xmlSerializer.startTag(null, "books"); if (single.count() != 0) for (BookMark mark : single.getList()) { xmlSerializer.startTag(null, "book"); // xmlSerializer.startTag(null, "book"+" name="+bookName); xmlSerializer.attribute(null, "name", mark.getBookName()); for (Items item : mark.getList()) { xmlSerializer.startTag(null, "items"); xmlSerializer.attribute(null, "begin", item.getBegin() + ""); xmlSerializer.attribute(null, "end", item.getEnd() + ""); xmlSerializer.attribute(null, "content", item.getContent()); xmlSerializer.attribute(null, "percent", item.getPercent()); xmlSerializer.attribute(null, "time", item.getTime()); xmlSerializer.endTag(null, "items"); } xmlSerializer.endTag(null, "book"); } xmlSerializer.endTag(null, "books"); xmlSerializer.endDocument(); // xmlSerializer.flush(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } return true; } }
BookPageFactory.java
package com.zeng.reader; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.text.DecimalFormat; import java.util.Vector; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Align; public class BookPageFactory { private File book_file = null; private MappedByteBuffer m_mbBuf = null; /** 字节总长度 */ private int m_mbBufLen = 0; /** 字节开始位置 */ private int m_mbBufBegin = 0; /** 字节结束位置 */ private int m_mbBufEnd = 0; private String m_strCharsetName = "gbk"; private Bitmap m_book_bg = null; private int mWidth; private int mHeight; /**Vector<String> m_lines String型数组*/ private Vector<String> m_lines = new Vector<String>(); private int m_fontSize = 24; private int m_textColor = Color.BLACK; private int m_backColor = 0xffff9e85; // 背景颜色 private int marginWidth = 15; // 左右与边缘的距离 private int marginHeight = 20; // 上下与边缘的距离 /**每页可以显示的行数*/ private int mLineCount; // 每页可以显示的行数 private float mVisibleHeight; // 绘制内容的宽 private float mVisibleWidth; // 绘制内容的宽 private boolean m_isfirstPage,m_islastPage; // private int m_nLineSpaceing = 5; private Paint mPaint; private Paint percentPaint; private String strPercent; public BookPageFactory(int w, int h) { // TODO Auto-generated constructor stub mWidth = w; mHeight = h; // percentPaint.设置不变样式 ;写百分比专用 percentPaint = new Paint(Paint.ANTI_ALIAS_FLAG); percentPaint.setTextAlign(Align.LEFT); percentPaint.setTextSize(24);//字体大小 24 percentPaint.setColor(Color.BLACK);//黑体 // 下面是几个设置paint的。 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setTextAlign(Align.LEFT); mPaint.setTextSize(m_fontSize);//字体大小 24 mPaint.setColor(m_textColor);//黑体 mVisibleWidth = mWidth - marginWidth * 2;// 绘制内容的宽 上下左右的边缘都空一定的距离 所以*2 mVisibleHeight = mHeight - marginHeight * 2;// 绘制内容的搞 mLineCount = (int) (mVisibleHeight / m_fontSize); // 可显示的行数 / 可显示的高度除于每个字体的高度 } public void openbook(String strFilePath) throws IOException { book_file = new File(strFilePath); long lLen = book_file.length();//算是字节长度,有多少个字节 m_mbBufLen = (int) lLen;//一样的 //MappedByteBuffer 缓存 m_mbBuf = new RandomAccessFile(book_file, "r").getChannel().map( FileChannel.MapMode.READ_ONLY, 0, lLen); } //读取前一断落 protected byte[] readParagraphBack(int nFromPos) { int nEnd = nFromPos; int i; byte b0, b1; if (m_strCharsetName.equals("UTF-16LE")) { i = nEnd - 2; while (i > 0) { b0 = m_mbBuf.get(i); b1 = m_mbBuf.get(i + 1); if (b0 == 0x0a && b1 == 0x00 && i != nEnd - 2) { i += 2; break; } i--; } } else if (m_strCharsetName.equals("UTF-16BE")) { i = nEnd - 2; while (i > 0) { b0 = m_mbBuf.get(i); b1 = m_mbBuf.get(i + 1); if (b0 == 0x00 && b1 == 0x0a && i != nEnd - 2) { i += 2; break; } i--; } } else { i = nEnd - 1;// 之前的结束点, 往回获取 while (i > 0) { b0 = m_mbBuf.get(i); if (b0 == 0x0a && i != nEnd - 1) { i++; break; } i--; } } if (i < 0) i = 0; int nParaSize = nEnd - i; int j; byte[] buf = new byte[nParaSize]; for (j = 0; j < nParaSize; j++) { buf[j] = m_mbBuf.get(i + j); } return buf; } // 读取上一段落 protected byte[] readParagraphForward(int nFromPos) { int nStart = nFromPos; int i = nStart; byte b0, b1; // 根据编码格式判断换行 if (m_strCharsetName.equals("UTF-16LE")) { while (i < m_mbBufLen - 1) { b0 = m_mbBuf.get(i++); b1 = m_mbBuf.get(i++); if (b0 == 0x0a && b1 == 0x00) { break; } } } else if (m_strCharsetName.equals("UTF-16BE")) { while (i < m_mbBufLen - 1) { b0 = m_mbBuf.get(i++); b1 = m_mbBuf.get(i++); if (b0 == 0x00 && b1 == 0x0a) { break; } } } else { while (i < m_mbBufLen) { b0 = m_mbBuf.get(i++); if (b0 == 0x0a) { // \r\n? break; } } } int nParaSize = i - nStart; byte[] buf = new byte[nParaSize]; for (i = 0; i < nParaSize; i++) { buf[i] = m_mbBuf.get(nFromPos + i); } return buf; } protected Vector<String> pageDown() { String strParagraph = ""; Vector<String> lines = new Vector<String>(); while (lines.size() < mLineCount && m_mbBufEnd < m_mbBufLen) { byte[] paraBuf = readParagraphForward(m_mbBufEnd); // 读取一个段落 m_mbBufEnd += paraBuf.length; try { strParagraph = new String(paraBuf, m_strCharsetName); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } String strReturn = ""; if (strParagraph.indexOf("\r\n") != -1) { strReturn = "\r\n"; strParagraph = strParagraph.replaceAll("\r\n", ""); } else if (strParagraph.indexOf("\n") != -1) { strReturn = "\n"; strParagraph = strParagraph.replaceAll("\n", ""); } if (strParagraph.length() == 0) { lines.add(strParagraph); } while (strParagraph.length() > 0) { float str_pixel = mPaint.measureText(strParagraph); System.out.println(str_pixel); int nSize = mPaint.breakText(strParagraph, true, mVisibleWidth, null); lines.add(strParagraph.substring(0, nSize));// strParagraph = strParagraph.substring(nSize);// if (lines.size() >= mLineCount) { System.out.println(lines.size()); break; } } if (strParagraph.length() != 0) { try { m_mbBufEnd -= (strParagraph + strReturn).getBytes(m_strCharsetName).length; } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return lines;//返回下一页的内容 } //读取前一页的内容, 再把开始点=>结束点 protected void pageUp() { if (m_mbBufBegin < 0) m_mbBufBegin = 0; Vector<String> lines = new Vector<String>(); String strParagraph = ""; while (lines.size() < mLineCount && m_mbBufBegin > 0) { Vector<String> paraLines = new Vector<String>(); byte[] paraBuf = readParagraphBack(m_mbBufBegin); m_mbBufBegin -= paraBuf.length; try { strParagraph = new String(paraBuf, m_strCharsetName); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } strParagraph = strParagraph.replaceAll("\r\n", ""); strParagraph = strParagraph.replaceAll("\n", ""); if (strParagraph.length() == 0) { paraLines.add(strParagraph); } while (strParagraph.length() > 0) { int nSize = mPaint.breakText(strParagraph, true, mVisibleWidth, null); paraLines.add(strParagraph.substring(0, nSize)); strParagraph = strParagraph.substring(nSize); } lines.addAll(0, paraLines); } while (lines.size() > mLineCount) { try { m_mbBufBegin += lines.get(0).getBytes(m_strCharsetName).length; lines.remove(0); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } } m_mbBufEnd = m_mbBufBegin; return; } protected void prePage() throws IOException { if (m_mbBufBegin <= 0) { m_mbBufBegin = 0;//第一页 m_isfirstPage=true; return; }else m_isfirstPage=false; m_lines.clear();//清空 pageUp();// m_mbBufEnd = m_mbBufBegin; m_lines = pageDown(); } public void nextPage() throws IOException { if (m_mbBufEnd >= m_mbBufLen) { m_islastPage=true; return; }else m_islastPage=false; m_lines.clear(); m_mbBufBegin = m_mbBufEnd;//把之前的结尾地方 为下一个开始位置 m_lines = pageDown(); } public void onDraw(Canvas c) { if (m_lines.size() == 0) m_lines = pageDown();//初始化时为0 if (m_lines.size() > 0) { if (m_book_bg == null) c.drawColor(m_backColor); else c.drawBitmap(m_book_bg, 0, 0, null); int y = marginHeight; for (String strLine : m_lines) {//每读取一行就绘一行的文字起点是marginwidth,x,marginHeight+字体大小 y += m_fontSize; c.drawText(strLine, marginWidth, y, mPaint); } } float fPercent = (float) (m_mbBufBegin * 1.0 / m_mbBufLen); DecimalFormat df = new DecimalFormat("#0.0"); strPercent = df.format(fPercent * 100) + "%"; int nPercentWidth = (int) percentPaint.measureText("999.9%") + 1; c.drawText(strPercent, mWidth - nPercentWidth, mHeight - 5, percentPaint); } public void setBgBitmap(Bitmap BG) { m_book_bg = BG; } public boolean isfirstPage() { return m_isfirstPage; } public boolean islastPage() { return m_islastPage; } // 下面是书签 需要使用到的两个数据 public int getM_mbBufBegin() { return m_mbBufBegin; } public void setM_mbBufBegin(int m_mbBufBegin) { this.m_mbBufBegin = m_mbBufBegin; } public Vector<String> getM_lines() { return m_lines; } public void setM_lines(Vector<String> m_lines) { this.m_lines = m_lines; } public int getM_mbBufEnd() { return m_mbBufEnd; } public void setM_mbBufEnd(int m_mbBufEnd) { this.m_mbBufEnd = m_mbBufEnd; } public void clearM_lines(){ this.m_lines.clear(); } public int getM_fontSize() { return m_fontSize; } public void setM_fontSize(int m_fontSize) { this.m_fontSize = m_fontSize; mPaint.setTextSize(m_fontSize); mLineCount = (int) (mVisibleHeight / m_fontSize); // 可显示的行数 / 可显示的高度除于每个字体的高度 } public String getStrPercent() { return strPercent; } public void setStrPercent(String strPercent) { this.strPercent = strPercent; } }
MarkManager.java。 里面是几个内部类,主要是管理书签,和每个书签的属性
package com.zeng.reader; import java.util.ArrayList; import java.util.List; public class MarkManager { public static final MarkManager single = new MarkManager(); private List<BookMark> list = new ArrayList<BookMark>(); public MarkManager() { // TODO Auto-generated constructor stub } public void add(BookMark bm) { list.add(bm); } public BookMark get(int position) { return list.get(position); } public int count() { return list.size(); } public void set(int index, BookMark bm) { list.set(index, bm); } // 测试使用, public void removeAll() { list.clear(); } public List<BookMark> getList() { return list; } /** * * @param name * @return 存在相同名字的则 返回该位置,否则 -1; */ public int isExist(String name) { for (int i = 0; i < single.count(); i++) { BookMark bm = single.get(i); String str = bm.getBookName(); if (str.equals(name)) return i; } return -1; } } class BookMark { String bookName; List<Items> list; public BookMark() { // TODO Auto-generated constructor stub list = new ArrayList<Items>(); } public BookMark(String bookName, Items item) { list = new ArrayList<Items>(); this.bookName = bookName; list.add(item); } public BookMark(String bookName, List<Items> items) { // TODO Auto-generated constructor stub } public void setItem(Items item) { list.add(item); } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public List<Items> getList() { return list; } public void setList(List<Items> list) { this.list = list; } } class Items { private long begin; private long end; private String content; private String percent; private String time; public Items() { // TODO Auto-generated constructor stub } public Items(long begin, long end, String content) { // TODO Auto-generated constructor stub this.begin = begin; this.end = end; this.content = content; } public Items(long begin, long end, String content, String percent, String time) { // TODO Auto-generated constructor stub this.begin = begin; this.end = end; this.content = content; this.time = time; this.percent = percent; } public long getBegin() { return begin; } public void setBegin(long begin) { this.begin = begin; } public long getEnd() { return end; } public void setEnd(long end) { this.end = end; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getPercent() { return percent; } public void setPercent(String percent) { this.percent = percent; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } }
package com.zeng.reader; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import android.util.Log; import static com.zeng.reader.MarkManager.single; public class XmlParser { public static void parseXml(String filepath)throws XmlPullParserException, IOException { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser m_Parser = factory.newPullParser(); FileInputStream infile = new FileInputStream(filepath); BufferedReader reader = new BufferedReader(new InputStreamReader(infile)); m_Parser.setInput(reader); single.removeAll();// 或许要这一句? int eventType = 0; int count = 0;int index = -1; eventType = m_Parser.getEventType(); int columnNumber = m_Parser.getColumnNumber(); Log.i("abc", columnNumber+"") ; // BookMark bm = null; while(eventType != XmlPullParser.END_DOCUMENT){ if(eventType == XmlPullParser.START_DOCUMENT){ Log.i("abc", "Start doc"); }else if(eventType == XmlPullParser.START_TAG){ Log.i("abc", "start tag"+m_Parser.getName()); if(m_Parser.getName().equals("books") == true){ }else if(m_Parser.getName().equals("book") == true){ bm = new BookMark(); if(m_Parser.getAttributeName(0).equals("name") == true){ bm.setBookName(m_Parser.getAttributeValue(0)); } }else if(m_Parser.getName().equals("items") == true){ Items item = new Items(); count = m_Parser.getAttributeCount(); for(int i = 0;i <count;i++){ if(m_Parser.getAttributeName(i).equals("begin") == true){ item.setBegin(Long.valueOf(m_Parser.getAttributeValue(i))); }else if(m_Parser.getAttributeName(i).equals("end") == true){ item.setEnd(Long.valueOf(m_Parser.getAttributeValue(i))); }else if(m_Parser.getAttributeName(i).equals("content")== true){ item.setContent(m_Parser.getAttributeValue(i)); }else if(m_Parser.getAttributeName(i).equals("percent")){ item.setPercent(m_Parser.getAttributeValue(i)); }else if(m_Parser.getAttributeName(i).equals("time")){ item.setTime(m_Parser.getAttributeValue(i)); } } bm.setItem(item); int isExist = single.isExist(bm.getBookName()); if(isExist<0) single.add(bm); else single.set(isExist, bm); } }else if(eventType == XmlPullParser.END_TAG){ if(m_Parser.getName().equals("items")){ } }else if(eventType == XmlPullParser.TEXT){ } eventType = m_Parser.next(); } } }
书架的话,这个源码暂时没加上,
之后再弄把。
源码下载地址。 不需要分~~~~
http://download.csdn.net/detail/zgf1991/4345552