首页

android 加载网络轮播图片 示例源码

java

2020-6-30

package com.example.downloadpictures;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.example.downloadpictures.object.WebServiceItem;
import com.example.downloadpictures.utils.Utils;

public class MainActivity extends Activity {
	private final String HOST_ADDRESS = "http://122.114.20.230:8888";
	private final String RESOURCE_ADDRESS = "/DiaoLan/android/ad/list.jsp";
	private final String SERVER_ADDRESS = HOST_ADDRESS   RESOURCE_ADDRESS;
	private boolean loopPlayState = false;
	private List<WebServiceItem> mData = null;
	private List<ImageView> mImageViewList = null;
	private List<View> mViewList = null;
	private TextView mArticleTitle = null;
	private ViewPager mViewPager = null;
	private MyPagerAdapter adapter = null;
	private WebView mWebView;// webView调用
	private LinearLayout mCustomSpace = null;
	private Handler mHandler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		this.initWidget();
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		mViewPager = (ViewPager) this.findViewById(R.id.viewpager);
		mArticleTitle = (TextView) this.findViewById(R.id.article_title);
		mCustomSpace = (LinearLayout) this.findViewById(R.id.custom_space);
		mHandler = new Handler();
		mData = new ArrayList<WebServiceItem>();
		mImageViewList = new ArrayList<ImageView>();
		mViewList = new ArrayList<View>();
		adapter = new MyPagerAdapter();
		loadData();
		mViewPager.setAdapter(adapter);

		// 设置一个监听器,当ViewPager中的页面改变时调用
		mViewPager.setOnPageChangeListener(new MyPageChangeListener());
	}

	private void loadData() {
		// TODO Auto-generated method stub
		new DownLoad(null).execute(SERVER_ADDRESS);
	}

	private final class ClickListener implements OnClickListener {

		private int mPosition;

		public ClickListener() {
			// TODO Auto-generated constructor stub
		}

		public ClickListener(int mPosition) {
			this.mPosition = mPosition;
		}

		@Override
		public void onClick(View v) {
			if (v instanceof ImageView) {
				String url = mData.get(mPosition).getmArticleUrl();
				Toast.makeText(MainActivity.this, url, 1).show();
			} else {
				return;
			}

		}
	}

	private final class MyPagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mImageViewList.size();
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			// TODO Auto-generated method stub
			((ViewPager) container).removeView(mImageViewList.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// TODO Auto-generated method stub
			ImageView temp = mImageViewList.get(position);
			new DownLoad(temp).execute(mData.get(position).getmPictureUrl());
			((ViewPager) container).addView(temp);
			return temp;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

	}

	private final class DownLoad extends AsyncTask<String, Void, Bitmap> {

		private ImageView mImageView;

		public DownLoad(ImageView imageView) {
			mImageView = imageView;
		}

		@Override
		protected Bitmap doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				if (params[0].equals(SERVER_ADDRESS)) {
					String json = (String) (Utils.getData(params[0],
							String.class));
					mData = Utils.fromJson(json);
					mImageViewList.clear();
					Log.e("图片地址", mData.get(0).getmPictureUrl());
					Log.e("还原", Utils.toJson(mData));
				} else {
					Bitmap bitmap = (Bitmap) (Utils.getData(params[0],
							Bitmap.class));
					return bitmap;
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return null;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null) {
				mImageView.setImageBitmap(result);
				if (!loopPlayState) {
					mArticleTitle.setText(mData.get(0).getmTitle());
					mViewPager.setCurrentItem(0);
					mHandler.postDelayed(loopPlay, 3000);
					loopPlayState = true;
				}
			} else {
				for (int i = 0; i < mData.size(); i  ) {
					Log.e("JRSEN-MDATA", mData.size()   "");
					ImageView imageView = new ImageView(MainActivity.this);
					imageView.setLayoutParams(new LayoutParams(
							LayoutParams.MATCH_PARENT,
							LayoutParams.MATCH_PARENT));
					imageView.setOnClickListener(new ClickListener(i));
					mImageViewList.add(imageView);
					View view = new View(MainActivity.this);
					LayoutParams layoutParams = new LayoutParams(14, 14);
					layoutParams.setMargins(3, 0, 3, 0);
					view.setLayoutParams(layoutParams);
					view.setBackgroundResource(R.drawable.dot_normal);
					mCustomSpace.addView(view);
					mViewList.add(view);
				}
				adapter.notifyDataSetChanged();
			}

		}

	}

	/**
	 * 当ViewPager中页面的状态发生改变时调用
	 * 
	 * @author Administrator
	 * 
	 */
	private class MyPageChangeListener implements OnPageChangeListener {

		private int historyPosition = 0;

		/**
		 * 当ViewPager中页面的状态发生改变时调用
		 */
		public void onPageSelected(int position) {
			mArticleTitle.setText(mData.get(position).getmTitle());
			mViewList.get(historyPosition).setBackgroundResource(
					R.drawable.dot_normal);
			mViewList.get(position).setBackgroundResource(
					R.drawable.dot_focused);
			historyPosition = position;
		}

		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}
	}

	Runnable loopPlay = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			int position = mViewPager.getCurrentItem();
			if (position == (mData.size() - 1)) {
				position = 0;
				mViewPager.setCurrentItem(position);
			} else {
				mViewPager.setCurrentItem(  position);
			}
			P.error("position thread"   position   "mData"   mData.size());
			mHandler.postDelayed(loopPlay, 3000);
		}
	};

	/**
	 * 该类为测试类
	 * 
	 * @author Jrsen
	 * 
	 */
	private final static class P {
		public static void error(Object str) {
			Log.e("JRSEN", (String) str);
		}
	}

}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
DownloadPictures/.classpath , 533
DownloadPictures/.project , 852
DownloadPictures/.settings/org.eclipse.core.resources.prefs , 57
DownloadPictures/.settings/org.eclipse.jdt.core.prefs , 177
DownloadPictures/AndroidManifest.xml , 1078
DownloadPictures/bin/AndroidManifest.xml , 1078
DownloadPictures/bin/classes/com/example/downloadpictures/BuildConfig.class , 365
DownloadPictures/bin/classes/com/example/downloadpictures/MainActivity$1.class , 1749
DownloadPictures/bin/classes/com/example/downloadpictures/MainActivity$ClickListener.class , 1510
DownloadPictures/bin/classes/com/example/downloadpictures/MainActivity$DownLoad.class , 5177
DownloadPictures/bin/classes/com/example/downloadpictures/MainActivity$MyPageChangeListener.class , 1897
DownloadPictures/bin/classes/com/example/downloadpictures/MainActivity$MyPagerAdapter.class , 2281
DownloadPictures/bin/classes/com/example/downloadpictures/MainActivity$P.class , 646
DownloadPictures/bin/classes/com/example/downloadpictures/MainActivity.class , 4862
DownloadPictures/bin/classes/com/example/downloadpictures/object/WebServiceItem.class , 1122
DownloadPictures/bin/classes/com/example/downloadpictures/R$attr.class , 370
DownloadPictures/bin/classes/com/example/downloadpictures/R$dimen.class , 491
DownloadPictures/bin/classes/com/example/downloadpictures/R$drawable.class , 506
DownloadPictures/bin/classes/com/example/downloadpictures/R$id.class , 560
DownloadPictures/bin/classes/com/example/downloadpictures/R$layout.class , 433
DownloadPictures/bin/classes/com/example/downloadpictures/R$menu.class , 418
DownloadPictures/bin/classes/com/example/downloadpictures/R$string.class , 502
DownloadPictures/bin/classes/com/example/downloadpictures/R$style.class , 494
DownloadPictures/bin/classes/com/example/downloadpictures/R.class , 773
DownloadPictures/bin/classes/com/example/downloadpictures/utils/Utils$1.class , 625
DownloadPictures/bin/classes/com/example/downloadpictures/utils/Utils.class , 3023
DownloadPictures/bin/classes.dex , 907980
DownloadPictures/bin/dexedLibs/android-support-v4-d8c4c53017c4964596e46ea21f9bb10a.jar , 236049
DownloadPictures/bin/dexedLibs/gson-2.2.4-a69d98208afaf26df872a912df46a73c.jar , 70809
DownloadPictures/bin/DownloadPictures.apk , 349540
DownloadPictures/bin/jarlist.cache , 120
DownloadPictures/bin/res/crunch/drawable-hdpi/ic_launcher.png , 5964
DownloadPictures/bin/res/crunch/drawable-mdpi/ic_launcher.png , 3112
DownloadPictures/bin/res/crunch/drawable-xhdpi/ic_launcher.png , 9355
DownloadPictures/bin/res/crunch/drawable-xxhdpi/ic_launcher.png , 17889
DownloadPictures/bin/resources.ap_ , 42839
DownloadPictures/gen/com/example/downloadpictures/BuildConfig.java , 170
DownloadPictures/gen/com/example/downloadpictures/R.java , 2896
DownloadPictures/ic_launcher-web.png , 51394
DownloadPictures/libs/android-support-v4.jar , 621451
DownloadPictures/libs/gson-2.2.4.jar , 190418
DownloadPictures/proguard-project.txt , 781
DownloadPictures/project.properties , 563
DownloadPictures/res/drawable/dot_focused.xml , 230
DownloadPictures/res/drawable/dot_normal.xml , 229
DownloadPictures/res/drawable-hdpi/ic_launcher.png , 7658
DownloadPictures/res/drawable-mdpi/ic_launcher.png , 3777
DownloadPictures/res/drawable-xhdpi/ic_launcher.png , 12516
DownloadPictures/res/drawable-xxhdpi/ic_launcher.png , 24777
DownloadPictures/res/layout/activity_main.xml , 1892
DownloadPictures/res/menu/main.xml , 263
DownloadPictures/res/values/dimens.xml , 220
DownloadPictures/res/values/strings.xml , 233
DownloadPictures/res/values/styles.xml , 1043
DownloadPictures/res/values-sw600dp/dimens.xml , 203
DownloadPictures/res/values-sw720dp-land/dimens.xml , 277
DownloadPictures/res/values-v11/styles.xml , 334
DownloadPictures/res/values-v14/styles.xml , 391
DownloadPictures/src/com/example/downloadpictures/MainActivity.java , 7585
DownloadPictures/src/com/example/downloadpictures/object/WebServiceItem.java , 841
DownloadPictures/src/com/example/downloadpictures/utils/Utils.java , 1907
DownloadPictures/说明.htm , 4317
没有账号? 忘记密码?

社交账号快速登录