Android绘制进阶之四:在位图上绘制文本并旋转

1,点击按钮,指定action和uri,设定结果码(ResultCode).到达手机默认相册的Gallery.

image

代码如下:

 

  
  
  
  
  1. public void onClick(View v) { 
  2.         // TODO Auto-generated method stub 
  3.         Intent intent = new Intent(Intent.ACTION_PICK, 
  4.                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 启动照片Gallery 
  5.         startActivityForResult(intent, 0); 
  6.     } 

2,选择图片,返回所选照片uri信息

代码如下:

 

  
  
  
  
  1. @Override 
  2.     protected void onActivityResult(int requestCode, int resultCode, 
  3.             Intent intent) { 
  4.         // TODO Auto-generated method stub 
  5.         super.onActivityResult(requestCode, resultCode, intent); 
  6.  
  7.         if (resultCode == RESULT_OK) {// 操作成功 
  8.             Uri imgFileUri = intent.getData();// 获得所选照片的信息 

3,处理图片,设定合适的尺寸(正好适应屏幕)

设定BitmapFactory.Options 参数inJustDecodeBounds = true,即不创建bitmap,但可获取bitmap的相关属性。以宽高比例差距 最大者为基准。

代码如下:

 

  
  
  
  
  1. // 由于返回的图像可能太大而无法完全加载到内存中。系统有限制,需要处理。 
  2.             Display currentDisplay = getWindowManager().getDefaultDisplay(); 
  3.             int defaultHeight = currentDisplay.getHeight(); 
  4.             int defaultWidth = currentDisplay.getWidth(); 
  5.  
  6.             BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options(); 
  7.             bitmapFactoryOptions.inJustDecodeBounds = true;// /只是为获取原始图片的尺寸,而不返回Bitmap对象 
  8.             // 注上:If set to true, the decoder will return null (no bitmap), but 
  9.             // the out... fields will still be set, 
  10.             // allowing the caller to query the bitmap without having to 
  11.             // allocate the memory for its pixels 
  12.             try { 
  13.                 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver() 
  14.                         .openInputStream(imgFileUri), null
  15.                         bitmapFactoryOptions); 
  16.                 int outHeight = bitmapFactoryOptions.outHeight; 
  17.                 int outWidth = bitmapFactoryOptions.outWidth; 
  18.                 int heightRatio = (int) Math.ceil((float) outHeight 
  19.                         / defaultHeight); 
  20.                 int widthRatio = (int) Math.ceil((float) outWidth 
  21.                         / defaultWidth); 
  22.  
  23.                 if (heightRatio > 1 || widthRatio > 1) { 
  24.                     if (heightRatio > widthRatio) { 
  25.                         bitmapFactoryOptions.inSampleSize = heightRatio; 
  26.                     } else { 
  27.                         bitmapFactoryOptions.inSampleSize = widthRatio; 
  28.                     } 
  29.                 } 

4,inJustDecodeBounds = false创建将此bitmap赋给第一个ImageView。

代码如下:

 

  
  
  
  
  1. bitmapFactoryOptions.inJustDecodeBounds = false
  2.                 bitmap = BitmapFactory.decodeStream(getContentResolver() 
  3.                         .openInputStream(imgFileUri), null
  4.                         bitmapFactoryOptions); 
  5.  
  6.                 mImageShow.setImageBitmap(bitmap); 

5,绘制bitmap(选中的图片),赋给第二个ImageView

a, 绘制正面Bitmap

b,在此Bitmap上绘制文本

c,创建Matrix对象,指定旋转角度。

d,创建新的bitmap对象,即canvas即将要绘制的倾斜区域

e,绘制

代码如下:

 

  
  
  
  
  1. // /* 
  2.                 // * 在位图上绘制位图 
  3.                 // */ 
  4.                 // 
  5.                  
  6.                 Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(), 
  7.                         bitmap.getHeight(), bitmap.getConfig()); 
  8.  
  9.                 Canvas canvas = new Canvas(bitmapAltered);// bitmap提供了画布,只在此提供了大小尺寸,偏移后并未有背景显示出来 
  10.                  
  11.                 Paint paint = new Paint(); 
  12.                  
  13.                 canvas.drawBitmap(bitmap, 00, paint); 
  14.                  
  15.                 paint.setColor(Color.RED); 
  16.  
  17.                 canvas.drawText("hello"1010, paint); 
  18.                  
  19.                 //先绘制正面图片,再旋转 
  20.                 Matrix matrix = new Matrix(); 
  21.                  
  22.                 matrix.setRotate(4500);      
  23.                 //创建一个倾斜的绘制区域 
  24.                 Bitmap bitmapRotated = Bitmap.createBitmap(bitmapAltered, 00, bitmapAltered.getWidth(), bitmapAltered.getHeight(), matrix, false); 
  25.                 canvas.drawBitmap(bitmapAltered, matrix, paint); 
  26.                 mImageAltered.setImageBitmap(bitmapRotated); 

 

你可能感兴趣的:(android,bitmap,canvas,paint,draw)