源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android GridView仿微信朋友圈显示图片

  • 时间:2021-07-23 02:49 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android GridView仿微信朋友圈显示图片
最近项目要求上传多图并且多图显示,而且要规则的显示,就像微信朋友圈的图片显示一样。 利用GridView再适合不过了,GridView可以动态加载图片的数量,而且还比较规律,下面说一下自己的思路: [list] [*][b]1.获取网络图片[/b][/*] [*][b]2.初始化gridview,自定义适配器[/b][/*] [*][b]3.根据图片数量设置gridview的列数[/b][/*] [*][b]4.更新适配器[/b][/*] [/list] 下面贴上部分源码并给大家解析一下 [b]一、首先是GridView的item[/b]
<com.view.SquareLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:orientation="vertical" > 
 <ImageView 
  android:id="@+id/item_grida_image" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:scaleType="fitXY" 
  android:layout_margin="@dimen/tinyest_space"> 
 </ImageView> 
</com.view.SquareLayout> 
这里的SquareLayout布局是自定义的下面会给大家详细讲解。 子项中是一个正方形布局里面嵌套着图片 [b]二、接下来自定义适配器[/b] 因为项目需求不同,自己定义的适配器和平时用的不太一样,这里就不贴源码了。大体上也是将图片下载到本地,用Imageloader加载,不过我这里有上传失败的和新建的,所以不太一样。 [b]三、最后在用到的Activity中设置[/b]
noScrollgridview = (GridView) findViewById(R.id.noScrollgridview); 
  noScrollgridview.setNumColumns(3); //默认设置在3列图片 
  //上传成功传值给adapter 
  picAdapter = new PictureAdapter(this, 1, appItem_file); 
  noScrollgridview.setAdapter(picAdapter); 



//根据图片数量设置图片的列 
  int size = appItemFile.getFiles().split(",").length; 
  if (size==1){ 
   noScrollgridview.setNumColumns(1); 
  } 
  else if (size==2){ 
   noScrollgridview.setNumColumns(2); 
  } 
  else if (size>2){ 
   noScrollgridview.setNumColumns(3); 
  } 
  picAdapter.notifyDataSetChanged(); 

默认设置GridView的列数为3,根据图片的数量动态设置列数。 最后贴上SquareLayout的源码解析一下
/** 
 * 方形布局 
 */ 
public class SquareLayout extends RelativeLayout { 
 public SquareLayout(Context context, AttributeSet attrs, int defStyle) { 
  super(context, attrs, defStyle); 
 } 
 
 public SquareLayout(Context context, AttributeSet attrs) { 
  super(context, attrs); 
 } 
 
 public SquareLayout(Context context) { 
  super(context); 
 } 
 
 @SuppressWarnings("unused") 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  // For simple implementation, or internal size is always 0. 
  // We depend on the container to specify the layout size of 
  // our view. We can't really know what it is since we will be 
  // adding and removing different arbitrary views and do not 
  // want the layout to change as this happens. 
  setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), 
    getDefaultSize(0, heightMeasureSpec)); 
 
  // Children are just made to fill our space. 
  int childWidthSize = getMeasuredWidth(); 
  int childHeightSize = getMeasuredHeight(); 
  // 高度和宽度一样 
  heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec( 
    childWidthSize, MeasureSpec.EXACTLY); 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 } 
} 
这里主要重写了onMeasure()方法,设置了高宽,需要注意的是在用SquareLayout的时候要设置它的高宽都是match_parent。这样就可以填满GridView的每一项了。 接下来贴图给大家看: [img]http://files.jb51.net/file_images/article/201601/2016126145731075.jpg?2016026145741[/img] ImgeView的scaleType的属性如果设置FitXY就会充满方形布局,如果center就会居中显示 详细说一下吧: [b]1)center:[/b]保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分裁剪处理。 [b]2)centerCrop:[/b]以填满整个ImageView为目的,将原图的中心对准ImageView的中心,等比例放大原图,直到填满ImageView为止(指的是ImageView的宽和高都要填满),原图超过ImageView的部分作裁剪处理。 [b]3)centerInside:[/b]以原图完全显示为目的,将图片的内容完整居中显示,通过按比例缩小原图的size宽(高)等于或小于ImageView的宽(高)。如果原图的size本身就小于ImageView的size,则原图的size不作任何处理,居中显示在ImageView。 [b]4)matrix:[/b]不改变原图的大小,从ImageView的左上角开始绘制原图,原图超过ImageView的部分作裁剪处理 [b]5)fitCenter:[/b]把原图按比例扩大或缩小到ImageView的ImageView的高度,居中显示 [b]6)fitEnd:[/b]把原图按比例扩大(缩小)到ImageView的高度,显示在ImageView的下部分位置 [b]7)fitStart:[/b]把原图按比例扩大(缩小)到ImageView的高度,显示在ImageView的上部分位置 [b]8)fitXY:[/b]把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageView. 本文已被整理到了《[url=http://www.1sucai.cn/Special/715.htm]Android微信开发教程汇总[/url]》,欢迎大家学习阅读。 以上就是本文的全部内容,希望对大家的学习有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部