android使用gridview的书架效果
16lz
2021-12-04
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 ,附件给出它的下载源码
更多相关文章
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android]Android(安卓)线程优先级修改
- Android(安卓)Studio安装使用图文教程
- Android(安卓)studio使用lambda表达式
- Android(安卓)高斯模糊 RenderScript封装工具类
- Android(安卓)Studio常见布局报错及解决方案