Android(安卓)Bitmap zoomIn/zoomOut/rotate ——图片的缩小,放大 和旋转
16lz
2021-01-24
Java代码
- publicvoidonCreate(Bundleicicle){
- super.onCreate(icicle);
- LinearLayoutlinLayout=newLinearLayout(this);
- //加载需要操作的图片,这里是eoeAndroid的logo图片
- BitmapbitmapOrg=BitmapFactory.decodeResource(getResources(),
- R.drawable.icon);
- //获取这个图片的宽和高
- intwidth=bitmapOrg.getWidth();
- intheight=bitmapOrg.getHeight();
- //定义预转换成的图片的宽度和高度
- intnewWidth=200;
- intnewHeight=200;
- //计算缩放率,新尺寸除原始尺寸
- floatscaleWidth=((float)newWidth)/width;
- floatscaleHeight=((float)newHeight)/height;
- //创建操作图片用的matrix对象
- Matrixmatrix=newMatrix();
- //缩放图片动作
- matrix.postScale(scaleWidth,scaleHeight);
- //旋转图片动作
- matrix.postRotate(45);
- //创建新的图片
- BitmapresizedBitmap=Bitmap.createBitmap(bitmapOrg,0,0,width,
- height,matrix,true);
- //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView,ImageButton中
- BitmapDrawablebmd=newBitmapDrawable(resizedBitmap);
- //创建一个ImageView
- ImageViewimageView=newImageView(this);
- //设置ImageView的图片为上面转换的图片
- imageView.setImageDrawable(bmd);
- //将图片居中显示
- imageView.setScaleType(ScaleType.CENTER);
- //将ImageView添加到布局模板中
- linLayout.addView(imageView,newLinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
- //设置为本activity的模板
- setContentView(linLayout);
- }
更多相关文章
- android调用系统拍照程序和从图库选取图片,返回后调用系统裁剪工
- android中Intent的介绍和常见用法总结
- Android(安卓)BitmapFactory用法总结
- Android使用post方式上传图片到服务器的方法
- Android(安卓)网易云音乐图片高斯模糊
- 个性缩放图片NinePatchDrawable
- android桌面快捷方式创建无标题文章
- Android选择图片的两种方式
- Android(安卓)获取照相机图片或本地图片