基于Android平台的手机电子书阅读器代码汇总.docx
《基于Android平台的手机电子书阅读器代码汇总.docx》由会员分享,可在线阅读,更多相关《基于Android平台的手机电子书阅读器代码汇总.docx(46页珍藏版)》请在冰点文库上搜索。
基于Android平台的手机电子书阅读器代码汇总
基于Android平台的手机电子书阅读器代码
packagecom.lrc.view;
/**
*书籍阅读
*/
importjava.io.IOException;
importjava.lang.reflect.Array;
importjava.math.BigDecimal;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.Vector;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
importcom.lrc.R;
importcom.lrc.bll.BookPageFactory;
importcom.lrc.bll.DbDataOperation;
importcom.lrc.bll.DbTags;
importcom.lrc.bll.PageWidget;
importcom.lrc.util.ArrayUtil;
importcom.lrc.util.BgUtil;
importcom.lrc.util.MathUtil;
importcom.lrc.util.TimeUtil;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.content.ContentResolver;
importandroid.content.ContentValues;
importandroid.content.DialogInterface;
importandroid.content.Intent;
importandroid.graphics.Bitmap;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
import.Uri;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.provider.Settings;
importandroid.util.DisplayMetrics;
importandroid.util.Log;
importandroid.view.Gravity;
importandroid.view.KeyEvent;
importandroid.view.Menu;
importandroid.view.MotionEvent;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.view.View.OnTouchListener;
importandroid.view.ViewGroup.LayoutParams;
importandroid.view.Window;
importandroid.view.WindowManager;
importandroid.widget.AdapterView;
importandroid.widget.AdapterView.OnItemClickListener;
importandroid.widget.EditText;
importandroid.widget.GridView;
importandroid.widget.ImageButton;
importandroid.widget.LinearLayout;
importandroid.widget.PopupWindow;
importandroid.widget.ProgressBar;
importandroid.widget.SeekBar;
importandroid.widget.SimpleAdapter;
importandroid.widget.TextView;
importandroid.widget.SeekBar.OnSeekBarChangeListener;
importandroid.widget.Toast;
publicclassBookReadActivityextendsActivity
{
privatePageWidgetmPageWidget;
BitmapmCurPageBitmap,mNextPageBitmap;
CanvasmCurPageCanvas,mNextPageCanvas;
publicstaticBookPageFactorypagefactory;
intscreenWidth,screenHeight;
intbookId;
StringbookPath,bookName,bookProgress;
privateintbookBeginPosition;
//db
privateContentResolverresolver;
//tabMenu
publicfinalstaticintTITLE_USING_OFTEN=0;
publicfinalstaticintTITLE_TOOLS=1;
publicfinalstaticintMENU_FONT_SIZE=0;
publicfinalstaticintMENU_READ_THEME=1;
publicfinalstaticintMENU_BRIGHTNESS_CONTROL=2;
publicfinalstaticintMENU_FLIP_OVER=3;
publicfinalstaticintMENU_JUMP=4;
publicfinalstaticintMENU_SEARCH=5;
publicfinalstaticintMENU_PRE_CHAPTER=6;
publicfinalstaticintMENU_NEXT_CHAPTER=7;
publicfinalstaticintMENU_CUSTOM_THEME=0;
publicfinalstaticintMENU_AUDIO_BOOK=1;
publicfinalstaticintMENU_BOOK_CHAPTER=2;
publicfinalstaticintMENU_BOOK_CODE=3;
publicfinalstaticintMENU_BOOK_MARK=4;
publicfinalstaticintMENU_SETTING=5;
publicfinalstaticintMENU_SHARE=6;
publicfinalstaticintMENU_BACK=7;
TabMenu.MenuBodyAdapter[]bodyAdapter=newTabMenu.MenuBodyAdapter[2];
TabMenu.MenuTitleAdaptertitleAdapter;
TabMenutabMenu;
intselTitle=0;
PopupWindowpopupWindow;
//定义每项分页栏的内容
String[]title1BodyName,title2BodyName;
int[]title1BodyIcon,title2BodyIcon;
privateSeekBarsbFontSize,sbBrightnessControl,sbJump;
//menujump
privateTextViewtvCurrentProgress;
privateImageButtonibtnJumpUp,ibtnJumpDown;
//flipover
privateHandlerreadHandler=newHandler();
privateFlipOverRunnableflipOverRunnable=newFlipOverRunnable();
privatebooleanisRead=true;
//menucustomtheme
privateSeekBarredForegroundSeekBar,greenForegroundSeekBar,blueForegroundSeekBar,
redBackgroundSeekBar,greenBackgroundSeekBar,blueBackgroundSeekBar;
intcurrentProgress;
privateTextViewfontColorTxt,backgroundColorTxt,themeStyleTxt;
privateintredForegoundProgress,greenForegroundProgress,blueForegroundProgress,
redBackgoundProgress,greenBackgroundProgress,blueBackgroundProgress;
privateStringforeColor,foreR="00",foreG="00",foreB="00",backColor,backR="00",backG="00",backB="00";
privateintcurrentForeColorId,currentBackColorId;
//chapter
Patternpattern=Ppile("第{1}.+章{1}.+");
Matchermatcher;
ListchapterName=newArrayList();
ListchapterBeginPositions=newArrayList();
Vectorlines;
intchapterNum=0;
privateAlertDialogchapterDialog;
privateStringcurrentChapterName="wu";
privateintcurrentBeginPosition=0;
HandlerchapterHandler=newHandler()
{
@Override
publicvoidhandleMessage(Messagemsg)
{
super.handleMessage(msg);
if(msg.what==0)
{
//updateView();
showChapterDialog();
}
elseif(msg.what==1)
{
chapterDialog.setMessage("正在提取章节,请稍后...\n已发现章节:
["+chapterNum+"]"+currentChapterName);
}
elseif(msg.what==2)
{
mPageWidget.invalidate();
}
}
};
//preandnextchapter
privateStringcurrentChapter;
privateintcurrentChapterIndex;
privatebooleanpreOrNext=true;
HandlerpreHandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg)
{
super.handleMessage(msg);
for(inti=0;i{
if(chapterName.get(i).equals(currentChapter))
{
currentChapterIndex=i;
Log.i("chapter"+i+"-->",currentChapterIndex+"");
break;
}
}
if(currentChapterIndex==0)
{
newAlertDialog.Builder(BookReadActivity.this).setTitle("提示").setMessage("已是第一章!
").setPositiveButton("确定",null).show();
}
else
{
pagefactory.setM_mbBufBegin(chapterBeginPositions.get(currentChapterIndex-1));
updateView();
}
}
};
HandlernextHandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg)
{
super.handleMessage(msg);
for(inti=0;i{
Log.i("name"+i,chapterName.get(i));
if(chapterName.get(i).equals(currentChapter))
{
currentChapterIndex=i;
Log.i("chapter"+i+"-->",currentChapterIndex+"");
break;
}
}
if(currentChapterIndex==(chapterName.size()-1))
{
newAlertDialog.Builder(BookReadActivity.this).setTitle("提示").setMessage("已是最后一章!
").setPositiveButton("确定",null).show();
}
else
{
pagefactory.setM_mbBufBegin(chapterBeginPositions.get(currentChapterIndex+1));
updateView();
}
}
};
@Override
publicvoidonCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
resolver=getContentResolver();
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
mPageWidget=newPageWidget(this);
setContentView(mPageWidget);
DisplayMetricsdm=newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth=dm.widthPixels;
screenHeight=dm.heightPixels;
mCurPageBitmap=Bitmap.createBitmap(screenWidth,screenHeight,Bitmap.Config.ARGB_8888);
mNextPageBitmap=Bitmap
.createBitmap(screenWidth,screenHeight,Bitmap.Config.ARGB_8888);
mCurPageCanvas=newCanvas(mCurPageBitmap);
mNextPageCanvas=newCanvas(mNextPageBitmap);
pagefactory=newBookPageFactory(screenWidth,screenHeight);
pagefactory.setBgBitmap(BgUtil.getSuitableBg(this,screenWidth,screenHeight,R.drawable.read_bg_3));
try
{
bookPath=getIntent().getExtras().getString(DbTags.FIELD_BOOK_PATH);
bookName=getIntent().getExtras().getString(DbTags.FIELD_BOOK_NAME);
bookId=getIntent().getExtras().getInt(DbTags.FIELD_BOOK_ID);
bookProgress=getIntent().getExtras().getString(DbTags.FIELD_BOOK_PROGRESS);
bookBeginPosition=getIntent().getExtras().getInt(DbTags.FIELD_BOOK_BEGIN_POSITION);
pagefactory.openbook(bookPath);
if(bookProgress==null||bookProgress.equals("onlyRead"))
{
pagefactory.onDraw(mCurPageCanvas);
System.out.println("1");
}
elseif(bookProgress.equals("begin"))
{
pagefactory.setM_mbBufBegin(getIntent().getIntExtra("begin",0));
updateView();
System.out.println("2");
}
else
{
pagefactory.setM_mbBufBegin(bookBeginPosition);
System.out.println("3");
updateView();
}
}
catch(IOExceptione1)
{
e1.printStackTrace();
Toast.makeText(this,"电子书不存在!
!
!
",
Toast.LENGTH_SHORT).show();
}
mPageWidget.setBitmaps(mCurPageBitmap,mCurPageBitmap);
mPageWidget.setOnTouchListener(newOnTouchListener()
{
publicbooleanonTouch(Viewv,MotionEvente)
{
booleanret=false;
if(popupWindow==null||!
popupWindow.isShowing())
{
if(v==mPageWidget)
{
if(e.getAction()==MotionEvent.ACTION_DOWN){
mPageWidget.abortAnimation();
mPageWidget.calcCornerXY(e.getX(),e.getY());
pagefactory.onDraw(mCurPageCanvas);
if(mPageWidget.DragToRight())
{
try
{
pagefactory.prePage();
}
catch(IOExceptione1)
{
e1.printStackTrace();
}
if(pagefactory.isfirstPage())
{
//Toast.makeText(BookReadActivity.this,"已到第一页!
",200).show();
returnfalse;
}
pagefactory.onDraw(mNextPageCanvas);
}else
{
try
{
pagefactory.nextPage();
}
catch(IOExceptione1)
{
e1.printStackTrace();
}
if(pagefactory.islastPage())
{
//Toast.makeText(BookReadActivity.this,"已到最后一页!
",200).show();
re