Android使用URLConnection显示网络图片
1. 代码
package android.test; 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.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; public class Main extends Activity { private Gallery m_gallery; private String[] m_imageURLs = { "http://www.lvyou114.com/member/1971/UploadFile/200691217738739.jpg", "http://img5.3lian.com/gaoqing/01/02/185.jpg", "http://www.wallcoo.com/nature/Fall_Colors/images/1Fall_Colors_07.jpg", "http://pic1a.nipic.com/2008-10-15/2008101585324870_2.jpg", "http://www.zjj-hc.com/view/jp/%E5%BC%A0%E5%AE%B6%E7%95%8C%E6%99%AF%E8%89%B2.gif" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); m_gallery = (Gallery) findViewById(R.id.myGallery); m_gallery.setAdapter(new MyInternetGalleryAdapter(Main.this)); } class MyInternetGalleryAdapter extends BaseAdapter { private Context m_context; private int m_nGalleryItemBG; public MyInternetGalleryAdapter(Context c) { m_context = c; TypedArray arr = m_context.obtainStyledAttributes(R.styleable.myGalleryStyleable); m_nGalleryItemBG = arr.getResourceId(R.styleable.myGalleryStyleable_android_galleryItemBackground, 0); arr.recycle(); } @Override public int getCount() { return m_imageURLs.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imgView = new ImageView(m_context); try { URL url = new URL(m_imageURLs[position]); URLConnection conn; conn = url.openConnection(); conn.connect(); InputStream in = conn.getInputStream(); Bitmap bmp = BitmapFactory.decodeStream(in); in.close(); imgView.setImageBitmap(bmp); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } imgView.setScaleType(ImageView.ScaleType.FIT_XY); imgView.setLayoutParams(new Gallery.LayoutParams(200, 150)); imgView.setBackgroundResource(m_nGalleryItemBG); return imgView; } /* 依据距离中央的位移量 利用getScale回传views的大小(0.0f to 1.0f) */ public float getScale(boolean focused, int offset) { /* Formula: 1 / (2 ^ offset) */ return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); } } }
2. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.test" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7"></uses-sdk> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" 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> <uses-permission android:name="android.permission.INTERNET"/> </manifest>
更多相关文章
- Android读取设备内存大小
- Android 获得屏幕大小
- android 中获取屏幕大小
- Android 获得sdcard大小与内存大小工具类
- android 网络下载获取文件大小
- Android system大小上限设置
- Android 同时播放缩放动画和位移动画 时, 位移的路径会发生偏移.
- Android中获取当前屏幕的尺寸大小