zoukankan      html  css  js  c++  java
  • 采取Volley,实现瀑布流

    今天停止php,在研究Volley框架的源代码,实现了瀑布流的效果。

    为了实现最终的级联效应,一些需要掌握的知识:

    (1)自己定义布局,由于我们要监听滑究竟部的事件就要实现自己定义的ScrollView并通过回调函数实现监听

    (2)对Vollet框架的掌握。我们须要新建一个requestQueue队列。通过研究源代码发如今新建这个队列的时候传入对应的构造函数。然后会调整当中的start方法。有四个线程池收发请求。每一个在一个while循环中实现队列的监听。

    (3)动态布局。在自己定义的scrollView里面放一个LinearLay然后在代码里面计算每列的宽度,加入ImageView到每列的布局里面。

    自己定义的MyScrollView

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




    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    2018/12/08 L1-043 阅览室 Java
    2018/12/08 L1-042 日期格式化 Java
    breeze源码阅读心得
    Spark ML源码分析之四 树
    Spark ML源码分析之三 分类器
    Spark ML源码分析之二 从单机到分布式
    Spark ML源码分析之一 设计框架解读
    Adaboost的意义
    RBM如何训练?
    ChromeTimeline
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4840832.html
Copyright © 2011-2022 走看看