学习了下图片压缩的实现方式,代码很简单,小马就不多讲了,直接上代码:
- package com.xiaoma.temp.android.demo;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.BitmapFactory.Options;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ImageView;
- /**
- * @Title: AndroidTempDemoActivity.java
- * @Package com.xiaoma.temp.android.demo
- * @Description: 压缩测试类
- * @author XiaoMa
- */
- public class CompressActivity extends Activity{
- private Bitmap bmap;
- private ImageView image1;
- private ImageView image2;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- private void init(){
- image1 = (ImageView)findViewById(R.id.imageView1);
- image2 = (ImageView)findViewById(R.id.imageView2);
- Bitmap image2Bit = copressImage("/sdcard/notes.png");
- saveCompressPic(image2Bit);
- Drawable d = new BitmapDrawable(image2Bit);
- image2.setBackgroundDrawable(d);
- }
- /**
- * 如果大家要保存图片的话返回bmap就行了。保存方法也写下面了。
- * 如果有要用到截剪图片的功能。请留言,小马一起发.
- * @param imgPath
- */
- @SuppressWarnings("unused")
- private Bitmap copressImage(String imgPath){
- File picture = new File(imgPath);
- Options bitmapFactoryOptions = new BitmapFactory.Options();
- //下面这个设置是将图片边界不可调节变为可调节
- bitmapFactoryOptions.inJustDecodeBounds = true;
- bitmapFactoryOptions.inSampleSize = 2;
- int outWidth = bitmapFactoryOptions.outWidth;
- int outHeight = bitmapFactoryOptions.outHeight;
- bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),
- bitmapFactoryOptions);
- float imagew = 150;
- float imageh = 150;
- int yRatio = (int) Math.ceil(bitmapFactoryOptions.outHeight
- / imageh);
- int xRatio = (int) Math
- .ceil(bitmapFactoryOptions.outWidth / imagew);
- if (yRatio > 1 || xRatio > 1) {
- if (yRatio > xRatio) {
- bitmapFactoryOptions.inSampleSize = yRatio;
- } else {
- bitmapFactoryOptions.inSampleSize = xRatio;
- }
- }
- bitmapFactoryOptions.inJustDecodeBounds = false;
- bmap = BitmapFactory.decodeFile(picture.getAbsolutePath(),
- bitmapFactoryOptions);
- if(bmap != null){
- //ivwCouponImage.setImageBitmap(bmap);
- return bmap;
- }
- return null;
- }
- /**
- * 保存方法实现
- * @param bitmap
- */
- private void saveCompressPic(Bitmap bitmap){
- File file=new File("/sdcard/kkk/notes2.png");
- if(!file.exists()){
- file.mkdirs();
- }
- Log.i("KKK", "调用咯"+"..."+file.toString());
- try {
- FileOutputStream out=new FileOutputStream(file);
- if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
- out.flush();
- out.close();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
源码小马也放附件里面了,代码临时写的,很简单,有需要的下载下,不需要的请直接跳过。。吼吼。。有问题请直接批评指点。。谢谢
附件:http://down.51cto.com/data/2359942
本文转自华华世界 51CTO博客,原文链接:http://blog.51cto.com/mzh3344258/791522,如需转载请自行联系原作者