Android使用控件ImageView加載圖片的方法
在 Android 加載圖片一般使用 ImageView,這里簡(jiǎn)單記錄一下這個(gè)控件的使用方法。
最簡(jiǎn)單就是在 xml 里直接使用 ImageView 標(biāo)簽:
<?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);
}
構(gòu)建ImageView對(duì)象時(shí)傳遞了一個(gè)this參數(shù),表示與當(dāng)前上下文(context)關(guān)聯(lián)。這個(gè)Context由系統(tǒng)處理,它提供諸如資源解析、獲取訪問數(shù)據(jù)庫(kù)和偏好等服務(wù)。因?yàn)锳ctivity類繼承自Context,且因?yàn)槟愕腍elloWorld類是Activity的子類,它也是一個(gè)Context。因此,你可以傳遞this作為你的Context給ImageView引用。
Android ImageView如何加載網(wǎng)絡(luò)圖片資源,代碼也分享給大家:
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 {
//定義一個(gè)圖片顯示控件
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);
}
/**
* 獲取網(wǎng)落圖片資源
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url){
URL myFileURL;
Bitmap bitmap=null;
try{
myFileURL = new URL(url);
//獲得連接
HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
//設(shè)置超時(shí)時(shí)間為6000毫秒,conn.setConnectionTiem(0);表示沒有時(shí)間限制
conn.setConnectTimeout(6000);
//連接設(shè)置獲得數(shù)據(jù)流
conn.setDoInput(true);
//不使用緩存
conn.setUseCaches(false);
//這句可有可無(wú),沒有影響
//conn.connect();
//得到數(shù)據(jù)流
InputStream is = conn.getInputStream();
//解析得到圖片
bitmap = BitmapFactory.decodeStream(is);
//關(guān)閉數(shù)據(jù)流
is.close();
}catch(Exception e){
e.printStackTrace();
}
return bitmap;
}
}
以上就是本文的全部?jī)?nèi)容,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)仿iOS菊花加載圈動(dòng)畫效果
- Android繪制圓形百分比加載圈效果
- Android自定義加載圈動(dòng)畫效果
- android動(dòng)態(tài)加載布局文件示例
- Android使用glide加載gif動(dòng)畫設(shè)置播放次數(shù)
- Android實(shí)現(xiàn)跳動(dòng)的小球加載動(dòng)畫效果
- Android中Glide加載圓形圖片和圓角圖片實(shí)例代碼
- Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
- Android通用LoadingView加載框架詳解
- Android實(shí)現(xiàn)加載圈
相關(guān)文章
Android 多線程的實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android 多線程的實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,這里提供三種方法,幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下2017-08-08
詳解android項(xiàng)目由Gradle 2.2 切換到 3.0的坑
本篇文章主要介紹了詳解android項(xiàng)目由Gradle 2.2 切換到 3.0的坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-02-02
Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
本篇文章主要介紹了Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09
Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解
下面小編就為大家分享一篇Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-01-01
Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)自定義倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
DCloud的native.js調(diào)用系統(tǒng)分享實(shí)例Android版代碼
本文為大家分享了DCloud的native.js如何調(diào)用系統(tǒng)分享功能Android版的實(shí)例代碼,直接拿來(lái)就用2018-09-09

