关闭

Android Bitmap zoomIn/zoomOut/rotate ——图片的缩小,放大 和旋转(转)

667人阅读 评论(0) 收藏 举报

ava代码 < type="application/x-shockwave-flash" width="14" height="15" src="http://wayfarer.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=public%20void%20onCreate(Bundle%20icicle)%20%7B%0A%09super.onCreate(icicle)%3B%0A%09LinearLayout%20linLayout%20%3D%20new%20LinearLayout(this)%3B%0A%0A%09%2F%2F%20%E5%8A%A0%E8%BD%BD%E9%9C%80%E8%A6%81%E6%93%8D%E4%BD%9C%E7%9A%84%E5%9B%BE%E7%89%87%EF%BC%8C%E8%BF%99%E9%87%8C%E6%98%AFeoeAndroid%E7%9A%84logo%E5%9B%BE%E7%89%87%0A%09Bitmap%20bitmapOrg%20%3D%20BitmapFactory.decodeResource(getResources()%2C%0A%09%09%09R.drawable.icon)%3B%0A%0A%09%2F%2F%20%E8%8E%B7%E5%8F%96%E8%BF%99%E4%B8%AA%E5%9B%BE%E7%89%87%E7%9A%84%E5%AE%BD%E5%92%8C%E9%AB%98%0A%09int%20width%20%3D%20bitmapOrg.getWidth()%3B%0A%09int%20height%20%3D%20bitmapOrg.getHeight()%3B%0A%0A%09%2F%2F%20%E5%AE%9A%E4%B9%89%E9%A2%84%E8%BD%AC%E6%8D%A2%E6%88%90%E7%9A%84%E5%9B%BE%E7%89%87%E7%9A%84%E5%AE%BD%E5%BA%A6%E5%92%8C%E9%AB%98%E5%BA%A6%0A%09int%20newWidth%20%3D%20200%3B%0A%09int%20newHeight%20%3D%20200%3B%0A%0A%09%2F%2F%20%E8%AE%A1%E7%AE%97%E7%BC%A9%E6%94%BE%E7%8E%87%EF%BC%8C%E6%96%B0%E5%B0%BA%E5%AF%B8%E9%99%A4%E5%8E%9F%E5%A7%8B%E5%B0%BA%E5%AF%B8%0A%09float%20scaleWidth%20%3D%20((float)%20newWidth)%20%2F%20width%3B%0A%09float%20scaleHeight%20%3D%20((float)%20newHeight)%20%2F%20height%3B%0A%0A%09%2F%2F%20%E5%88%9B%E5%BB%BA%E6%93%8D%E4%BD%9C%E5%9B%BE%E7%89%87%E7%94%A8%E7%9A%84matrix%E5%AF%B9%E8%B1%A1%0A%09Matrix%20matrix%20%3D%20new%20Matrix()%3B%0A%0A%09%2F%2F%20%E7%BC%A9%E6%94%BE%E5%9B%BE%E7%89%87%E5%8A%A8%E4%BD%9C%0A%09matrix.postScale(scaleWidth%2C%20scaleHeight)%3B%0A%0A%09%2F%2F%20%E6%97%8B%E8%BD%AC%E5%9B%BE%E7%89%87%20%E5%8A%A8%E4%BD%9C%0A%09matrix.postRotate(45)%3B%0A%0A%09%2F%2F%20%E5%88%9B%E5%BB%BA%E6%96%B0%E7%9A%84%E5%9B%BE%E7%89%87%0A%09Bitmap%20resizedBitmap%20%3D%20Bitmap.createBitmap(bitmapOrg%2C%200%2C%200%2C%20width%2C%0A%09%09%09height%2C%20matrix%2C%20true)%3B%0A%0A%09%2F%2F%20%E5%B0%86%E4%B8%8A%E9%9D%A2%E5%88%9B%E5%BB%BA%E7%9A%84Bitmap%E8%BD%AC%E6%8D%A2%E6%88%90Drawable%E5%AF%B9%E8%B1%A1%EF%BC%8C%E4%BD%BF%E5%BE%97%E5%85%B6%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E5%9C%A8ImageView%2C%20ImageButton%E4%B8%AD%0A%09BitmapDrawable%20bmd%20%3D%20new%20BitmapDrawable(resizedBitmap)%3B%0A%0A%09%2F%2F%20%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAImageView%0A%09ImageView%20imageView%20%3D%20new%20ImageView(this)%3B%0A%0A%09%2F%2F%20%E8%AE%BE%E7%BD%AEImageView%E7%9A%84%E5%9B%BE%E7%89%87%E4%B8%BA%E4%B8%8A%E9%9D%A2%E8%BD%AC%E6%8D%A2%E7%9A%84%E5%9B%BE%E7%89%87%0A%09imageView.setImageDrawable(bmd)%3B%0A%0A%09%2F%2F%20%E5%B0%86%E5%9B%BE%E7%89%87%E5%B1%85%E4%B8%AD%E6%98%BE%E7%A4%BA%0A%09imageView.setScaleType(ScaleType.CENTER)%3B%0A%0A%09%2F%2F%20%E5%B0%86ImageView%E6%B7%BB%E5%8A%A0%E5%88%B0%E5%B8%83%E5%B1%80%E6%A8%A1%E6%9D%BF%E4%B8%AD%0A%09linLayout.addView(imageView%2C%20new%20LinearLayout.LayoutParams(%0A%09%09%09LayoutParams.FILL_PARENT%2C%20LayoutParams.FILL_PARENT))%3B%0A%0A%09%2F%2F%20%E8%AE%BE%E7%BD%AE%E4%B8%BA%E6%9C%ACactivity%E7%9A%84%E6%A8%A1%E6%9D%BF%0A%09setContentView(linLayout)%3B%0A%7D">

  1. public   void  onCreate(Bundle icicle) {  
  2.     super .onCreate(icicle);  
  3.     LinearLayout linLayout = new  LinearLayout( this );  
  4.   
  5.     // 加载需要操作的图片,这里是eoeAndroid的logo图片   
  6.     Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),  
  7.             R.drawable.icon);  
  8.   
  9.     // 获取这个图片的宽和高   
  10.     int  width = bitmapOrg.getWidth();  
  11.     int  height = bitmapOrg.getHeight();  
  12.   
  13.     // 定义预转换成的图片的宽度和高度   
  14.     int  newWidth =  200 ;  
  15.     int  newHeight =  200 ;  
  16.   
  17.     // 计算缩放率,新尺寸除原始尺寸   
  18.     float  scaleWidth = (( float ) newWidth) / width;  
  19.     float  scaleHeight = (( float ) newHeight) / height;  
  20.   
  21.     // 创建操作图片用的matrix对象   
  22.     Matrix matrix = new  Matrix();  
  23.   
  24.     // 缩放图片动作   
  25.     matrix.postScale(scaleWidth, scaleHeight);  
  26.   
  27.     // 旋转图片 动作   
  28.     matrix.postRotate(45 );  
  29.   
  30.     // 创建新的图片   
  31.     Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0 0 , width,  
  32.             height, matrix, true );  
  33.   
  34.     // 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中   
  35.     BitmapDrawable bmd = new  BitmapDrawable(resizedBitmap);  
  36.   
  37.     // 创建一个ImageView   
  38.     ImageView imageView = new  ImageView( this );  
  39.   
  40.     // 设置ImageView的图片为上面转换的图片   
  41.     imageView.setImageDrawable(bmd);  
  42.   
  43.     // 将图片居中显示   
  44.     imageView.setScaleType(ScaleType.CENTER);  
  45.   
  46.     // 将ImageView添加到布局模板中   
  47.     linLayout.addView(imageView, new  LinearLayout.LayoutParams(  
  48.             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  49.   
  50.     // 设置为本activity的模板   
  51.     setContentView(linLayout);  
  52. }  
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27169次
    • 积分:379
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:42篇
    • 译文:0篇
    • 评论:2条
    文章分类
    文章存档
    最新评论