Android使用控件ImageView加载图片的方法

所属分类: 软件编程 / Android 阅读数: 1976
收藏 0 赞 0 分享

在 Android 加载图片一般使用 ImageView,这里简单记录一下这个控件的使用方法。

最简单就是在 xml 里直接使用 ImageView 标签:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >  
<ImageView
 android:id="@+id/iv"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:src="@drawable/welcome"
/>
</LinearLayout>

如果不想在 xml 里,也可以在程序里面加载。比如:

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 //setContentView(R.layout.activity_main);
  
 ImageView welcome = new ImageView(this);
 welcome.setImageResource(R.drawable.welcome);
 setContentView(welcome);
}

构建ImageView对象时传递了一个this参数,表示与当前上下文(context)关联。这个Context由系统处理,它提供诸如资源解析、获取访问数据库和偏好等服务。因为Activity类继承自Context,且因为你的HelloWorld类是Activity的子类,它也是一个Context。因此,你可以传递this作为你的Context给ImageView引用。

Android ImageView如何加载网络图片资源,代码也分享给大家:

package com.android.antking.imageview; 
 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
 
public class MainActivity extends Activity { 
  //定义一个图片显示控件 
  private ImageView imageView; 
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //图片资源 
    String url = "/orignal/89429f6dhb99b4903ebcf&690"; 
    //得到可用的图片 
    Bitmap bitmap = getHttpBitmap(url); 
    imageView = (ImageView)this.findViewById(R.id.imageViewId); 
    //显示 
    imageView.setImageBitmap(bitmap); 
     
  } 
  /** 
   * 获取网落图片资源 
   * @param url 
   * @return 
   */ 
  public static Bitmap getHttpBitmap(String url){ 
    URL myFileURL; 
    Bitmap bitmap=null; 
    try{ 
      myFileURL = new URL(url); 
      //获得连接 
      HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection(); 
      //设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制 
      conn.setConnectTimeout(6000); 
      //连接设置获得数据流 
      conn.setDoInput(true); 
      //不使用缓存 
      conn.setUseCaches(false); 
      //这句可有可无,没有影响 
      //conn.connect(); 
      //得到数据流 
      InputStream is = conn.getInputStream(); 
      //解析得到图片 
      bitmap = BitmapFactory.decodeStream(is); 
      //关闭数据流 
      is.close(); 
    }catch(Exception e){ 
      e.printStackTrace(); 
    } 
     
    return bitmap; 
     
  } 
} 

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多