安卓仿蘑菇街瀑布流demo
这是蘑菇街的页面,看上去像gridview,但是每个图片item高度都有不同,这样的风格显得有点儿洋气是吧。其实这并不是用gridview实现,用linearlayout实现布局即可。
首先自定义一个scrollview,然后往这个scrollview里添加一个linearlayout布局,再往linearlayout里面动态添加列布局,本例添加3列。
布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone"
></ProgressBar>
<com.example.mywaterflldemo.LazyScrollView
android:id="@+id/scroll_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/image_ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></LinearLayout>"
</com.example.mywaterflldemo.LazyScrollView>"
<TextView
android:id="@+id/loading_mesg_tv"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:gravity="center" />
</RelativeLayout>
异步图片下载类:
public class ImageDownLoadAsyncTask extends AsyncTask<Void, Void, Bitmap> {
private String imagePath;
private ImageView imageView;
private static final String ALBUM_PATH = "/sdcard/pb";
private Context context;
private AssetManager assetManager;
private int Image_width;// 显示图片的宽度
private final String file = "images/";
private ProgressBar progressbar;
private TextView loadtext;
/**
* 构造方法
*
* @param context
* @param imagePath
* @param imageView
*/
public ImageDownLoadAsyncTask(Context context, String imagePath,
ImageView imageView, int Image_width) {
this.imagePath = imagePath;
this.imageView = imageView;
this.context = context;
assetManager = this.context.getAssets();
this.Image_width = Image_width;
}
public void setLoadtext(TextView loadtext) {
this.loadtext = loadtext;
}
public void setProgressbar(ProgressBar progressbar) {
this.progressbar = progressbar;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;// 这里只返回bitmap的大小
InputStream inputStream = assetManager.open(file + imagePath);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null,
options);
return bitmap;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(Bitmap drawable) {
// TODO Auto-generated method stub
super.onPostExecute(drawable);
if (drawable != null) {
LayoutParams layoutParams = imageView.getLayoutParams();
int height = drawable.getHeight();// 获取图片的高度.
int width = drawable.getWidth();// 获取图片的宽度
layoutParams.height = (height * Image_width) / width;
imageView.setLayoutParams(layoutParams);
imageView.setImageBitmap(drawable);
}
if (progressbar.isShown() || loadtext.isShown()) {
progressbar.setVisibility(View.GONE);
loadtext.setVisibility(View.GONE);
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (!loadtext.isShown()) {
loadtext.setVisibility(View.VISIBLE);
}
}
}
主Activity:获取asset文件夹下图片总数,布局分为3列,类似数组方式将图片逐个添加至imageview布局里面。本例每页显示图片数量和列数都可动态修改。页面上滑至底部获取更多图片展示下一页布局
public class MainActivity extends Activity implements OnScrollListener {
private int colum = 3; // 图片总共4列显示
private int count = 20; // 每页显示的图片个数
private int curPage = 0; // 当前页码
private int itemWidth = 0; // 图片宽度
private List<LinearLayout> linearLayouts; // 列布局
private LazyScrollView lazyscrollview;
private ProgressBar loading_bar; // 加载进度条
private TextView loading_tv; // 加载图片提示语
private LinearLayout img_ll; // 图片显示布局
private AssetManager assetManager;
private List<String> image_filenames; // 图片集合
private final String file = "images";
private ImageDownLoadAsyncTask asyncTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
assetManager = this.getAssets();
try {
image_filenames = Arrays.asList(assetManager.list(file));
} catch (IOException e) {
e.printStackTrace();
}
addImage(curPage, count);
}
private void initView() {
// TODO Auto-generated method stub
lazyscrollview = (LazyScrollView) findViewById(R.id.scroll_container);
lazyscrollview.getView();
lazyscrollview.setOnScrollListener(this);
loading_bar = (ProgressBar) findViewById(R.id.progressbar);
loading_tv = (TextView) findViewById(R.id.loading_mesg_tv);
img_ll = (LinearLayout) findViewById(R.id.image_ll);
linearLayouts = new ArrayList<LinearLayout>();
itemWidth = getWindowManager().getDefaultDisplay().getWidth() / colum; // 获取每列图片的宽度
for (int i = 0; i < colum; i++) {
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
itemWidth, LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(params);
linearLayouts.add(ll);
img_ll.addView(ll);
}
}
// 获取相应的BitmapFactory.Options
private BitmapFactory.Options getBitmapBounds(String filename) {
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true; // 只返回bitmap的大小等信息,防止oom
InputStream ins = null;
try {
ins = assetManager.open((file) + "/" + filename);
} catch (IOException e) {
e.printStackTrace();
}
BitmapFactory.decodeStream(ins, null, option);// 解码图片
return option;
}
// 获取显示图片的对象
private ImageView getImageView(String imgName) {
ImageView imageview = new ImageView(this);
BitmapFactory.Options option = getBitmapBounds(imgName);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.FILL_PARENT);
imageview.setLayoutParams(params);
imageview.setMinimumHeight(option.outHeight);
imageview.setMinimumWidth(option.outWidth);
imageview.setPadding(2, 0, 2, 2);
imageview.setBackgroundResource(R.drawable.image_border);
if (option != null)
option = null;
return imageview;
}
/*
* 添加相应的图片imageview imageName: 图片名称 j:列 i:行
*/
private void addBitMapToImage(String imageName, int j, int i) {
ImageView imageview = getImageView(imageName);
asyncTask = new ImageDownLoadAsyncTask(this, imageName, imageview,
itemWidth);
asyncTask.setLoadtext(loading_tv);
asyncTask.setProgressbar(loading_bar);
asyncTask.execute();
imageview.setTag(i);
linearLayouts.get(j).addView(imageview);
imageview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "您点击了弟" + v.getTag() + "个图片",
Toast.LENGTH_SHORT).show();
}
});
}
// 加载更多
private void addImage(int current_page, int count) {
int j = 0;
int imageCount = image_filenames.size();
for (int i = curPage * count; i < imageCount
&& i < (curPage + 1) * count; i++) {
addBitMapToImage(image_filenames.get(i), j, i);
j++;
if (j >= colum)
j = 0;
}
}
@Override
public void onBottom() {
// TODO Auto-generated method stub
addImage(++curPage, count);
}
@Override
public void onTop() {
// TODO Auto-generated method stub
}
@Override
public void onScroll() {
// TODO Auto-generated method stub
}
}
源码下载:http://download.csdn.net/detail/kochenmiao/9390281
安卓仿蘑菇街瀑布流demo相关推荐
- ajax php瀑布流数据库,原生ajax瀑布流demo实例分享
本文主要为大家带来一篇原生ajax瀑布流demo分享(必看篇).小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧,希望能帮助到大家. 简单分为三个文档,有详细的注释:img ...
- 瀑布流ajax思路步骤,原生ajax瀑布流demo分享(必看篇)
最近听朋友们说起瀑布流挺多的,自己就去研究下了,一个简单的原生demo,分享给大家... 简单分为三个文档,有详细的注释:img:ajax.php:demo.php 其中img文件夹中放入图片 1.j ...
- jQuery图片瀑布流demo效果示例(整理)jQuery瀑布流效果
效果图: <!DOCTYPE html> <html lang="zh"><head><meta charset="UTF-8& ...
- android 仿商城瀑布流,android_waterfall
Android瀑布流实例 此项目由于最初设计问题,导致现在问题比较多,暂时停止维护. 我现在在其他类似的瀑布流上进行完善开发, ####请关注: 有必要解释一下程序为什么采用addview方式而不是做 ...
- html5实现瀑布流效果
先给出极客学院的一个瀑布流小例程:瀑布流demo.这是个实现瀑布流很典型的demo.这个例子的实现思路是这样的:父级元素container采用相对定位,子元素container使用绝对定位,用函数im ...
- 微信小程序中如何制作瀑布流效果
微信小程序中如何制作瀑布流效果如图是 首先,页面布局,页面分为两个部分,左右各一部分,各自插入图片,页面代码如下: <view class="city_history"> ...
- android瀑布流效果(仿蘑菇街)
Android 转载分享(10) 我们还是来看一款示例:(蘑菇街) 看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此. ...
- 【jQuery Demo】图片瀑布流实现
瀑布流就是像瀑布一样的网站--丰富的网站内容,特别是绚美的图片会让你流连忘返.你在浏览网站的时候只需要轻轻滑动一下鼠标滚轮,一切的美妙的图片精彩便可呈现在你面前.瀑布流网站是新兴的一种网站模式--她的 ...
- 瀑布流插件|jquery.masonry|使用demo
Maonsry+Infinite-Scroll实现滚动式分页,网上有很多,这里只说: 瀑布流插件的一个基本使用,附上基本功能的demo <html> <head> <me ...
最新文章
- 一文搞懂select语句在MySQL中的执行流程!
- Docker渐入佳境
- PHP-Wakeup魔术漏洞骚操作
- Notepad++如何编译、运行Java
- iOS开发(8)UISwitch
- 理解大型分布式网站你必须知道这些概念
- 使用Spring Cloud HystrixCommands的功能Hystrix
- 解决 Unable to translate SQLException with Error code ‘17059‘, will now try the fallback translator
- DecimalFormat的用法
- 何恺明!再斩ICCV 2017最佳论文
- oracle 时间戳
- Flex3 CRUD 与Java后台交互 完整Demo
- Pandas——如何更改DataFrame中的值
- React怎样从函数中辨别类
- TwinCAT3中台达A3伺服使用
- 示波器抓取RC663身份证的天线耦合波形
- input/output is not in graph tf.layers.conv2d在name命名时会自动在其后添加Conv2D
- NFT周刊|Jay-Z拍卖“Reasonable Doubt”NFT;漫威推出NFT藏品
- bc伐木机器人_1+X | 工业机器人操作编程平台(HB-CZBC-C10)
- vue页面背景颜色修改