1、图片缩放:不解析整个图片信息。
public class DemoActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView iv = (ImageView) this.findViewById(R.id.iv); // BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); // //按1/8的大小加载到内存 // bmpFactoryOptions.inSampleSize = 8; // Bitmap bm = BitmapFactory.decodeFile("/sdcard/img.jpg",bmpFactoryOptions); // iv.setImageBitmap(bm); // //1. 首先得到屏幕的宽高 Display currentDisplay = getWindowManager().getDefaultDisplay(); int dw = currentDisplay.getWidth(); int dh = currentDisplay.getHeight(); // 2. 获取图片的宽度和高度 BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); // 设置inJustDecodeBounds = true ,只会解析文件头信息; bmpFactoryOptions.inJustDecodeBounds = true; // 不会真正的解析这个位图 Bitmap bmp = BitmapFactory.decodeFile("/sdcard/img.jpg", bmpFactoryOptions); //得到图片的宽度和高度 int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh); int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw); Log.v("HEIGHTRATIO",""+heightRatio); Log.v("WIDTHRATIO",""+widthRatio); //真正的解析位图 bmpFactoryOptions.inJustDecodeBounds = false; if(heightRatio>widthRatio){ bmpFactoryOptions.inSampleSize = heightRatio; }else{ bmpFactoryOptions.inSampleSize = widthRatio; } bmp = BitmapFactory.decodeFile("/sdcard/img.jpg", bmpFactoryOptions); iv.setImageBitmap(bmp); } }
2、ExifInterface获取或设置图片的exif信息:
public class DemoActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { ExifInterface exif = new ExifInterface("sdcard/img.jpg"); System.out.println( exif.getAttribute(ExifInterface.TAG_DATETIME)); System.out.println( exif.getAttribute(ExifInterface.TAG_IMAGE_LENGTH)); System.out.println( exif.getAttribute(ExifInterface.TAG_IMAGE_WIDTH)); exif.setAttribute("camera", "柯达相机"); System.out.println( exif.getAttribute("camera")); } catch (Exception e) { e.printStackTrace(); } } }
3、从图库里面选择一个图片,然后将它设置到ImageView。
public class DemoActivity extends Activity { ImageView iv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv = (ImageView) this.findViewById(R.id.iv); } public void click(View view){ //选择图库意图,打开图库 Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(data!=null){ //图片的uri Uri uri = data.getData() ; iv.setImageURI(uri); } super.onActivityResult(requestCode, resultCode, data); } }
4、创建图片(bitmap)的拷贝:
public class DemoActivity extends Activity{ ImageView iv1; ImageView iv2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); iv1= (ImageView) this.findViewById(R.id.iv1); iv2= (ImageView) this.findViewById(R.id.iv2); } public void click(View view){ Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(data!=null){ //图片的uri Uri uri = data.getData() ; iv1.setImageURI(uri); // 获取第一个图片 的拷贝 设置到iv2的控件里面 try { //通过uri获取一个流对象。getContentResolver().openInputStream(uri) Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); //图片缩放 Matrix matrix = new Matrix(); // matrix.setValues(new float[] { // 2, 0, 0, // 0, 1, 0, // 0, 0, 1 // });//矩阵缩放 // matrix.setScale(1.5f, 1.0f);//水平方向为1.5倍,垂直方向不做改变 // matrix.setRotate(30); //画笔 Paint paint = new Paint(); paint.setAntiAlias(true); matrix.setRotate(15);//图片旋转 //得到一个大小 配置跟原图一样的空白的位图 //Bitmap alterbitmap = Bitmap.createBitmap(bitmap.getWidth()*2, bitmap.getHeight(), bitmap.getConfig()); Bitmap alteredBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); //画布对象 Canvas canvas = new Canvas(); //将图片流bitmap应用到画布上 canvas.drawBitmap(bitmap, matrix, paint); iv2.setImageBitmap(alteredBitmap); } catch (Exception e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } }