图片旋转
matrix.setRotate(30, bitmap.getWidth()/2, bitmap.getHeight()/2); 绕( bitmap.getWidth()/2, bitmap.getHeight()/2)这个点旋转30度。 |
核心代码: public class MainActivity extends Activity {
private ImageView iv;
private ImageView iv_after;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
iv_after = (ImageView) findViewById(R.id.iv_after);
}
public void selectImage(View view){
Intent intent = new Intent();
intent.setAction("android.intent.action.PICK");
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data!=null){
//获取要查看的图片的路径
try {
Uri uri = data.getData();
iv.setImageURI(uri);
//得到原图的bitmap 只读
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
//空白可可以修改的bitmap
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
//创建画布
Canvas canvas = new Canvas(alterBitmap);
//创建画笔
Paint paint = new Paint();
paint.setColor(Color.BLACK);
Matrix matrix = new Matrix();
matrix.setRotate(30, bitmap.getWidth()/2, bitmap.getHeight()/2);
canvas.drawBitmap(bitmap, matrix, paint);
iv_after.setImageBitmap(alterBitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
|