採起Volley,實現瀑布流

今天中止php,在研究Volley框架的源代碼,實現了瀑布流的效果。php

爲了實現最終的級聯效應,一些須要掌握的知識:java

(1)本身定義佈局,因爲咱們要監聽滑究竟部的事件就要實現本身定義的ScrollView並經過回調函數實現監聽android

(2)對Vollet框架的掌握。咱們需要新建一個requestQueue隊列。經過研究源代碼發現在新建這個隊列的時候傳入對應的構造函數。而後會調整當中的start方法。有四個線程池收發請求。每個在一個while循環中實現隊列的監聽。app

(3)動態佈局。在本身定義的scrollView裏面放一個LinearLay而後在代碼裏面計算每列的寬度,加入ImageView到每列的佈局裏面。框架

本身定義的MyScrollViewide

package com.fallview;

import com.android.volley.RetryPolicy;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView implements OnTouchListener {

	public MyScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	ScrollTouch scrollTouch;

	public void setScrollTouch(ScrollTouch touch) {
		scrollTouch = touch;
		this.setOnTouchListener(this);
	}

	@Override
	public boolean onTouch(View view, MotionEvent arg1) {
		// TODO Auto-generated method stub

		switch (arg1.getAction()) {
		case MotionEvent.ACTION_DOWN:

			break;
		case MotionEvent.ACTION_UP:
			if (view.getMeasuredHeight() <= getHeight() + getScrollY()) {
				scrollTouch.onButtom();
			}

			return true;

		default:
			break;
		}

		return false;
	}

	public interface ScrollTouch {

		public void onButtom();
	}

}

實現的java主類

package com.fallview;

import com.android.volley.RequestQueue;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.example.wangyitest.R;
import com.fallview.MyScrollView.ScrollTouch;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class FallImageAct extends Activity implements PicAdds {
	String[] myPics = pics;
	LinearLayout linearLayout;
	int culomWidth;
	int culNum;
	RequestQueue queue;
	int time = 1;

	class scrollListener implements ScrollTouch {

		@Override
		public void onButtom() {
			// TODO Auto-generated method stub
			intLayOne();
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.falllay);
		linearLayout = (LinearLayout) findViewById(R.id.Linearlay);
		((MyScrollView) findViewById(R.id.scroll))
				.setScrollTouch(new scrollListener());
		int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		culomWidth = (screenWidth - 4) / 3;
		culNum = myPics.length / 3;
		queue = Volley.newRequestQueue(getApplicationContext());
		iniadd3Lay();
		intLayOne();

	}

	LinearLayout.LayoutParams params;

	private void iniadd3Lay() {
		// TODO Auto-generated method stub
		layout1 = new LinearLayout(getApplicationContext());
		params = new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		layout1.setPadding(2, 2, 2, 2);
		layout1.setOrientation(LinearLayout.VERTICAL);
		layout1.setLayoutParams(new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		linearLayout.addView(layout1);

		layout2 = new LinearLayout(getApplicationContext());
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				culomWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
		layout2.setPadding(2, 2, 2, 2);
		layout2.setOrientation(LinearLayout.VERTICAL);
		layout2.setLayoutParams(new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		linearLayout.addView(layout2);

		layout3 = new LinearLayout(getApplicationContext());
		params = new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT);
		layout3.setPadding(2, 2, 2, 2);
		layout3.setOrientation(LinearLayout.VERTICAL);
		layout3.setLayoutParams(new LinearLayout.LayoutParams(culomWidth,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		linearLayout.addView(layout3);

	}

	LinearLayout layout1;
	LinearLayout layout2;
	LinearLayout layout3;

	private void intLayOne() {
		// TODO Auto-generated method stub

		time++;
		if (time > 5)
			return;

		for (int i = 0; i < culNum; i++) {
			ImageRequest imageRequest = new ImageRequest(pics[i],
					new Listener<Bitmap>() {

						@Override
						public void onResponse(Bitmap response) {
							// TODO Auto-generated method stub
							ImageView imageView = new ImageView(
									getApplicationContext());
							imageView.setLayoutParams(new LayoutParams(params));
							imageView.setImageBitmap(response);
							layout1.addView(imageView);
						}
					}, culomWidth, 0, Config.RGB_565, null);
			queue.add(imageRequest);
		}

		intLayTwo();
	}

	private void intLayTwo() {
		// TODO Auto-generated method stub

		for (int i = culNum; i < 2 * culNum; i++) {
			ImageRequest imageRequest = new ImageRequest(pics[i],
					new Listener<Bitmap>() {

						@Override
						public void onResponse(Bitmap response) {
							// TODO Auto-generated method stub
							ImageView imageView = new ImageView(
									getApplicationContext());
							imageView.setLayoutParams(new LayoutParams(params));
							imageView.setImageBitmap(response);
							layout2.addView(imageView);
						}
					}, culomWidth, 0, Config.RGB_565, null);
			queue.add(imageRequest);
		}
		intLaythree();
	}

	private void intLaythree() {
		// TODO Auto-generated method stub

		for (int i = 2 * culNum; i < pics.length; i++) {
			ImageRequest imageRequest = new ImageRequest(pics[i],
					new Listener<Bitmap>() {

						@Override
						public void onResponse(Bitmap response) {
							// TODO Auto-generated method stub
							ImageView imageView = new ImageView(
									getApplicationContext());
							imageView.setLayoutParams(new LayoutParams(params));
							imageView.setImageBitmap(response);
							layout3.addView(imageView);
						}
					}, culomWidth, 0, Config.RGB_565, null);
			queue.add(imageRequest);
		}

	}
}




版權聲明:本文博主原創文章,博客,未經贊成不得轉載。函數

相關文章
相關標籤/搜索