转自http://blog.csdn.net/wangkuifeng0118/article/details/7944215
书架效果:
下面先看一下书架的实现原理吧!
首先看一下layout下的布局文件main.xml
1 xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:orientation="vertical" 5 android:layout_width="fill_parent" 6 android:layout_height="fill_parent" 7 > 8 <include layout="@layout/head" android:id="@+id/head"/> 9 10 <cn.com.karl.view.MyGridView 11 android:id="@+id/bookShelf" 12 android:layout_width="fill_parent" 13 android:layout_height="fill_parent" 14 android:layout_below="@id/head" 15 android:cacheColorHint="#00000000" 16 android:columnWidth="90.0dip" 17 android:fadingEdge="none" 18 android:horizontalSpacing="5dp" 19 android:listSelector="#00000000" 20 android:numColumns="3" 21 android:scrollbars="none" 22 android:verticalSpacing="20dp" /> 23 24 <SlidingDrawer 25 android:id="@+id/sliding" 26 android:layout_width="match_parent" 27 android:layout_height="match_parent" 28 android:content="@+id/allApps" 29 android:handle="@+id/imageViewIcon" 30 android:orientation="vertical" > 31 32 33 <Button 34 android:id="@+id/imageViewIcon" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="本地" 38 android:textSize="18dp" 39 android:background="@drawable/btn_local" /> 40 41 <GridView 42 android:id="@+id/allApps" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:background="@drawable/file_list_bg" 46 android:columnWidth="60dp" 47 android:gravity="center" 48 android:horizontalSpacing="10dp" 49 android:numColumns="auto_fit" 50 android:padding="10dp" 51 android:stretchMode="columnWidth" 52 android:verticalSpacing="10dp" /> 53 54 55 56 SlidingDrawer> 57 58 RelativeLayout>
上面是个自定义的gridview主要来实现书架,因为每一本书是一个item,在自定义的gridview中计算每一行的高度,然后把书架画上去。下面是个抽屉。
1 public class MyGridView extends GridView { 2 3 private Bitmap background; 4 5 public MyGridView(Context context, AttributeSet attrs) { 6 super(context, attrs); 7 background = BitmapFactory.decodeResource(getResources(), 8 R.drawable.bookshelf_layer_center); 9 } 10 11 @Override 12 protected void dispatchDraw(Canvas canvas) { 13 int count = getChildCount(); 14 int top = count > 0 ? getChildAt(0).getTop() : 0; 15 int backgroundWidth = background.getWidth(); 16 int backgroundHeight = background.getHeight()+2; 17 int width = getWidth(); 18 int height = getHeight(); 19 20 for (int y = top; y < height; y += backgroundHeight) { 21 for (int x = 0; x < width; x += backgroundWidth) { 22 canvas.drawBitmap(background, x, y, null); 23 } 24 } 25 26 super.dispatchDraw(canvas); 27 } 28 29 }
上面就是自定义书架的gridview,也是实现书架最核心的方法。
然后是每一个item的布局:
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: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中显示出来了。
1 public class BookShelfActivity extends BaseActivity { 2 private GridView bookShelf; 3 private int[] data = { 4 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt, 5 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt, 6 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt, 7 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt, 8 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt, 9 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt, 10 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt, 11 R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt,R.drawable.cover_txt 12 13 }; 14 private String[] name={ 15 "天龙八部","搜神记","水浒传","黑道悲情" 16 }; 17 18 private GridView gv; 19 private SlidingDrawer sd; 20 private Button iv; 21 private Listapps; 22 23 24 /** Called when the activity is first created. */ 25 @Override 26 public void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 29 setContentView(R.layout.main); 30 31 bookShelf = (GridView) findViewById(R.id.bookShelf); 32 ShlefAdapter adapter=new ShlefAdapter(); 33 bookShelf.setAdapter(adapter); 34 bookShelf.setOnItemClickListener(new OnItemClickListener() { 35 36 @Override 37 public void onItemClick(AdapterView> arg0, View arg1, int arg2, 38 long arg3) { 39 // TODO Auto-generated method stub 40 if(arg2>=data.length){ 41 42 }else{ 43 Toast.makeText(getApplicationContext(), ""+arg2, Toast.LENGTH_SHORT).show(); 44 } 45 } 46 }); 47 loadApps(); 48 gv = (GridView) findViewById(R.id.allApps); 49 sd = (SlidingDrawer) findViewById(R.id.sliding); 50 iv = (Button) findViewById(R.id.imageViewIcon); 51 gv.setAdapter(new GridAdapter()); 52 sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener()// 开抽屉 53 { 54 @Override 55 public void onDrawerOpened() { 56 iv.setText("返回"); 57 iv.setBackgroundResource(R.drawable.btn_local);// 响应开抽屉事件 58 // ,把图片设为向下的 59 } 60 }); 61 sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { 62 @Override 63 public void onDrawerClosed() { 64 iv.setText("本地"); 65 iv.setBackgroundResource(R.drawable.btn_local);// 响应关抽屉事件 66 } 67 }); 68 } 69 70 class ShlefAdapter extends BaseAdapter{ 71 72 @Override 73 public int getCount() { 74 // TODO Auto-generated method stub 75 return data.length+5; 76 } 77 78 @Override 79 public Object getItem(int arg0) { 80 // TODO Auto-generated method stub 81 return arg0; 82 } 83 84 @Override 85 public long getItemId(int arg0) { 86 // TODO Auto-generated method stub 87 return arg0; 88 } 89 90 @Override 91 public View getView(int position, View contentView, ViewGroup arg2) { 92 // TODO Auto-generated method stub 93 94 contentView=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item1, null); 95 96 TextView view=(TextView) contentView.findViewById(R.id.imageView1); 97 if(data.length>position){ 98 if(position<name.length){ 99 view.setText(name[position]); 100 } 101 view.setBackgroundResource(data[position]); 102 }else{ 103 view.setBackgroundResource(data[0]); 104 view.setClickable(false); 105 view.setVisibility(View.INVISIBLE); 106 } 107 return contentView; 108 } 109 110 } 111 112 @Override 113 public boolean onKeyDown(int keyCode, KeyEvent event) { 114 // TODO Auto-generated method stub 115 116 if (keyCode == KeyEvent.KEYCODE_BACK) { 117 AlertDialog.Builder builder = new AlertDialog.Builder(this); 118 builder.setMessage("你确定退出吗?") 119 .setCancelable(false) 120 .setPositiveButton("确定", 121 new DialogInterface.OnClickListener() { 122 public void onClick(DialogInterface dialog, 123 int id) { 124 finish(); 125 } 126 }) 127 .setNegativeButton("返回", 128 new DialogInterface.OnClickListener() { 129 public void onClick(DialogInterface dialog, 130 int id) { 131 dialog.cancel(); 132 } 133 }); 134 AlertDialog alert = builder.create(); 135 alert.show(); 136 return true; 137 } 138 139 return super.onKeyDown(keyCode, event); 140 } 141 142 143 private void loadApps() { 144 Intent intent = new Intent(Intent.ACTION_MAIN, null); 145 intent.addCategory(Intent.CATEGORY_LAUNCHER); 146 147 apps = getPackageManager().queryIntentActivities(intent, 0); 148 } 149 150 public class GridAdapter extends BaseAdapter { 151 public GridAdapter() { 152 153 } 154 155 public int getCount() { 156 // TODO Auto-generated method stub 157 return apps.size(); 158 } 159 160 public Object getItem(int position) { 161 // TODO Auto-generated method stub 162 return apps.get(position); 163 } 164 165 public long getItemId(int position) { 166 // TODO Auto-generated method stub 167 return position; 168 } 169 170 public View getView(int position, View convertView, ViewGroup parent) { 171 // TODO Auto-generated method stub 172 ImageView imageView = null; 173 if (convertView == null) { 174 imageView = new ImageView(BookShelfActivity.this); 175 imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 176 imageView.setLayoutParams(new GridView.LayoutParams(50, 50)); 177 } else { 178 imageView = (ImageView) convertView; 179 } 180 181 ResolveInfo ri = apps.get(position); 182 imageView.setImageDrawable(ri.activityInfo 183 .loadIcon(getPackageManager())); 184 185 return imageView; 186 } 187 188 } 189 190 }
源码下载:http://download.csdn.net/detail/wangkuifeng0118/4548542