安卓获得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;  
    }  






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