<五>RecycleView+CardView实现瀑布流(类in界面效果)
来源:互联网 发布:不干胶排版打印软件 编辑:程序博客网 时间:2024/05/17 06:00
Demo效果:
该Demo应用了之前的文章:<二>Material主题的使用 <三>定义阴影与裁剪视图
分析
1>主题设置:
<!-- Base application theme. --><style name="AppTheme" parent= "Theme.AppCompat.Light.DarkActionBar" > <!-- Customize your theme here. --> <item name= "colorPrimary"> #ec584e </item> <item name= "colorPrimaryDark" >#ec584e </item> <item name= "colorAccent" >@color/colorAccent </item></style>
2>RecycleView:采用系统提供的StaggeredGridLayoutManager即可实现效果中的不规则排列的item效果。
StaggeredGridLayoutManager mLayoutManager = new StaggeredGridLayoutManager( 2 , StaggeredGridLayoutManager. VERTICAL ); //两列,纵向排列mRecyclerView.setLayoutManager(mLayoutManager) ;
3>Item项使用了CardView, 内部包含一个经过自定义的ImageView和TextView。代码如下:
<? xml version= "1.0" encoding= "utf-8" ?><android.support.v7.widget.CardView xmlns: android= "http://schemas.android.com/apk/res/android" xmlns: card_view ="http://schemas.android.com/apk/res-auto" android :id= "@+id/my_text_view" android :layout_width= "match_parent" android :layout_height= "wrap_content" card_view :cardUseCompatPadding= "true" > <LinearLayout android :layout_width= "match_parent" android :layout_height= "wrap_content" android :orientation= "vertical"> <com.example.wiky.materialdesigndemo1.view.DynamicHeightImageView android :id= "@+id/image" android :background= "#000000" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :scaleType= "fitXY" /> <TextView android :id= "@+id/tv_userName" android :layout_width= "match_parent" android :layout_height= "30dp" android :gravity= "center" android :textColor= "#000000"/> </LinearLayout></android.support.v7.widget.CardView>
4>因为本例中需要使每个ImageView都能展示一张完整的图片,即在宽度固定的情况下,我们需要自己动态的设置ImageView的高度,使ImageView的宽高比与图片的一致,才能够完整的显示缩放后的图片且避免变形。所以这里对ImageView进行自定义处DynamicHeightImageView。代码:
package com.example.wiky.materialdesigndemo1.view ;import android.content.Context ;import android.graphics.Bitmap ;import android.graphics.BitmapFactory ;import android.util.AttributeSet ;import android.widget.ImageView ;/*** 动态高度的ImageView* Created by wiky on 3/4/15.*/public class DynamicHeightImageView extends ImageView { /** * 图片高宽比(高/宽) */ private double hwRatio ; public DynamicHeightImageView(Context context , AttributeSet attrs) { super(context , attrs); } public DynamicHeightImageView(Context context) { super(context) ; } @Override protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec) { //获取当前ImageView分配的宽度(即Item项的宽度) int widthSize = MeasureSpec. getSize(widthMeasureSpec) ; if (widthSize!=0 && hwRatio != 0 ) { //根据高宽比,计算出ImagView需要的高度widthSize* hwRatio,并设置其大小 setMeasuredDimension(widthSize , ( int )(widthSize* hwRatio )); } else{ super .onMeasure(widthMeasureSpec, heightMeasureSpec) ; } } @Override public void setImageResource (int resId) { super.setImageResource(resId) ; //获取图片的高宽比(高/宽) BitmapFactory.Options options = new BitmapFactory.Options() ; options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeResource(getResources() , resId, options) ; hwRatio = options. outHeight /(double )options. outWidth; bmp.recycle(); } }
<补充>方法二:不需要自定义的ImageView,可以使用android:adjustViewBounds=”true”,即调整ImageView的界限来保持图像纵横比不变(宽、高其中一个是确定值,另一个根据宽高比调整)。android:adjustViewBounds=”true”,会将这个ImageView的scaleType设为fitCenter,不过可被覆盖。这里建议用fitXY(可避免计算上的误差导致图片未能铺满ImageView)
源码:
项目地址:http://download.csdn.net/detail/cai_iac/9385391
0 0
- <五>RecycleView+CardView实现瀑布流(类in界面效果)
- <五>RecycleView+CardView实现瀑布流(类in界面效果)
- 瀑布流效果RecycleView+CardView
- RecycleView 实现瀑布流效果
- <八>RecycleView+CardView实现瀑布流(2、加载网络图片)
- <八>RecycleView+CardView实现瀑布流(2、加载网络图片)
- RecycleView实现瀑布流的效果
- RecycleView实现瀑布流
- Recycleview实现瀑布流
- recycleview实现瀑布流
- (4.1.11.1)Android中使用RecyclerView和CardView实现瀑布流效果(StaggeredGrid)
- Android手把手教你实现卡片式瀑布流效果(RecyclerView+CardView,附源码)
- Android中使用RecyclerView和CardView实现瀑布流效果(StaggeredGrid)
- pulltorefresh 实现瀑布流的方式(基于recycleview)
- RecycleView 实现瀑布流 添加分隔
- RecycleView实现瀑布流添加分隔线
- RecycleView的使用(包括瀑布流)
- recycleView 瀑布流
- GreenDao的简单使用说明(二)单表的增,删,改,查
- POI实现Excl文件的导入
- 嵌入式系统WinCE下应用程序GUI界面开发
- 【常用工具类】DensityUtils(dp px 互相转换)
- 从头认识java-15.7 Map(2)-介绍HashMap的工作原理-put方法
- <五>RecycleView+CardView实现瀑布流(类in界面效果)
- Web Scraping with Python 学习笔记9
- Android 圆角边框
- pandas安装若干异常及解决方案总结
- web前端基本用法(二)
- iOS图片处理(一)调用系统相机和相册获取图片,给相机添加自定义覆盖物
- dos 网络相关 指令
- .net 分布式架构之分布式缓存中间件
- log4j配置详解