編輯:Android開發實例
測試環境為Adnroid 2.1以上。
1:AndroidManifest.xml 權限配置:
添加互聯網訪問權限:
<uses-permission android:name="android.permission.INTERNET" />
2.異步圖片類 ImageDownloadTask
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.MalformedURLException;
- import java.net.URL;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.widget.ImageView;
- public class ImageDownloadTask extends AsyncTask<Object, Object, Bitmap> {
- private ImageView imageView = null;
- /***
- * 這裡獲取到手機的分辨率大小
- * */
- public void setDisplayWidth(int width) {
- _displaywidth = width;
- }
- public int getDisplayWidth() {
- return _displaywidth;
- }
- public void setDisplayHeight(int height) {
- _displayheight = height;
- }
- public int getDisplayHeight() {
- return _displayheight;
- }
- public int getDisplayPixels() {
- return _displaypixels;
- }
- private int _displaywidth = 480;
- private int _displayheight = 800;
- private int _displaypixels = _displaywidth * _displayheight;
- @Override
- protected Bitmap doInBackground(Object... params) {
- // TODO Auto-generated method stub
- Bitmap bmp = null;
- imageView = (ImageView) params[1];
- try {
- String url = (String) params[0];
- bmp = getBitmap(url, _displaypixels,true);
- } catch (Exception e) {
- return null;
- }
- return bmp;
- }
- protected void onPostExecute(Bitmap result) {
- if (imageView != null&&result!=null) {
- imageView.setImageBitmap(result);
- if (null != result && result.isRecycled() == false)
- System.gc();
- }
- }
- /**
- * 通過URL獲得網上圖片。如:http://www.xxxxxx.com/xx.jpg
- * */
- public Bitmap getBitmap(String url, int displaypixels, Boolean isBig) throws MalformedURLException, IOException {
- Bitmap bmp = null;
- BitmapFactory.Options opts = new BitmapFactory.Options();
- InputStream stream = new URL(url).openStream();
- byte[] bytes = getBytes(stream);
- //這3句是處理圖片溢出的begin( 如果不需要處理溢出直接 opts.inSampleSize=1;)
- opts.inJustDecodeBounds = true;
- BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
- opts.inSampleSize = computeSampleSize(opts, -1, displaypixels);
- //end
- opts.inJustDecodeBounds = false;
- bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, opts);
- return bmp;
- }
- /**
- * 數據流轉成btyle[]數組
- * */
- private byte[] getBytes(InputStream is) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] b = new byte[2048];
- int len = 0;
- try {
- while ((len = is.read(b, 0, 2048)) != -1) {
- baos.write(b, 0, len);
- baos.flush();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- byte[] bytes = baos.toByteArray();
- return bytes;
- }
- /****
- * 處理圖片bitmap size exceeds VM budget (Out Of Memory 內存溢出)
- */
- private int computeSampleSize(BitmapFactory.Options options,
- int minSideLength, int maxNumOfPixels) {
- int initialSize = computeInitialSampleSize(options, minSideLength,
- maxNumOfPixels);
- int roundedSize;
- if (initialSize <= 8) {
- roundedSize = 1;
- while (roundedSize < initialSize) {
- roundedSize <<= 1;
- }
- } else {
- roundedSize = (initialSize + 7) / 8 * 8;
- }
- return roundedSize;
- }
- private int computeInitialSampleSize(BitmapFactory.Options options,
- int minSideLength, int maxNumOfPixels) {
- double w = options.outWidth;
- double h = options.outHeight;
- int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
- .sqrt(w * h / maxNumOfPixels));
- int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
- Math.floor(w / minSideLength), Math.floor(h / minSideLength));
- if (upperBound < lowerBound) {
- return lowerBound;
- }
- if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
- return 1;
- } else if (minSideLength == -1) {
- return lowerBound;
- } else {
- return upperBound;
- }
- }
- }
3.測試調用代碼:
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ImageDownloadTask imgtask =new ImageDownloadTask();
- /**這裡是獲取手機屏幕的分辨率用來處理 圖片 溢出問題的。begin*/
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- imgtask.setDisplayWidth(dm.widthPixels);
- imgtask.setDisplayHeight(dm.heightPixels);
- //end
- ImageView imageView_test= (ImageView)findViewById(R.id.imageView_test);
- imgtask.execute("http://pic.test.com/big/7515/201201031116491.jpg",imageView_test);
- }
4.小結:
主要是通過 extends AsyncTask<Object, Object, Bitmap> 來實現異步的。
圖片Out Of Memory 內存溢出 這一塊操作,在實際應用中應該考慮淡定抽取出來。這裡為了方便放進來了。 溢出處理實際上就是獲得設備分辨率把圖片進行壓縮。
轉自:http://www.cnblogs.com/yeqw1985/archive/2013/02/06/2908190.html
今天記錄一下TextView的倒影效果,顯示一串文字,然後在文字的下方顯示出它的倒影,先上效果圖: 最重要的就是View中getDrawingCache()方法
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
本文實例講述了android編程實現懸浮窗體的方法。分享給大家供大家參考,具體如下: 突然對懸浮窗體感興趣,查資料做了個小Demo,效果是點擊按鈕後,關閉當前Ac
引言在windows安裝Android的開發環境不簡單也說不上算復雜,本文寫給第一次想在自己Windows上建立Android開發環境投入Android浪潮的朋友