android瀑布流简单实现原理

网上关于android瀑布流的例子一大堆,但是很多都是很复杂,对于新手来说有一定的难度。

原理很简单,就是异步下载图片,把图片addView到ScrollView(因为可以上下一直拖动)中,你需要屏幕显示几列就在ScrollView中放置几个LinearLayout,

下面我就一个简单的例子来讲解android瀑布流的用法,样子很丑就不上图了。。

1、在xml布局文件:很简单就是

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollview">
    <LinearLayout 
        android:orientation="horizontal"
         android:layout_width="fill_parent"
   		 android:layout_height="wrap_content">
       <LinearLayout 
         android:id="@+id/left"
         android:orientation="vertical"
         android:layout_width="fill_parent"
   		 android:layout_height="wrap_content"
        >
   		</LinearLayout>
      <LinearLayout 
          android:id="@+id/right"
          android:orientation="vertical"
          android:layout_width="fill_parent"
     		android:layout_height="wrap_content">
      </LinearLayout>
    </LinearLayout>
</ScrollView>

2、在java代码中:

先声明几个变量,其中imagePathStr数组用来存图片的链接

private LinearLayout leftLayout;
  private LinearLayout rightLayout;
  private String[] imagePathStr = { "http://www.cf69.com/Upfiles/BeyondPic/2010-08/20108175740983313.jpg",
             "http://www.syfff.com/UploadFile/pic/2008122163204.jpg", "http://pic.newssc.org/0/10/34/32/10343297_564251.jpg",
             "http://ent.hangzhou.com.cn/images/20090311/zym2009031323.jpg", "http://a4.att.hudong.com/86/60/01300000013093119087608457965.jpg",
             "http://file.sdteacher.gov.cn/upload/gz0901/images/0907/22/110437191.jpg",
             "http://www.fun9.cn/uploadfile/starpic/uploadpics/200910/20091008090155126.jpg",
             "http://img3.yxlady.com/yl/UploadFiles_5361/20110820/20110820120609469.jpg",
             "http://news.sznews.com/content/images/site3/20070827/001558d90baa083c6da20d.jpg", "http://henan.sinaimg.cn/cr/2010/0824/2297073692.jpg",
             "http://www.cf69.com/Upfiles/BeyondPic/2010-08/20108175740983313.jpg", "http://www.syfff.com/UploadFile/pic/2008122163204.jpg",
             "http://pic.newssc.org/0/10/34/32/10343297_564251.jpg", "http://ent.hangzhou.com.cn/images/20090311/zym2009031323.jpg",
             "http://a4.att.hudong.com/86/60/01300000013093119087608457965.jpg", "http://file.sdteacher.gov.cn/upload/gz0901/images/0907/22/110437191.jpg",
             "http://www.fun9.cn/uploadfile/starpic/uploadpics/200910/20091008090155126.jpg",
             "http://img3.yxlady.com/yl/UploadFiles_5361/20110820/20110820120609469.jpg",
             "http://news.sznews.com/content/images/site3/20070827/001558d90baa083c6da20d.jpg", "http://henan.sinaimg.cn/cr/2010/0824/2297073692.jpg" };

其次,在oncreate()中采用异步加载图片的方法把获取到的Drawable添加到左右两栏的LinearLayout中:

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    leftLayout=(LinearLayout) findViewById(R.id.left);
    rightLayout=(LinearLayout) findViewById(R.id.right);
    int j=0;
    for (int i = 0; i < imagePathStr.length; i++) {
      addToAsynLoadImage(imagePathStr[i],j,i);
      j++;
      if(j<=2){
        j=0;
      }
    }
    
  }

addToAsynLoadImage() 方法如下,每次加载一个图片就创建一个ImageView,然后把ImageView加到LinearLayout中:

private void addToAsynLoadImage(String imageUrl, int j, int i) {
    ImageView imageView=new ImageView(this);
    imageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
    imageView.setTag(i);
    new ImageDownloadAsynTask(MainActivity.this,imageUrl,imageView).execute(null);
    if(j==0){
      leftLayout.addView(imageView);
    }else if(j==1){
      rightLayout.addView(imageView);
    }
  }

ImageDownloadAsynTask()方法继承自AsyncTask<Params, Progress, Result>,

这个类有三个参数,4个步骤(begin,doinbackground,processProgress,end)

最后一个参数是在doinbackground()中返回的结果,另外还有onPreExecute()、onPostExecute()

public class ImageDownloadAsynTask extends AsyncTask<Void, Void, Drawable>{

  private Context context;
  private String imageUrl;
  private ImageView imageView;
  private String sdPath="/sdcard/netImages";
  ProgressDialog progressDialog;
  
  public ImageDownloadAsynTask(Context context, String imageUrl,ImageView imageView) {
    this.context=context;
    this.imageUrl=imageUrl;
    this.imageView=imageView;
  }

  /* 后台执行,比较耗时的操作都可以放在这里。注意这里不能直接操作UI
   * 不需要传入什么参数,返回一个Drawable
   */
  @Override
  protected Drawable doInBackground(Void... params) {
    String filename=sdPath+imageUrl.substring(imageUrl.lastIndexOf("/"));
    File file=new File(filename);
    if(file.exists()==true){
      Bitmap bitmap=BitmapFactory.decodeFile(filename);
      BitmapDrawable bitmapDrawable=new BitmapDrawable(bitmap);
      return bitmapDrawable;
    }else{
      try {
        URL url=new URL(imageUrl);
        URLConnection connection=url.openConnection();
        connection.setDoInput(true);// 使用 URL 连接进行输入
        connection.connect();
        InputStream is = connection.getInputStream();
        Bitmap b=BitmapFactory.decodeStream(is);
        BitmapDrawable bd=new BitmapDrawable(b);
        saveFile(bd,filename);
//				connection.getContent();
        return bd;
      } catch (MalformedURLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return null;
  }

  
  /**通过outPutStream、bitmap.compress(),flush()把图片保存到指定路径
   * @param bd
   * @param filename
   */
  private void saveFile(BitmapDrawable bd, String filename) {
    File file = new File(sdPath);
    if(!file.exists()){
      file.mkdir();
    }
    File f=new File(filename);
    try {
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
      Bitmap b=bd.getBitmap();
      b.compress(Bitmap.CompressFormat.JPEG, 80, bos);
      bos.flush();
      bos.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  protected void onPreExecute() {
    super.onPreExecute();
    progressDialog.show(context, "","正在下载图片。。。");
  }
  
  
  /**
   * 相当于Handler 处理UI的方式,在这里面可以使用在doInBackground 得到的结果处理操作UI。
   * 此方法在主线程执行,任务执行的结果作为此方法的参数返回
   */
  @Override
  protected void onPostExecute(Drawable result) {
    super.onPostExecute(result);
    if(result!=null){//如果doInBackground()获取的结果不为空
      imageView.setBackgroundDrawable(result);//那么就在这一步更新UI
    }
    progressDialog.dismiss();
  }


}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
33号铺是使用codeigniter和淘宝API制作的淘宝客瀑布流系统 33号铺的设计理念是,做一个体验最好的导购系统。 相信PGC(Professional Generated Content),而不是蘑菇街那样的UGC。但不排除加入投票系统。 相信人工的推荐,而不是机器采集。机器采集的垃圾站我见过很多,大部分是根据taobao API批量拉取数据,这个功能很好做,但是我不希望做自动采集的站点,这对用户是没有好处的。所以33号铺的优化目标是尽量优化人工采集的流程,后台也好,书签也好,url转化也好。 重视用户体验,相信用户体验也可以卖钱。所以33号铺会不断优化站长和访客的体验。 下载 Clone代码到本地,git clone git://github.com/yuguo/33pu.git 或者下载最新的ZIP 安装 配置 application/config/config.php 为你的站点url,配置 application/config/site_info.php 中的站点名称、appkey、secret还有最重要的taobaoke pid。taobaoke pid不再作为参数传入,现在是以自己的开发账户为准,官方解释如下: 未来pid、nick入参将取消,程序会自动根据appkey对应的nick去查询pid。pid、nick入参将兼容支持到10月31日,请ISV做好改造工作,不要传入pid或nick。 首先自己在数据库中创建一个数据库(比如使用phpmyadmin之类的可视化工具 ),然后配置application/database 中的 username,password,database 访问 站点url/index.php/login/install ,输入管理员的email和密码 访问 站点url/index.php/login 登录 访问 站点url/index.php/admin/cat 新增你的站点的商品类别(类别会出现在首页tab中) 访问 站点url/index.php/admin/cat 修改类别slug为英文(中文url目前有bug,而且不优雅) 访问 站点url/index.php/admin ,选择类别之后搜索关键词,点击某个条目之后再选择图片,条目就会出现在首页(请选择类别之后再搜索关键词,这样条目会自动添加到该类别) 请修改 application/views/home.php 底部的统计代码为你自己的百度统计或者Google Analytics. 说明 后台搜索的时候的过滤条件在后台配置是,您可以自己修改配置application/models/m_taobaoapi.php: 佣金比5% - 50% 天猫商品 按卖家信用排序 每页80条 关于为什么要想到做这个系统的两篇文章:先做一半 利用淘宝API构建淘宝客自动发布系统 为了帮助更多人,希望你能保留底部的版权,声明站点是由33号铺构建,但这并不是必须的 系统架构 整站大部分代码是PHP,基于CodeIgniter构建,CodeIgniter是一个非常适合快速开发的PHP框架。 后台UI基于Bootstrap构建。 整站的JS都是基于jQuery构建。 数据来源于淘宝开放平台。 demo:33号铺 标签:33号铺
要在Android中实现瀑布流的效果,可以使用RecyclerView和Adapter来实现。首先,需要创建一个布局文件fragment_friend.xml,其中包含一个LinearLayout和一个RecyclerView。LinearLayout用于包裹RecyclerView,并设置垂直方向的布局。RecyclerView用于展示瀑布流的内容,需要设置宽度为match_parent,高度为wrap_content,并禁用滚动条和过度滚动效果。\[1\] 接下来,需要创建一个item_friend.xml布局文件,用于定义每个瀑布流项的样式。可以在LinearLayout中添加一个ImageView,设置宽度为match_parent,高度为wrap_content,并设置适当的内边距。\[2\] 在代码中,需要创建一个适配器FriendAdapter.java,用于将数据绑定到RecyclerView上。可以继承RecyclerView.Adapter,并实现必要的方法。在适配器中,可以根据需要加载图片或其他内容,并将其显示在ImageView上。\[3\] 最后,在friendFragment.java文件中,需要实现必要的功能,如加载数据、设置适配器等。可以使用LayoutManager来设置RecyclerView的布局管理器,以实现瀑布流的效果。 通过以上步骤,就可以在Android中实现瀑布流的效果了。 #### 引用[.reference_title] - *1* *2* *3* [Android开发:RecyclerView和Adapter实现瀑布流效果](https://blog.csdn.net/m0_47114547/article/details/123818917)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值