1:activity_main.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"> <Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button test1"/> <Button
android:layout_below="@id/btn_1"
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Intent Other Activity"/> <ImageView
android:layout_below="@id/btn_2"
android:id="@+id/img_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/> </RelativeLayout>

2:HttpHelper.java

public class HttpHelper {
//图片资源缓存
private static Map<String,Bitmap>bitmapCache=new HashMap<String,Bitmap>(); public static Bitmap getHttpBitmap(String url){
     //首先先从缓存中取数据
Bitmap bitmap=bitmapCache.get(url);
if(bitmap!=null){
        //如果取到就直接返回
return bitmap;
} try{
URL myUrl=new URL(url);
HttpURLConnection conn=(HttpURLConnection)myUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is=conn.getInputStream();
bitmap=BitmapFactory.decodeStream(is);
is.close();
}catch(Exception e){
e.printStackTrace();
} if(bitmap!=null){
        //将获取到的图片缓存起来
bitmapCache.put(url, bitmap);
} return bitmap;
}
}

3:MainActivity.java

public class MainActivity extends Activity {
private Button btnTest1=null;
private Button btnTest2=null;
private ImageView imgView=null;
private Bitmap bitmap=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initUI(); btnTest1.setOnClickListener(new OnClickListener(){
public void onClick(View view){
new Thread(new GetImgThread()).start();
}
}); btnTest2.setOnClickListener(new OnClickListener(){
public void onClick(View view){
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
} private void initUI(){
btnTest1=(Button)findViewById(R.id.btn_1);
btnTest2=(Button)findViewById(R.id.btn_2);
imgView=(ImageView)findViewById(R.id.img_view);
} Handler myHandler=new Handler(){
public void handleMessage(Message msg){
imgView.setImageBitmap(bitmap);
}
}; class GetImgThread implements Runnable{
public void run(){
String url="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Hukou_Waterfall.jpg/800px-Hukou_Waterfall.jpg";
bitmap=HttpHelper.getHttpBitmap(url);
myHandler.obtainMessage().sendToTarget();
}
}
}

4:activity_other.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" > <Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Get Img"/> <ImageView
android:id="@+id/img_view_2"
android:layout_below="@id/btn_get"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

5:OtherActivity.java

public class OtherActivity extends Activity {
private Bitmap bitmap=null;
private Button btnGetImg=null;
private ImageView imgView=null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other); btnGetImg=(Button)findViewById(R.id.btn_get);
imgView=(ImageView)findViewById(R.id.img_view_2); btnGetImg.setOnClickListener(new OnClickListener(){
public void onClick(View view){
new Thread(new GetImgThread()).start();
}
});
} Handler myHandler=new Handler(){
public void handleMessage(Message msg){
imgView.setImageBitmap(bitmap);
}
}; class GetImgThread implements Runnable{
public void run(){
String url="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Hukou_Waterfall.jpg/800px-Hukou_Waterfall.jpg";
bitmap=HttpHelper.getHttpBitmap(url);
myHandler.obtainMessage().sendToTarget();
}
} }

6:运行结果如下:

最新文章

  1. Npoi导入导出Excel操作
  2. web 页面上纯js实现按钮倒计数功能
  3. javascript判断某种元素是否进入可视区域
  4. object.assign()方法的使用
  5. Linux中启动和停止jar包的运行
  6. flex经验记录(转载)
  7. delphi 窗口最大化后控件的大小变化怎么设置
  8. 1064: [Noi2008]假面舞会 - BZOJ
  9. Hadoop分布式文件系统(HDFS)详解
  10. WifiDog and OpenWrt
  11. wxWidgets刚開始学习的人导引(4)——wxWidgets学习资料及利用方法指导
  12. 我的Spring学习记录(三)
  13. 19款Windows实用软件推荐,满满的干货,总有一款是你必备的
  14. php插入上万条mysql数据最快的方法
  15. 一: Docker的概念
  16. Java基础四(switch、数组、)
  17. linux 基本操作centos7
  18. 关于 URL 编码及 JavaScript 编码函数【转载+整理】
  19. CF573C Bear and Drawing 构造+树论
  20. apt-get强制使用Ipv4

热门文章

  1. C - Courses - hdu 1083(模板)
  2. Ubuntu 用户及组管理
  3. SOAP详解
  4. 简单的jQuery日期选择
  5. 浅谈Manacher算法与扩展KMP之间的联系
  6. Android 物理按键
  7. HDU 3791 二叉搜索树 题解
  8. Android 百度地图 SDK v3.0.0 (二) 定位与结合方向传感器
  9. 逆拓扑排序 HDU2647Reward
  10. MediaPlayer 音频播放 示例