package com.cnn.asynctask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity {
private Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnButton=(Button) findViewById(R.id.button1);
btnButton.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO 自动生成的方法存根
Intent intent = new Intent(MainActivity.this, ImageTest.class);
startActivity(intent);
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

  

package com.cnn.asynctask;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar; public class ImageTest extends Activity {
private ImageView imageView;
private ProgressBar bar;
private String urlString="http://pic2.ooopic.com/01/03/51/25b1OOOPIC19.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.image); imageView=(ImageView) findViewById(R.id.image);
bar=(ProgressBar) findViewById(R.id.bar); new ImageAsync().execute(urlString);
} class ImageAsync extends AsyncTask<String, Void, Bitmap> { @Override
protected Bitmap doInBackground(String... params) {
// TODO 自动生成的方法存根
//获取传递进来的参数
String urlString=params[0];
Bitmap bitmap=null;
URLConnection connection;
InputStream inputStream;
try {
connection=new URL(urlString).openConnection();
inputStream=connection.getInputStream();
BufferedInputStream bisBufferedInputStream= new BufferedInputStream(inputStream);
//通过decodeStream方法解析输入流 转化为bitmap
bitmap=BitmapFactory.decodeStream(bisBufferedInputStream);
inputStream.close();
bisBufferedInputStream.close();
} catch (MalformedURLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
//bitmap作为返回值返回给后面调用的方法
return bitmap;
} @Override
protected void onPreExecute() {
// TODO 自动生成的方法存根
super.onPreExecute();
bar.setVisibility(View.VISIBLE);
} @Override
protected void onPostExecute(Bitmap result) {
// TODO 自动生成的方法存根
super.onPostExecute(result);
bar.setVisibility(View.GONE);
imageView.setImageBitmap(result);
}
}
}

  

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cnn.asynctask.MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Button" /> </RelativeLayout>

  

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="16dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
android:id="@+id/bar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>

  

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cnn.asynctask"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/> <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> <activity
android:name=".ImageTest"
android:label="@string/app_name" >
</activity>
</application> </manifest>

  

安卓开发--AsyncTask的更多相关文章

  1. 安卓开发30:AsyncTask的用法

    http://blog.csdn.net/initphp/article/details/10392093 安卓开发笔记系列(43)  在开发Android应用时必须遵守单线程模型的原则: Andro ...

  2. 安卓开发笔记——关于AsyncTask的使用

    在安卓开发中,我们经常要进行一些耗时操作,比如数据库操作,获取网络资源,读取内存文件等等,当我们在处理这些耗时操作的时候,如果我们直接在UI主线程进行,那么可能会导致阻塞UI主线程,使得UI界面卡顿, ...

  3. 安卓开发笔记——关于照片墙的实现(完美缓存策略LruCache+DiskLruCache)

    这几天一直研究在安卓开发中图片应该如何处理,在网上翻了好多资料,这里做点小总结,如果朋友们有更好的解决方案,可以留言一起交流下. 内存缓存技术 在我们开发程序中要在界面上加载一张图片是件非常容易的事情 ...

  4. 学习安卓开发[5] - HTTP、后台任务以及与UI线程的交互

    在上一篇学习安卓开发[4] - 使用隐式Intent启动短信.联系人.相机应用中了解了在调用其它应用的功能时隐式Intent的使用,本次基于一个图片浏览APP的开发,记录使用AsyncTask在后台执 ...

  5. 安卓开发笔记——关于Handler的一些总结(上)

    接上篇文章<安卓开发笔记——关于AsyncTask的使用>,今天来讲下在安卓开发里"重中之重"的另一个异步操作类Handler. 今天打算先讲下关于Handler的一些 ...

  6. 安卓中AsyncTask的基本使用

    安卓中AsyncTask的基本使用 使用场景介绍 在安卓开发中,我们经常需要访问互联网资源,这些访问是都需要在后台线程中去完成的,因为安卓的UI线程不允许执行耗时任务.然而,后台线程是不可以修改安卓的 ...

  7. 基于eclipse-java的平台上搭建安卓开发环境

    首先感谢好人的分享!http://www.mamicode.com/info-detail-516839.html 系统:windows 7 若想直接安装eclipse—android的,请启动如下传 ...

  8. 关于安卓开发当中通过java自带的HttpURLConnection访问XML的java.io.EOFException问题

    刚接触安卓开发,试着写个小程序熟悉下,就写了天气预报的小程序,通过httpUrlConnection读流的方式来获取网络公共接口提供的天气XML信息.但在建立http连接时一直报java.io.EOF ...

  9. Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境

    我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...

随机推荐

  1. Spring+mybatis+struts框架整合的配置具体解释

    学了非常久的spring+mybatis+struts.一直都是单个的用他们,或者是两两组合用过,今天总算整合到一起了,配置起来有点麻烦.可是配置完一次之后.就轻松多了,那么框架整合配置具体解释例如以 ...

  2. POJ-1785-Binary Search Heap Construction(笛卡尔树)

    Description Read the statement of problem G for the definitions concerning trees. In the following w ...

  3. 安卓ProgressBar水平进度条的颜色设置

    安卓系统提供了水平进度条ProgressBar的样式,而我们在实际开发中,差点儿不可能使用默认的样式.原因就是"太丑"^_^ 所以我们在很多其它的时候须要对其颜色进行自己定义,主要 ...

  4. 基于One-Class的矩阵分解方法

    在矩阵分解中. 有类问题比較常见,即矩阵的元素仅仅有0和1. 相应实际应用中的场景是:用户对新闻的点击情况,对某些物品的购买情况等. 基于graphchi里面的矩阵分解结果不太理想.调研了下相关的文献 ...

  5. 云server之间实时文件同步和文件备份的最简单高效的免费方案

     分布于不同云计算中心的多台云server,通常须要进行文件同步.以满足业务的须要. 传统的文件同步方案,部署繁琐.同步实时性差.无法令人惬意. 端端Clouduolc,一款纯p2p方式的文件实时 ...

  6. 十分钟掌握diff&patch用法

    作为程序员,了解diff&patch命令是非常必要的.比如说我们发现某个项目有bug代码,而自己又没有svn的提交权限,那么此时最合适的解决方法就是用diff命令做一个补丁发给项目成员.项目成 ...

  7. String转换成int型

    private boolean judge(String str){ int year = 0; try{ year = Integer.valueOf(str).intValue(); }catch ...

  8. cell的重用

    cell的重用 简单来说,就是为了节省内存,系统通过一个重用的表示进行获取重用的控件 1 定义重用的标识 NSString * reuseId = @"hero"  //这里的he ...

  9. CDN(Content Distribution Network)概念

    CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节 ...

  10. POJ 2431 Expedition (priority_queue或者multiset可解)

    Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18655   Accepted: 5405 Descr ...