本篇着重:
- 如何解码位图文件
- 如何设置图片
- 避免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;
}
}
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文件:
好了,就到这里。