public void handleMessage(Message msg) { switch (msg.what) { case TEXTSET: pagefactory.changBackGround(msg.arg1); pagefactory.onDraw(mCurPageCanvas); mPageWidget.postInvalidate(); break; case OPENMARK: try { mCursor = db.select(); } catch (Exception e) { e.printStackTrace(); } if (mCursor.getCount() > 0) { mCursor.moveToPosition(mCursor.getCount() - 1); String pos = mCursor.getString(2); mCursor.getString(1); pagefactory.setBeginPos(Integer.valueOf(pos)); try { pagefactory.prePage(); } catch (IOException e) { e.printStackTrace(); } pagefactory.onDraw(mNextPageCanvas); mPageWidget.setBitmaps(mCurPageBitmap, mNextPageBitmap); mPageWidget.invalidate(); db.close(); } break; case SAVEMARK: try { db.update(book.id, book.bookname, String.valueOf(msg.arg2)); db.updateSetup(setup.id, String.valueOf(msg.arg1), "0", "0"); // mCursor = db.select(); } catch (Exception e) { e.printStackTrace(); } // System.out.println(mCursor.getCount()); // if (mCursor.getCount() > 0) { // mCursor.moveToPosition(mCursor.getCount()-1); // db.update(book.id, book.bookname, // String.valueOf(msg.arg2),String.valueOf(msg.arg1)); // } else { // db.insert("", String.valueOf(msg.arg2),String.valueOf(msg.arg1)); // } db.close(); break; default: break; } super.handleMessage(msg); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow() .setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Display用于提供屏幕尺寸和分辨率的信息 Display display = getWindowManager().getDefaultDisplay(); int w = display.getWidth(); int h = display.getHeight(); System.out.println(w + "\t" + h); mCurPageBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // 当前页位图 mNextPageBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); // 下一页位图 mCurPageCanvas = new Canvas(mCurPageBitmap); // 显示当前页位图 mNextPageCanvas = new Canvas(mNextPageBitmap); // 显示下一页位图 pagefactory = new BookPageFactory(w, h); pagefactory.setBgBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bg)); // 取得传递的参数 Intent intent = getIntent(); String bookid = intent.getStringExtra("bookid"); mContext = this; db = new DbHelper(mContext); try { book = db.getBookInfo(Integer.parseInt(bookid)); setup = db.getSetupInfo(); } catch (Exception e) { e.printStackTrace(); } if (book != null) { pagefactory.setFileName(book.bookname); mPageWidget = new PageWidget(this, w, h); setContentView(mPageWidget); pagefactory.openbook(mBookPath + book.bookname); if (book.bookmark > 0) { whichSize = setup.fontsize; pagefactory.setFontSize(Integer.parseInt(font[setup.fontsize])); pagefactory.setBeginPos(Integer.valueOf(book.bookmark)); try { pagefactory.prePage(); } catch (IOException e) { e.printStackTrace(); } // setContentView(mPageWidget); pagefactory.onDraw(mNextPageCanvas); mPageWidget.setBitmaps(mNextPageBitmap, mNextPageBitmap); // mPageWidget.invalidate(); mPageWidget.postInvalidate(); db.close(); } else { pagefactory.onDraw(mCurPageCanvas); // setContentView(mPageWidget); mPageWidget.setBitmaps(mCurPageBitmap, mCurPageBitmap); } mPageWidget.setOnTouchListener( new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent e) { boolean ret = false; if (v == mPageWidget) { if (e.getAction() == MotionEvent.ACTION_DOWN) { mPageWidget.abortAnimation(); mPageWidget.calcCornerXY(e.getX(), e.getY()); if (mPageWidget.isMiddle()) { openOptionsMenu(); return true; } pagefactory.onDraw(mCurPageCanvas); if (mPageWidget.DragToRight()) { try { pagefactory.prePage(); } catch (IOException e1) { e1.printStackTrace(); } if (pagefactory.isfirstPage()) { Toast.makeText(mContext, "已经是第一页", Toast.LENGTH_SHORT).show(); return false; } pagefactory.onDraw(mNextPageCanvas); } else { try { pagefactory.nextPage(); } catch (IOException e1) { e1.printStackTrace(); } if (pagefactory.islastPage()) { Toast.makeText(mContext, "已经是最后一页", Toast.LENGTH_SHORT).show(); return false; } pagefactory.onDraw(mNextPageCanvas); } mPageWidget.setBitmaps(mCurPageBitmap, mNextPageBitmap); } if (mPageWidget.isMiddle()) return true; ret = mPageWidget.doTouchEvent(e); return ret; } return false; } }); } else { Toast.makeText(mContext, "电子书不存在!可能已经删除", Toast.LENGTH_SHORT).show(); BookActivity.this.finish(); } }