android之Matrix
16lz
2021-01-23
今天我学习下android中和图片相关的Matrix.
先看代码示例:
view plain copy to clipboard print ?- packagecn.com.chenzheng_java;
- importandroid.app.Activity;
- importandroid.graphics.Bitmap;
- importandroid.graphics.BitmapFactory;
- importandroid.graphics.Matrix;
- importandroid.graphics.drawable.BitmapDrawable;
- importandroid.os.Bundle;
- importandroid.view.ViewGroup.LayoutParams;
- importandroid.widget.ImageView;
- importandroid.widget.LinearLayout;
- importandroid.widget.ImageView.ScaleType;
- publicclassMatrixActivityextendsActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setTitle("eoeAndroid教程:缩放和旋转图片-by:IceskYsl");
- LinearLayoutlinLayout=newLinearLayout(this);
- //加载需要操作的图片,这里是eoeAndroid的logo图片
- BitmapbitmapOrg=BitmapFactory.decodeResource(getResources(),
- R.drawable.blackk);
- //获取这个图片的宽和高
- 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.graphics.Matrix类的主要应用。
Matrix的操作,总共分为translate(平移),rotate(旋转),scale(缩放)和skew(倾斜)四种,每一种变换在Android的API里都提供了set, post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
旋转、缩放和倾斜都可以围绕一个中心点来进行,如果不指定,默认情况下,是围绕(0,0)点来进行。
具体的方法及使用方法请参考API、这里就不一一列出了,实在是太多了。
更多相关文章
- android文件操作的实例
- Android 以流的方式读取服务器图片文件
- Android手机操作系统中实现图片浏览
- Android 利用Matrix实现图片随手指平移、旋转、缩放
- 想抢先体验Android操作系统的魅力吗?那就使用Android LiveCD吧!
- android与html5的交互——数据库操作,UI操作,以及html5的localStor
- OMS:拥有Android血统的智能操作系统王
- 巧用Android图片资源,打造更精致的APP
- Android Studio 基础 之 如何取消使用 Android X 的操作(附有详细