安卓獲得Bitmap的幾種方式

大三學生   自己做項目的時候通過很多種方式得到Bitmap  這裏總結一下   如有不足或錯誤抓緊給建議啊

自己軟件的一個截圖  

 

中間的圓角圖片是QQ登錄後的下載下來的頭像

圓角圖片用的是BitmapShader     詳解請參考大神博客:http://blog.csdn.net/lmj623565791/article/details/41967509

圖片外圍的白色環的實現是:

先定義一個Paint

        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setColor(mBorderColor);         //  環的顏色   自己定啊 
        mBorderPaint.setStrokeWidth(mBorderWidth);   // 通過設定畫筆的寬度設定環的寬度 
然後用canvas.drawCircle繪製

好了不扯題外話了   先說第一種得到我們想要的  Bitmap 的方法 :

     1 , 下載網絡圖片

public static Bitmap getImagebitmap(final String imageUri) {
		// 顯示網絡上的圖片
		Bitmap bitmap = null;
		InputStream is = null;
		try {
			URL myFileUrl = new URL(imageUri);
			HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection(); //  用了最直接的HttpURLConnection
			conn.setConnectTimeout(1000 * 60);
			conn.setReadTimeout(1000 * 60);     //   響應超時的時間設置的特別長  爲了適應2G的比較慢的網速
			conn.setRequestMethod("GET");       
			conn.setDoInput(true);
			conn.connect();
			is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);        //關鍵是用BitmapFactory 解析這個輸入流
			// 關閉資源連接
			conn.disconnect();

		} catch (OutOfMemoryError e) {
			e.printStackTrace();
			bitmap = null;
		} catch (IOException e) {
			e.printStackTrace();
			bitmap = null;              // 異常處理中  當然是將這個bitmap設置成null了
		}
		return bitmap;
	}
下載網絡上的圖片  尤其是需要用到多線程同時給Adapter加載多張圖片    推薦使用Volley  或者風格略顯文藝的picasso框架   這些在以後的博客進行介紹  今天先用最簡單的。

當然這麼一張頭像下載下來之後  最好是存到手機文件中    給出寫入到文件的方法:

public final static String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/download_test/";     // <span class="con">獲取SD卡根目錄</span>
public void saveFile(Bitmap bm, String fileName) throws IOException {
		File dirFile = new File(ALBUM_PATH);
		if (!dirFile.exists()) {
			dirFile.mkdir();   //   文件目錄不存在需要手動建立
		}
		File myCaptureFile = new File(ALBUM_PATH + fileName);
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
		bm.compress(Bitmap.CompressFormat.PNG, 100, bos);  //  將這個Bitmap 寫入到bos 這個輸出流中  
		bos.flush();
		bos.close();      //   關閉資源   減少內存浪費   
	}

      2 , 從文件中讀取
直接上代碼:


public Bitmap getBitmapFromFile(String path) {         //   這個路徑是之前保存的圖片的路徑
		Bitmap bit = null;
		try {
			FileInputStream fis = new FileInputStream(path);   //  從文件中讀取需要用到FileInputStream
			bit = BitmapFactory.decodeStream(fis);             //  是不是和上面的網絡的輸入流轉化成Bitmap很相似呢  沒錯 他們是一樣的<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />
		} catch (FileNotFoundException e) {
			bit = null;
			e.printStackTrace();
		}
		return bit;

	}
     3 , 從項目中的drawable文件夾中讀取

 這個十分簡單   一行代碼:


Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);    //  icon是drawable文件夾中圖片文件名

    4 , 從 Drawable對象中獲取Bitmap


public Bitmap drawableToBitamp(Drawable drawable)  
    {  
        if (drawable instanceof BitmapDrawable)     // 如果是BitmapDrawable的實例或者其子類的實例
        {  
            BitmapDrawable bd = (BitmapDrawable) drawable;  
            return bd.getBitmap();  
        }  
        int w = drawable.getIntrinsicWidth();  
        int h = drawable.getIntrinsicHeight();  
        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);  
        Canvas canvas = new Canvas(bitmap);  
        drawable.setBounds(0, 0, w, h);  
        drawable.draw(canvas);  
        return bitmap;  
    }  






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章