Android书架实现
阅读原文时间:2023年07月09日阅读:2

转自http://blog.csdn.net/wangkuifeng0118/article/details/7944215

书架效果:

  下面先看一下书架的实现原理吧!

  首先看一下layout下的布局文件main.xml


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"

 <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>  

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

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);  
 }

}

  上面就是自定义书架的gridview,也是实现书架最核心的方法。

  然后是每一个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<ResolveInfo> 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<name.length){  
                view.setText(name\[position\]);  
             }  
             view.setBackgroundResource(data\[position\]);  
         }else{  
             view.setBackgroundResource(data\[0\]);  
             view.setClickable(false);  
             view.setVisibility(View.INVISIBLE);  
         }  
         return contentView;  
     }

 }

 @Override  
 public boolean onKeyDown(int keyCode, KeyEvent event) {  
     // TODO Auto-generated method stub

     if (keyCode == KeyEvent.KEYCODE\_BACK) {  
         AlertDialog.Builder builder = new AlertDialog.Builder(this);  
         builder.setMessage("你确定退出吗?")  
                 .setCancelable(false)  
                 .setPositiveButton("确定",  
                         new DialogInterface.OnClickListener() {  
                             public void onClick(DialogInterface dialog,  
                                     int id) {  
                                 finish();  
                             }  
                         })  
                 .setNegativeButton("返回",  
                         new DialogInterface.OnClickListener() {  
                             public void onClick(DialogInterface dialog,  
                                     int id) {  
                                 dialog.cancel();  
                             }  
                         });  
         AlertDialog alert = builder.create();  
         alert.show();  
         return true;  
     }

     return super.onKeyDown(keyCode, event);  
 }

 private void loadApps() {  
     Intent intent = new Intent(Intent.ACTION\_MAIN, null);  
     intent.addCategory(Intent.CATEGORY\_LAUNCHER);  

     apps = getPackageManager().queryIntentActivities(intent, 0);  
 }  

 public class GridAdapter extends BaseAdapter {  
     public GridAdapter() {  

     }  

     public int getCount() {  
         // TODO Auto-generated method stub  
         return apps.size();  
     }  

     public Object getItem(int position) {  
         // TODO Auto-generated method stub  
         return apps.get(position);  
     }  

     public long getItemId(int position) {  
         // TODO Auto-generated method stub  
         return position;  
     }  

     public View getView(int position, View convertView, ViewGroup parent) {  
         // TODO Auto-generated method stub  
         ImageView imageView = null;  
         if (convertView == null) {  
             imageView = new ImageView(BookShelfActivity.this);  
             imageView.setScaleType(ImageView.ScaleType.FIT\_CENTER);  
             imageView.setLayoutParams(new GridView.LayoutParams(50, 50));  
         } else {  
             imageView = (ImageView) convertView;  
         }  

         ResolveInfo ri = apps.get(position);  
         imageView.setImageDrawable(ri.activityInfo  
                 .loadIcon(getPackageManager()));  

         return imageView;  
     }  

 }  

}

源码下载:http://download.csdn.net/detail/wangkuifeng0118/4548542

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章