特別簡單的android瀑布流實現

來源:互聯網
上載者:User

最近想弄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.csdn.net/detail/samguoyi/4423302

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.