最近想弄android瀑布流的例子,网上找了一些例子,感觉太麻烦了https://github.com/dodola/android_waterfall
于是自己写了一个
所谓瀑布流,其实一个最明显的特点就是每个流的宽度是固定的,所以只需要用几个linearlayout就可以实现了
下面是一个三个流的瀑布:
package sam.test.waterfall;
import java.io.InputStream;
import android.R.integer;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.View;
public class WaterfallActivity extends Activity implements View.OnClickListener
{
private LinearLayout linearLayout1 = null;
private LinearLayout linearLayout2 = null;
private LinearLayout linearLayout3 = null;
private int USE_LINEAR_INTERVAL = 0;
private int linearlayoutWidth = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout1 = (LinearLayout)findViewById(R.id.main_linearlayout1);
linearLayout2 = (LinearLayout)findViewById(R.id.main_linearlayout2);
linearLayout3 = (LinearLayout)findViewById(R.id.main_linearlayout3);
linearlayoutWidth = getWindowManager().getDefaultDisplay().getWidth()/3;
addBitmaps();
}
private void addBitmaps()
{
int index =0;
try {
String filepaths[] = getResources().getAssets().list("images");
for(String string:filepaths)
{
try {
InputStream inputStream = getResources().getAssets().open("images/"+string);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Bitmap bitmap2 = BitmapZoom.bitmapZoomByWidth(bitmap, linearlayoutWidth);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(bitmap2.getWidth(), bitmap2.getHeight());
imageView.setLayoutParams(layoutParams);
imageView.setOnClickListener(this);
imageView.setTag(new Integer(index));
switch (USE_LINEAR_INTERVAL)
{
case 0:
linearLayout1.addView(imageView);
break;
case 1:
linearLayout2.addView(imageView);
break;
case 2:
linearLayout3.addView(imageView);
break;
default:
break;
}
index++;
USE_LINEAR_INTERVAL++;
USE_LINEAR_INTERVAL= USE_LINEAR_INTERVAL%3;
inputStream.close();
} catch (Exception e) {
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onClick(View v)
{
int index = (Integer)v.getTag();
System.out.println("click index= "+index);
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" android:background="#ffff8725">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:layout_weight="1" android:id="@+id/main_linearlayout1"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:layout_weight="1" android:id="@+id/main_linearlayout2"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical"
android:layout_weight="1" android:id="@+id/main_linearlayout3"/>
</LinearLayout>
</ScrollView>
实现方式很简单,将实例化imageview往layout里面添加就行
上源代码 http://download.youkuaiyun.com/detail/samguoyi/4423302