Android-ThreadPool
2015-09-25 16:54
441 查看
Executors.newFixedThreadPool(5);
具体代码如下
mainfest
layout.xml
Java code
参考原文是
http://blog.csdn.net/jie1991liu/article/details/16961701
具体代码如下
mainfest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.inctech.hello" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <ImageView android:id="@+id/iv1" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="20dp" android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> <ImageView android:id="@+id/iv2" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_alignLeft="@+id/iv1" android:layout_below="@+id/iv1" android:layout_marginTop="20dp" android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> <ImageView android:id="@+id/iv3" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_alignLeft="@+id/iv1" android:layout_below="@+id/iv2" android:layout_marginTop="20dp" android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> <ImageView android:id="@+id/iv4" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_alignLeft="@+id/iv1" android:layout_below="@+id/iv3" android:layout_marginTop="20dp" android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> <ImageView android:id="@+id/iv5" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_alignLeft="@+id/iv1" android:layout_below="@+id/iv4" android:layout_marginTop="20dp" android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Btn" /> </RelativeLayout>
Java code
package com.inctech.hello; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { private Button bt; private static Handler mainHandler = new Handler(){ @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); } }; ExecutorService service = Executors.newFixedThreadPool(5); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); } // private void testThread0() { // Handler handler = new Handler(); // for (int i = 0; i < 3; i++) { // handler.post(new Runnable() { // @Override // public void run() { // Log.i("testThread0", Thread.currentThread().getName()); // } // }); // } // handler = null; // } private void initViews(){ bt = (Button)findViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/23c1625aca99f02c50d8e510383a34e7.jpg",R.id.iv1); loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/c4698d97ef6d10722c8e917733c7beb3.jpg",R.id.iv2); loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/f332ffe433be2a3112be15f78bff5a40.jpg",R.id.iv3); loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/6ff8a9c647a1e80bc602eeda48865d4c.jpg",R.id.iv4); loadImagesByExecutors("http://news.baidu.com/z/resource/r/image/2012-11-23/f104d069f7443dca52a878d779392874.jpg",R.id.iv5); } }); } private void loadImagesByExecutors(final String url,final int id){ service.submit(new Runnable(){ @Override public void run() { Log.i("当前线程:", ""+Thread.currentThread().getName()); try { final Drawable drawable = Drawable.createFromStream(new URL(url).openStream(), "image.gif"); mainHandler.post(new Runnable(){ @Override public void run() {//这将在主线程运行 ((ImageView)MainActivity.this.findViewById(id)).setImageDrawable(drawable); } }); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } }
参考原文是
http://blog.csdn.net/jie1991liu/article/details/16961701
相关文章推荐
- ADT和Android SDK的安装
- Android基础入门教程——2.4.13 ViewFlipper(翻转视图)的基本使用
- android学习
- Appium Android Bootstrap控制源代码的分析AndroidElement
- 【转载】android中如何显示图片局部或者不同区域
- Cursor
- Android 如何直播RTMP流
- 50 Android Hacks(hack 14)
- (转)Android之值得开发者(安卓)必备的十八大开发资源
- Android 设置background(seletor)无效
- android wear应用开发
- android继承一个布局文件实现自定义控件
- Android四大组件之--BroadcastReceiver,监测手机电量变化
- Android性能专项测试之Heap Viewer工具
- Android中LayoutParams总结和用法
- Android Lollipop 新特性 - Palette
- android 软键盘设置
- android点击屏幕空白处隐藏软键盘
- android 电容屏(三):驱动调试之驱动程序分析篇
- android annotation(二)