android使用gridview的书架效果,需要使用自定义的gridview来填充每行的背景,核心代码如下:

BookGridView.java

/** * 书架效果 * */public class BookGridView extends GridView {private Bitmap background;Context context;public BookGridView(Context context, AttributeSet attrs) {super(context, attrs);this.context = context;background = null;}@Overrideprotected void dispatchDraw(Canvas canvas) {int count = getChildCount();int top = count > 0 ? getChildAt(0).getTop() : 0;int width = getWidth();int height = getHeight();int totalCount = this.getCount();//如果总数为0自然没必要计算高度并画图if (totalCount > 0) {//获取列数int numColumns=3;// 计算行高,通过行数和控件高度计算int rowCount = totalCount / numColumns;if (totalCount % numColumns != 0) {rowCount++;}int rowHeight = height / rowCount;if (background == null) {background = BitmapFactory.decodeResource(getResources(),R.drawable.bookshelf_layer_center);background = Bitmap.createScaledBitmap(background, width,rowHeight, true);}int backgroundWidth = background.getWidth();int backgroundHeight = background.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);}}

 

 

使用如下:

            

  具体可以参照:

http://blog.csdn.net/caizhegnhao/article/details/38857531 ,附件给出它的下载源码

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. android]Android(安卓)线程优先级修改
  6. Android(安卓)Studio安装使用图文教程
  7. Android(安卓)studio使用lambda表达式
  8. Android(安卓)高斯模糊 RenderScript封装工具类
  9. Android(安卓)Studio常见布局报错及解决方案

随机推荐

  1. 简单明了的分析Android触摸事件,看完再也
  2. Android实例剖析笔记(二)
  3. Android activity 详解一:activity的生命
  4. 一个android工程的运行过程
  5. adb下的tcpdump抓包方法
  6. Android百度地图(六):百度地图POI检索,行
  7. android:paddingLeft与android:layout_ma
  8. 在IDEA/Android Studio中调试Gradle Plug
  9. android task与back stack 开发文档翻译
  10. qt for android 实现开机自启