Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
/*
* 将原来的bitmap画到新的易变的alteredBitmap对象中
*/
Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas canvas = new Canvas(alteredBitmap);
Paint paint = new Paint();
canvas.drawBitmap(bmp, 0, 0, paint);
Matrix matrix = new Matrix();
//matrix.setRotate(15);
matrix.setRotate(15, bmp.getWidth()/2, bmp.getHeight()/2); //从图片中心点开始旋转
canvas.drawBitmap(bmp, matrix, paint);
matrix.setScale(1.5f, 1);
第一个参数是x轴上的缩放,第二个参数是y轴上的缩放
Translate:图片在x轴或y轴移动
setTranslate方法接收两个参数,分别是x、y轴上的移动
matrix.setTranslate(15, 10);
以上的方法都有pre和post版本,可以使我们在一次时间序列中执行多于一次的变换
matrix.setScale(1.5f, 1.5f);
matrix.postRotate(15, bmp.getWidth()/2, bmp.getHeight()/2);
实现镜面翻转效果
matrix.setScale(-1, 1);
matrix.postTranslate(bmp.getWidth(), 0);
matrix.setScale(1, -1);
matrix.postTranslate(0, bmp.getHeight());
//It is initialized with the same density as the original bitmap.
//变换后的图片与原图片同样大小
Matrix matrix = new Matrix();
matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2);
Bitmap alteredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false);
alteredImageView.setImageBitmap(alteredBitmap);
package com.liang.imageTest; import java.io.FileNotFoundException; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class ChoosePictureActivity1 extends Activity implements OnClickListener{ ImageView choseImageView; ImageView alteredImageView; Button choosePicture; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.choosepicture1); choseImageView = (ImageView)findViewById(R.id.chosenImageView); alteredImageView = (ImageView)findViewById(R.id.alteredImageView); choosePicture = (Button)findViewById(R.id.choosePictureButton); choosePicture.setOnClickListener(this); } @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode==RESULT_OK){ Uri imageFileUri = data.getData(); Display currentDisplay = getWindowManager().getDefaultDisplay(); int dw = currentDisplay.getWidth(); int dh = currentDisplay.getHeight(); try{ BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null,options); /* * 图片得尺寸比屏幕大小大,按照图片尺寸与屏幕高、宽最大的比例来缩小图片 */ int heightRatio = (int)Math.ceil(options.outHeight/(float)dh); int widthRatio = (int)Math.ceil(options.outWidth/(float)dw); if(heightRatio > 1 && widthRatio > 1){ if(heightRatio > widthRatio) options.inSampleSize = heightRatio; else options.inSampleSize = widthRatio; } options.inJustDecodeBounds = false; bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, options); choseImageView.setImageBitmap(bmp); /* * 将原来的bitmap画到新的易变的alteredBitmap对象中 */ Bitmap alteredBitmap = Bitmap.createBitmap(bmp.getWidth()*2, bmp.getHeight()*2, bmp.getConfig()); Canvas canvas = new Canvas(alteredBitmap); Paint paint = new Paint(); /* * 1. */ // canvas.drawBitmap(bmp, 0, 0, paint); /* * 2.旋转 */ Matrix matrix = new Matrix(); // matrix.setRotate(15); // matrix.setRotate(15, bmp.getWidth()/2, bmp.getHeight()/2);//从图片中心点开始旋转 /* * 3.缩放 */ // matrix.setScale(1.5f, 1);//x轴1.5倍,y轴1倍 /* * 3-1.翻转 */ // matrix.setScale(-1, 1); // matrix.postTranslate(bmp.getWidth(), 0); /* * 3-2.翻转 */ // matrix.setScale(1, -1); // matrix.postTranslate(0, bmp.getHeight()); /* * 4.移动 */ // matrix.setTranslate(15, 10); /* * 5.序列 */ // matrix.setScale(1.5f, 1.5f); // matrix.postRotate(15, bmp.getWidth()/2, bmp.getHeight()/2); canvas.drawBitmap(bmp, matrix, paint); alteredImageView.setImageBitmap(alteredBitmap); /* * 图片与原图片大小一样 */ /* Matrix matrix = new Matrix(); matrix.setRotate(15,bmp.getWidth()/2,bmp.getHeight()/2); Bitmap alteredBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, false); alteredImageView.setImageBitmap(alteredBitmap); */ }catch(FileNotFoundException e){ Log.v("ERROR",e.toString()); } } } }