以前也模仿者ireader实现了书架的效果,但是那种是使用listview实现的,并不好用。绝大多数都是用gridview实现的,网上这方面资料比较少,有些开源的电子书都是重点做了阅读,并没有像ireader和QQ阅读这样的书架效果。
书架这种效果我早就实现了,本来想做一个完美的电子书,但是因为自己的懒惰,仅仅持续了一两天,今天又找到了以前的代码分享出来,希望大家能一起实现一个比较完美的开源的电子书。废话不多说先看下效果:
本地部分还没有做,做好以后就可以吧本地的书加载到书架里了,这只是一个开始,后面还有很多复杂的没有做。
下面先看一下书架的实现原理吧!
首先看一下layout下的布局文件main.xml
public class MyGridView extends GridView {
private Bitmap background;
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
background = BitmapFactory.decodeResource(getResources(),
R.drawable.bookshelf_layer_center);
}
@Override
protected void dispatchDraw(Canvas canvas) {
int count = getChildCount();
int top = count > 0 ? getChildAt(0).getTop() : 0;
int backgroundWidth = background.getWidth();
int backgroundHeight = background.getHeight()+2;
int width = getWidth();
int height = getHeight();
for (int y = top; y < height; y += backgroundHeight) {
for (int x = 0; x < width; x += backgroundWidth) {
canvas.drawBitmap(background, x, y, null);
}
}
super.dispatchDraw(canvas);
}
}
然后是每一个item的布局:
最后就可以在主activity中显示出来了。
public class BookShelfActivity extends BaseActivity {
private GridView bookShelf;
private int[] 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
};
private String[] name={
"天龙八部","搜神记","水浒传","黑道悲情"
};
private GridView gv;
private SlidingDrawer sd;
private Button iv;
private List apps;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bookShelf = (GridView) findViewById(R.id.bookShelf);
ShlefAdapter adapter=new ShlefAdapter();
bookShelf.setAdapter(adapter);
bookShelf.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
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(new GridAdapter());
sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()// 开抽屉
{
@Override
public void onDrawerOpened() {
iv.setText("返回");
iv.setBackgroundResource(R.drawable.btn_local);// 响应开抽屉事件
// ,把图片设为向下的
}
});
sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
iv.setText("本地");
iv.setBackgroundResource(R.drawable.btn_local);// 响应关抽屉事件
}
});
}
class ShlefAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.length+5;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View contentView, ViewGroup arg2) {
// TODO Auto-generated method stub
contentView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item1, null);
TextView view=(TextView) contentView.findViewById(R.id.imageView1);
if(data.length>position){
if(position
代码写的有点乱,有待整理下,哈哈。
最后附上源码下载地址:点击打开链接