该功能主要实现的有选择阅读模式(白天模式,夜晚模式,图片模式)和选择阅读进度,改变了退出方式。
public class ReadBook extends BaseActivity {
private BookManager bookManager;
private Book book;
private BookPageView mBookPageView;
private Bitmap mCurPageBitmap, mNextPageBitmap;
private Canvas mCurPageCanvas, mNextPageCanvas;
private BookPageFactory pagefactory;
long fileLenth = 1L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
mBookPageView = new BookPageView(this, width, height);
setContentView(mBookPageView);
mCurPageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mNextPageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCurPageCanvas = new Canvas(mCurPageBitmap);
mNextPageCanvas = new Canvas(mNextPageBitmap);
pagefactory = new BookPageFactory(this, width, height);
try {
bookManager = new BookManager(this);
Long bookId = getIntent().getLongExtra("bookId", -1);
if (bookId == -1) {
ToastUtils.toast(this, "查询的id不存在");
startActivity(new Intent(this, MyBook.class));
return;
}
book = bookManager.get(bookId);
// 读取图书
fileLenth = pagefactory.openBook(book);
pagefactory.onDraw(mCurPageCanvas);
} catch (IOException e1) {
ToastUtils.toast(this, "查询的电子书不存在");
Log.e(TAG, e1.getMessage());
}
mBookPageView.setBitmaps(mCurPageBitmap, mCurPageBitmap);
mBookPageView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
boolean ret = false;
if (v == mBookPageView) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
mBookPageView.abortAnimation();
mBookPageView.calcCornerXY(e.getX(), e.getY());
pagefactory.onDraw(mCurPageCanvas);
if (mBookPageView.DragToRight()) {
pagefactory.prePage();// 上一页
if (pagefactory.isfirstPage()) {
ToastUtils.toast(ReadBook.this, "首页");
return false;
}
pagefactory.onDraw(mNextPageCanvas);
} else {
pagefactory.nextPage();// 下一页
if (pagefactory.islastPage()) {
ToastUtils.toast(ReadBook.this, "末页");
return false;
}
pagefactory.onDraw(mNextPageCanvas);
}
mBookPageView.setBitmaps(mCurPageBitmap, mNextPageBitmap);
}
ret = mBookPageView.doTouchEvent(e);
return ret;
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.read_book, menu);
return super.onCreateOptionsMenu(menu);
}
// 处理选项菜单的点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:// 白天模式
break;
case R.id.menu2:// 夜晚模式
break;
case R.id.menu3:// 图片模式
pagefactory.setBgBitmap(BitmapFactory.decodeResource(this.getResources(), R.drawable.bg2));
case R.id.menu4:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout myLayout = (LinearLayout) layoutInflater.inflate(R.layout.read_book_progress, null);
prog_int = (EditText) myLayout.findViewById(R.id.prog_int);
prog_float = (EditText) myLayout.findViewById(R.id.prog_float);
seekBar = (SeekBar) myLayout.findViewById(R.id.seekBar);
// 添加事件监听
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
prog_int.setText(progress + "");
if (progress == 100) {
prog_float.setText("0");
} else {
double putvalue = progress * 0.01;
DecimalFormat df = new DecimalFormat("#0.00");
double fp = new Double(df.format(putvalue));
// 小数点后两位前移,并四舍五入
int j = (int) Math.round(fp * 100);
prog_float.setText(j + "");
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
String string = book.getProgress();
Log.d(TAG, "a" + string + "a");
String begin = string.substring(0, string.indexOf('.'));
String end = string.substring(string.indexOf('.') + 1, string.length() - 1);
Log.d(TAG, begin + end);
seekBar.setProgress(Integer.parseInt(begin));
prog_int.setText(begin);
prog_float.setText(end);
AlterD = new AlertDialog.Builder(this);
AlterD.setTitle("进度跳转");
AlterD.setIcon(R.drawable.mb);
// AlterD.setMessage("测试View加入进度条");
AlterD.setView(myLayout);
AlterD.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
DecimalFormat df = new DecimalFormat("#0.00");
Log.d(TAG, "prog_int:" + prog_int.getText().toString() + ",prog_float:" + prog_float.getText().toString());
String string = prog_int.getText().toString() + "." + prog_float.getText().toString();
double d = Double.parseDouble(string);
double fp = new Double(df.format(d / 100));
int begin = (int) (fp * fileLenth);
Log.d(TAG, "begin:" + begin);
Log.d(TAG, "fileLenth:" + fileLenth);
pagefactory.setM_mbBufEnd(begin);
pagefactory.onDraw(mCurPageCanvas);
}
});
AlterD.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlterD.show();
break;
}
return super.onOptionsItemSelected(item);
}
private AlertDialog.Builder AlterD;
SeekBar seekBar;
EditText prog_int, prog_float;
// 改变阅读页面的返回事件
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
startActivity(new Intent(this, MyBook.class));
ReadBook.this.finish();
return false;
}
return false;
}
}