Simple Android waterfall stream implementation

Source: Internet
Author: User

Recently want to get examples of Android waterfall stream, online looking for some examples, I feel too much trouble https://github.com/dodola/android_waterfall

So I wrote

The most obvious feature of the so-called waterfall stream is that the width of each stream is fixed, so you only need to use a few linearlayout to implement it.

Below is a waterfall with three streams:

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);                }        }

Layout File

<?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>

The implementation method is simple. Just add the instantiated imageview to layout.

Source code http://download.csdn.net/detail/samguoyi/4423302 on

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.