【安卓知识点汇总】Bitmap位图解码

本篇着重:

  • 如何解码位图文件
  • 如何设置图片
  • 避免oom(多图模式),当然这里只有一张图片

1.封装工具类:BitmapTools和HttpUtils

BitmapTools:

public class BitmapTools {

	public BitmapTools() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * @param resources
	 *            资源文件
	 * @param resId
	 *            解码位图的id
	 * @param reqWith
	 *            指定输出位图的宽度
	 * @param reqHeight
	 *            指定输出位图的高度
	 * @return
	 */
	public static Bitmap decodeBitmap(Resources resources, int resId,
			int reqWith, int reqHeight) {
		// 对位图进行解码的参数设置
		BitmapFactory.Options options = new BitmapFactory.Options();
		// 在对位图进行解码的过程中,避免申请内存空间
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(resources, resId, options);
		// 对图片进行一定比例的压缩处理
		options.inSampleSize = calculateInSimpleSize(options, reqWith,
				reqHeight);
		options.inJustDecodeBounds = false;// 真正输出位图
		return BitmapFactory.decodeResource(resources, resId, options);
	}

	/**
	 * 
	 * @param options
	 * @param reqWith
	 * @param reqHeight
	 * @return
	 */
	public static int calculateInSimpleSize(BitmapFactory.Options options,
			int reqWith, int reqHeight) {
		// 获得图片的原始宽高
		int imageHeight = options.outHeight;
		int imageWidth = options.outWidth;
		int inSimpleSize = 1;// 压缩比例
		if (imageHeight > reqHeight || imageWidth > reqWith) {
			final int heightRatio = Math.round((float) imageHeight
					/ (float) reqHeight);
			final int widthRatio = Math.round((float) imageWidth
					/ (float) reqWith);
			inSimpleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
		}
		return inSimpleSize;
	}
}

HttpUtils:

public class HttpUtils {

	public HttpUtils() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * 
	 * @param path
	 *            访问图片的路径
	 * @return
	 */
	public static byte[] sendPost(String path) {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(path);
		HttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode() == 200) {
				return EntityUtils.toByteArray(response.getEntity());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			httpClient.getConnectionManager().shutdown();
		}
		return null;
	}
}


接下来,对temp.jpg文件进行示例解析并set:

public class MainActivity extends Activity {
	private Button button;
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button1);
		imageView = (ImageView) this.findViewById(R.id.imageView1);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Bitmap bm = BitmapTools.decodeBitmap(getResources(),
						R.drawable.temp, 50, 50);
				imageView.setImageBitmap(bm);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

这里代码,我尽可能详尽,以便和大家分享,也便于个人以后作为资料查询,取其精髓,去其糟粕吧:

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="47dp"
        android:text="对位图进行解码操作" />

</RelativeLayout>

这里解析是JPG文件:



好了,就到这里。




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值