欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android书架效果实现原理与代码

程序员文章站 2023-12-05 16:37:46
以前也模仿者ireader实现了书架的效果,但是那种是使用listview实现的,并不好用。绝大多数都是用gridview实现的,网上这方面资料比较少,有些开源的电子书都是...
以前也模仿者ireader实现了书架的效果,但是那种是使用listview实现的,并不好用。绝大多数都是用gridview实现的,网上这方面资料比较少,有些开源的电子书都是重点做了阅读,并没有像ireader和qq阅读这样的书架效果。

书架这种效果我早就实现了,本来想做一个完美的电子书,但是因为自己的懒惰,仅仅持续了一两天,今天又找到了以前的代码分享出来,希望大家能一起实现一个比较完美的开源的电子书。废话不多说先看下效果:
android书架效果实现原理与代码 
本地部分还没有做,做好以后就可以吧本地的书加载到书架里了,这只是一个开始,后面还有很多复杂的没有做。
下面先看一下书架的实现原理吧!
首先看一下layout下的布局文件main.xml
复制代码 代码如下:

<?xmlversion="1.0"encoding="utf-8"?>
<relativelayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<includelayout="@layout/head"android:id="@+id/head"/>
<cn.com.karl.view.mygridview
android:id="@+id/bookshelf"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/head"
android:cachecolorhint="#00000000"
android:columnwidth="90.0dip"
android:fadingedge="none"
android:horizontalspacing="5dp"
android:listselector="#00000000"
android:numcolumns="3"
android:scrollbars="none"
android:verticalspacing="20dp"/>

<slidingdrawer
android:id="@+id/sliding"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:content="@+id/allapps"
android:handle="@+id/imageviewicon"
android:orientation="vertical">

<button
android:id="@+id/imageviewicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="本地"
android:textsize="18dp"
android:background="@drawable/btn_local"/>
<gridview
android:id="@+id/allapps"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/file_list_bg"
android:columnwidth="60dp"
android:gravity="center"
android:horizontalspacing="10dp"
android:numcolumns="auto_fit"
android:padding="10dp"
android:stretchmode="columnwidth"
android:verticalspacing="10dp"/>
</slidingdrawer>
</relativelayout>

上面是个自定义的gridview主要来实现书架,因为每一本书是一个item,在自定义的gridview中计算每一行的高度,然后把书架画上去。下面是个抽屉。
复制代码 代码如下:

publicclassmygridviewextendsgridview{
privatebitmapbackground;
publicmygridview(contextcontext,attributesetattrs){
super(context,attrs);
background=bitmapfactory.decoderesource(getresources(),
r.drawable.bookshelf_layer_center);
}
@override
protectedvoiddispatchdraw(canvascanvas){
intcount=getchildcount();
inttop=count>0?getchildat(0).gettop():0;
intbackgroundwidth=background.getwidth();
intbackgroundheight=background.getheight()+2;
intwidth=getwidth();
intheight=getheight();
for(inty=top;y<height;y+=backgroundheight){
for(intx=0;x<width;x+=backgroundwidth){
canvas.drawbitmap(background,x,y,null);
}
}
super.dispatchdraw(canvas);
}
}

上面就是自定义书架的gridview,也是实现书架最核心的方法。
然后是每一个item的布局:
复制代码 代码如下:

<?xmlversion="1.0"encoding="utf-8"?>
<linearlayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">
<textview
android:layout_height="110dp"
android:layout_width="90dp"
android:layout_margintop="10dp"
android:background="@drawable/cover_txt"
android:id="@+id/imageview1"
android:text="天龙八部"
android:padding="15dp"
android:textcolor="#000000"
/>
</linearlayout>

最后就可以在主activity中显示出来了。
复制代码 代码如下:

publicclassbookshelfactivityextendsbaseactivity{
privategridviewbookshelf;
privateint[]data={
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,
r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt,r.drawable.cover_txt

};
privatestring[]name={
"天龙八部","搜神记","水浒传","黑道悲情"
};
privategridviewgv;
privateslidingdrawersd;
privatebuttoniv;
privatelist<resolveinfo>apps;
/**calledwhentheactivityisfirstcreated.*/
@override
publicvoidoncreate(bundlesavedinstancestate){
super.oncreate(savedinstancestate);
this.requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.main);
bookshelf=(gridview)findviewbyid(r.id.bookshelf);
shlefadapteradapter=newshlefadapter();
bookshelf.setadapter(adapter);
bookshelf.setonitemclicklistener(newonitemclicklistener(){
@override
publicvoidonitemclick(adapterview<?>arg0,viewarg1,intarg2,
longarg3){
//todoauto-generatedmethodstub
if(arg2>=data.length){

}else{
toast.maketext(getapplicationcontext(),""+arg2,toast.length_short).show();
}
}
});
loadapps();
gv=(gridview)findviewbyid(r.id.allapps);
sd=(slidingdrawer)findviewbyid(r.id.sliding);
iv=(button)findviewbyid(r.id.imageviewicon);
gv.setadapter(newgridadapter());
sd.setondraweropenlistener(newslidingdrawer.ondraweropenlistener()//开抽屉
{
@override
publicvoidondraweropened(){
iv.settext("返回");
iv.setbackgroundresource(r.drawable.btn_local);//响应开抽屉事件
//,把图片设为向下的
}
});
sd.setondrawercloselistener(newslidingdrawer.ondrawercloselistener(){
@override
publicvoidondrawerclosed(){
iv.settext("本地");
iv.setbackgroundresource(r.drawable.btn_local);//响应关抽屉事件
}
});
}
classshlefadapterextendsbaseadapter{
@override
publicintgetcount(){
//todoauto-generatedmethodstub
returndata.length+5;
}
@override
publicobjectgetitem(intarg0){
//todoauto-generatedmethodstub
returnarg0;
}
@override
publiclonggetitemid(intarg0){
//todoauto-generatedmethodstub
returnarg0;
}
@override
publicviewgetview(intposition,viewcontentview,viewgrouparg2){
//todoauto-generatedmethodstub

contentview=layoutinflater.from(getapplicationcontext()).inflate(r.layout.item1,null);

textviewview=(textview)contentview.findviewbyid(r.id.imageview1);
if(data.length>position){
if(position<name.length){
view.settext(name[position]);
}
view.setbackgroundresource(data[position]);
}else{
view.setbackgroundresource(data[0]);
view.setclickable(false);
view.setvisibility(view.invisible);
}
returncontentview;
}

}
@override
publicbooleanonkeydown(intkeycode,keyeventevent){
//todoauto-generatedmethodstub
if(keycode==keyevent.keycode_back){
alertdialog.builderbuilder=newalertdialog.builder(this);
builder.setmessage("你确定退出吗?")
.setcancelable(false)
.setpositivebutton("确定",
newdialoginterface.onclicklistener(){
publicvoidonclick(dialoginterfacedialog,
intid){
finish();
}
})
.setnegativebutton("返回",
newdialoginterface.onclicklistener(){
publicvoidonclick(dialoginterfacedialog,
intid){
dialog.cancel();
}
});
alertdialogalert=builder.create();
alert.show();
returntrue;
}
returnsuper.onkeydown(keycode,event);
}
privatevoidloadapps(){
intentintent=newintent(intent.action_main,null);
intent.addcategory(intent.category_launcher);
apps=getpackagemanager().queryintentactivities(intent,0);
}
publicclassgridadapterextendsbaseadapter{
publicgridadapter(){
}
publicintgetcount(){
//todoauto-generatedmethodstub
returnapps.size();
}
publicobjectgetitem(intposition){
//todoauto-generatedmethodstub
returnapps.get(position);
}
publiclonggetitemid(intposition){
//todoauto-generatedmethodstub
returnposition;
}
publicviewgetview(intposition,viewconvertview,viewgroupparent){
//todoauto-generatedmethodstub
imageviewimageview=null;
if(convertview==null){
imageview=newimageview(bookshelfactivity.this);
imageview.setscaletype(imageview.scaletype.fit_center);
imageview.setlayoutparams(newgridview.layoutparams(50,50));
}else{
imageview=(imageview)convertview;
}
resolveinfori=apps.get(position);
imageview.setimagedrawable(ri.activityinfo
.loadicon(getpackagemanager()));
returnimageview;
}
}
}

代码写的有点乱,有待整理下,哈哈。
上面只是一个画龙点睛的作用,真要实现一个好的电子书,后面还有跟多的工作,也希望有兴趣的朋友能在此基础上实现一个完美的电子书,然后把源代码开放,这样我就不用在往下做了,嘎嘎。