特别简单的android瀑布流实现

本文介绍了一种简单的Android瀑布流布局实现方法,通过使用多个LinearLayout来达到瀑布流的效果,并提供了完整的源代码及布局文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近想弄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


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值