Principles and Examples of android waterfall stream

Source: Internet
Author: User

Recently, it was found that the waterfall stream is very popular, so I studied it and found that the waterfall stream is actually very simple and not that complicated. Read the code first and then explain it again.

First look at the figure

<?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" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="horizontal" >        <LinearLayout            android:id="@+id/layout01"                android:orientation="vertical"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1" >        </LinearLayout>        <LinearLayout            android:id="@+id/layout02"            android:orientation="vertical"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:layout_weight="1" >        </LinearLayout>    </LinearLayout></ScrollView>

You can see clearly here that there is a ScrollView and two LinearLayout, And the outermost layer of linearlayout does not need to be taken care. As you may understand, the waterfall stream I want to do this time has only two columns. The role of ScrollView is to make this waterfall scroll!

Next let's look at the code

The Code has no difficulty, so I will not explain it. Let's see for yourself. I uploaded the complete instance code.

Package com. pb. main; import java. io. IOException; import java. io. inputStream; import java.net. httpURLConnection; import java.net. malformedURLException; import java.net. URL; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android. OS. asyncTask; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup. layoutParams; import android. widget. imageView; import android. widget. linearLayout; import android. widget. toast; public class TestPbActivity extends Activity {/** Called when the activity is first created. */private LinearLayout layout01; private LinearLayout layout02; private Drawable [] drawables; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); layout01 = (LinearLayout) findViewById (R. id. layout01); layout02 = (LinearLayout) findViewById (R. id. layout02); String [] imagePathStr = {"http://www.cf69.com/Upfiles/BeyondPic/2010-08/20108175740983313.jpg", "http://www.syfff.com/UploadFile/pic/2008122163204.jpg", "http://pic.newssc.org/0/10/34/32/10343297_564251.jpg", "http://ent.hangzhou.com.cn/images/20090311/zym2009031323.jpg", "http://a4.att.hudong.com/86/60/01300000013093119087608457965.jpg", "http://file.sdteacher.gov.cn/upload/gz0901/images/0907/22/110437191.jpg", "http://www.fun9.cn/uploadfile/starpic/uploadpics/200910/20091008090155126.jpg ", "http://img3.yxlady.com/yl/UploadFiles_5361/20110820/20110820120609469.jpg", "http://news.sznews.com/content/images/site3/20070827/001558d90baa083c6da20d.jpg", "http://henan.sinaimg.cn/cr/2010/0824/2297073692.jpg", "http://www.cf69.com/Upfiles/BeyondPic/2010-08/20108175740983313.jpg", "http://www.syfff.com/UploadFile/pic/2008122163204.jpg", "http://pic.newssc.org/0/10/34/32/10343297_564251.jpg", "http://ent.hangzhou.com.cn/images/20090311/zym2009031323.jpg", "http://a4.att.hudong.com/86/60/01300000013093119087608457965.jpg", "http://file.sdteacher.gov.cn/upload/gz0901/images/0907/22/110437191.jpg", "http://www.fun9.cn/uploadfile/starpic/uploadpics/200910/20091008090155126.jpg ", "http://img3.yxlady.com/yl/UploadFiles_5361/20110820/20110820120609469.jpg", "http://news.sznews.com/content/images/site3/20070827/001558d90baa083c6da20d.jpg", "http://henan.sinaimg.cn/cr/2010/0824/2297073692.jpg"}; // new loadimageviews(cmd.exe cute (imagePathStr); int j = 0; for (int I = 0; I <imagePathStr. length; I ++) {addBitMapToImage (imagePathStr [I], j, I); j ++; if (j> = 2) {j = 0 ;}}} // Add the imageview to layout public void addBitMapToImage (String imagePath, int j, int I) {ImageView imageView = new ImageView (this); imageView. setLayoutParams (new LayoutParams (LayoutParams. FILL_PARENT, LayoutParams. WRAP_CONTENT); new ImageDownLoadAsyncTask (this, imagePath, imageview20..exe cute (Void) null); imageView. setTag (I); if (j = 0) {layout01.addView (imageView);} else if (j = 1) {layout02.addView (imageView);} imageView. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {Toast. makeText (TestPbActivity. this, "you clicked" + v. getTag () + "Item", Toast. LENGTH_SHORT ). show ();}});}}
/*** Download images asynchronously ** @ author sy **/public class ImageDownLoadAsyncTask extends AsyncTask <Void, Void, Drawable> {private String imagePath; private View imageView; private static final String ALBUM_PATH = "/sdcard/pb"; ProgressDialog progre; private Context context; /*** constructor ** @ param context * @ param imagePath * @ param imageView */public ImageDownLoadAsyncTask (Context context, String imageP Ath, View imageView) {this. imagePath = imagePath; this. imageView = imageView; this. context = context;} @ Override protected Drawable doInBackground (Void... params) {// TODO Auto-generated method stub String filePath = ALBUM_PATH + imagePath. substring (imagePath. lastIndexOf ("/"); File mfile = new File (filePath); if (mfile. exists () {// if the file contains Bitmap bm = BitmapFactory. decodeFile (filePath); Bitmap Drawable bd = new BitmapDrawable (bm); return bd;} else {URL url; try {url = new URL (imagePath); HttpURLConnection conn = (HttpURLConnection) url. openConnection (); conn. setDoInput (true); conn. connect (); InputStream inputStream = conn. getInputStream (); Bitmap bitmap = BitmapFactory. decodeStream (inputStream); BitmapDrawable bd = new BitmapDrawable (bitmap); saveFile (bd, imagePath. substring (imagePat H. lastIndexOf ("/"); return bd;} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} return null ;}@ Override protected void onPostExecute (Drawable drawable) {// TODO Auto-generated method stub super. onPostExecute (drawable); if (drawable! = Null) {imageView. setBackgroundDrawable (drawable);} progressDialog. dismiss () ;}@ Override protected void onPreExecute () {// TODO Auto-generated method stub super. onPreExecute (); progressDialog = ProgressDialog. show (context, "", "downloading image, please aftersales ");} /*** Save the image to the SD card ** @ param drawable * @ param fileName * @ throws IOException * @ author sy */public void saveFile (Drawable drawable, String fileName) thro Ws IOException {File dirFile = new File (ALBUM_PATH); if (! DirFile. exists () {dirFile. mkdirs ();} File myCaptureFile = new File (ALBUM_PATH + fileName); inclubos = new bytes (new FileOutputStream (myCaptureFile); BitmapDrawable bd = (BitmapDrawable) drawable; bitmap bm = bd. getBitmap (); bm. compress (Bitmap. compressFormat. JPEG, 80, bos); bos. flush (); bos. close ();}}

Ah, I don't know how to upload the blog park nearby .. Tangle.

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.