Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放、平移、旋转等操作。
首先介绍一下矩阵运算。加法和减法就不用说了,太简单了,对应位相加就好。图像处理,主要用到的是乘法 。下面是一个乘法的公式:

在 Android 里面, Matrix 由 9 个 float 值构成,是一个 3*3 的矩阵。如下图。
解释一下,上面的 sinX 和 cosX ,表示旋转角度的 cos 值和 sin 值,注意,旋转角度是按顺时针方向计算的。 translateX 和 translateY 表示 x 和 y 的平移量。 scale 是缩放的比例, 1 是不变, 2 是表示缩放 1/2 。
Matrix的操作,总共分为四种:
translate(平移),
rotate(旋转),
scale(缩放).
skew(倾斜).
每一种变换在
Android的API里都提供了set, post和pre三种操作方式,除了translate,其他三种操作都可以指定中心点。
set是直接设置Matrix的值,每次set一次,整个Matrix的数组都会变掉。
post是后乘,当前的矩阵乘以参数给出的矩阵。可以连续多次使用post,来完成所需的整个变换。例如,要将一个图片旋
转30度,然后平移到(100,100)的地方,那么可以这样做:
- Matrix m = new Matrix();
- m.postRotate(30);
- m.postTranslate(100, 100);
pre是前乘,参数给出的矩阵乘以当前的矩阵。所以操作是在当前矩阵的最前面发生的。例如上面的例子,如果用pre的话
- Matrix m = new Matrix();
- m.setTranslate(100, 100);
- m.preRotate(30);
旋转、缩放和倾斜都可以围绕一个中心点来进行,如果不指定,默认情况下,是围绕(0,0)点来进行。
下面给出一个例子,实现图片的缩小和放大:
首先以两张截图示例本程序:


下面给出实现的代码:
1.布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ImageView
android:id="@+id/myImageView"
android:layout_width="200px"
android:layout_height="150px"
android:src="@drawable/ex04_23"
android:layout_alignParentLeft="true"
/>
<Button
android:id="@+id/myButton1"
android:layout_width="90px"
android:layout_height="60px"
android:text="@string/str_button1"
android:textSize="18sp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/myButton2"
android:layout_width="90px"
android:layout_height="60px"
android:text="@string/str_button2"
android:textSize="18sp"
android:layout_toRightOf="@+id/myButton1"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
2.实现代码
public class EX04_23 extends Activity
{
/* 相关变量声明 */
private ImageView mImageView;
private Button mButton01;
private Button mButton02;
private RelativeLayout layout1;
private Bitmap bmp;
private int id=0;
private int displayWidth;
private int displayHeight;
private float scaleWidth=1;
private float scaleHeight=1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 加载main.xml Layout */
setContentView(R.layout.main);
/* 取得屏幕分辨率大小 */
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayWidth=dm.widthPixels;
/* 屏幕高度须扣除下方Button高度 */
displayHeight=dm.heightPixels-80;
/* 初始化相关变量 */
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.ex04_23);
mImageView = (ImageView)findViewById(R.id.myImageView);
layout1 = (RelativeLayout)findViewById(R.id.layout1);
mButton01 = (Button)findViewById(R.id.myButton1);
mButton02 = (Button)findViewById(R.id.myButton2);
/* 缩小按钮onClickListener */
mButton01.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
small();
}
});
/* 放大按钮onClickListener */
mButton02.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
big();
}
});
}
/* 图片缩小的method */
private void small()
{
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
/* 设定图片缩小的比例 */
double scale=0.8;
/* 计算出这次要缩小的比例 */
scaleWidth=(float) (scaleWidth*scale);
scaleHeight=(float) (scaleHeight*scale);
/* 生成reSize后的Bitmap对象 */
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
// Added in API level 1
// Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap. If the source bitmap is immutable and the requested subset is the same as the source bitmap itself, then the source bitmap is returned and no new bitmap is created.
//
// Parameters
// source The bitmap we are subsetting
// x The x coordinate of the first pixel in source
// y The y coordinate of the first pixel in source
// width The number of pixels in each row
// height The number of rows
// m Optional matrix to be applied to the pixels
// filter true if the source should be filtered. Only applies if the matrix contains more than just translation.
//
// Returns
// A bitmap that represents the specified subset of source
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
if(id==0)
{
/* 如果是第一次按,就移除原来设定的ImageView */
layout1.removeView(mImageView);
}
else
{
/* 如果不是第一次按,就移除上次放大缩小所生成的ImageView */
layout1.removeView((ImageView)findViewById(id));
}
/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(EX04_23.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
/* 因为图片放到最大时放大按钮会disable,所以在缩小时把他重设为enable */
mButton02.setEnabled(true);
}
/* 图片放大的method */
private void big()
{
int bmpWidth=bmp.getWidth();
int bmpHeight=bmp.getHeight();
/* 设定图片放大的比例 */
double scale=1.25;
/* 计算这次要放大的比例 */
scaleWidth=(float)(scaleWidth*scale);
scaleHeight=(float)(scaleHeight*scale);
/* 产生reSize后的Bitmap对象 */
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,bmpHeight,matrix,true);
if(id==0)
{
/* 如果是第一次按,就移除原来设定的ImageView */
layout1.removeView(mImageView);
}
else
{
/* 如果不是第一次按,就移除上次放大缩小所生成的ImageView */
layout1.removeView((ImageView)findViewById(id));
}
/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
id++;
ImageView imageView = new ImageView(EX04_23.this);
imageView.setId(id);
imageView.setImageBitmap(resizeBmp);
layout1.addView(imageView);
setContentView(layout1);
/* 如果再放大会超过屏幕大小,就把Button disable */
if(scaleWidth*scale*bmpWidth>displayWidth||scaleHeight*scale*bmpHeight>displayHeight)
{
mButton02.setEnabled(false);
}
}
}
说明:
因为在Android中不允许ImageView在产生之后,动态修改其长度和宽度,所以,为了实现图片的放大,缩小的共功能,使用的方式是当用户在单击按钮之后,需要将原布局文件中的图片文件删除,重新生成一个ImageView,再放入Layout之中。
分享到:
相关推荐
在Android开发中,ImageView是用于显示图像的常见组件。然而,仅靠基本的ImageView功能,我们往往无法满足一些复杂的交互需求,比如用户可以对图片进行拖动和双指缩放。这种功能通常需要借助Matrix类来实现。Matrix...
在Android开发中,实现图片随手势放大缩小的功能是一项常见的需求,尤其在开发图像查看器或者照片浏览应用时。这个功能让用户体验更加流畅自然,能够自由地探索图片的细节。以下将详细讲解如何实现这一功能。 首先...
在Android平台上,开发一款能够实现图片放大、缩小、旋转以及涂鸦功能的应用是常见的需求,这对于用户交互和图像处理有着重要的意义。这份"android应用源码图片放大缩小旋转涂鸦源码.zip"提供了完整的源代码,可以...
在Android开发中,实现图片的放大缩小功能是常见的需求,特别是在设计相册或者查看图片的应用中。本Demo主要展示了如何在Android环境中实现这样的功能,既支持网络图片的加载,也支持本地图片的显示,并允许用户进行...
总的来说,Android中实现图片的放大缩小和平移是一项涉及到图形变换、手势识别和用户交互的重要技术。通过自定义`DragImageView`,我们可以创建一个功能强大的图片查看器,提供类似照片应用的用户体验。这个过程不仅...
在Android应用开发中,用户交互体验是至关重要的,特别是在电商应用中,商品详情页面的图片展示功能需要提供良好的用户体验,比如双击图片可以实现图片的放大缩小。这种功能的实现通常涉及手势检测、图片处理以及...
本文将详细介绍如何使用`Matrix`类来实现图片在Android应用中的随意放大、缩小和拖动功能。 首先,我们创建一个新的Android项目,命名为`DragAndZoom`。在`res/drawable-hdpi`目录下放入需要处理的图片,例如`wall....
在Android开发中,图片的放大缩小功能是移动应用中常见的需求,尤其在图像查看器、照片编辑器等应用中更是必不可少。本节我们将详细探讨如何实现这个功能,主要涉及的知识点包括手势检测、图片矩阵变换以及视图更新...
在图片放大缩小中,我们需要改变`ImageView`的`Matrix`属性来实现图片的动态调整。当用户执行缩放手势时,我们根据`ScaleGestureDetector`提供的比例因子更新`Matrix`。 4. **OnTouchListener与GestureListener**: ...
在Android开发中,图片双击放大缩小是一种常见的交互方式,为用户提供更好的查看体验。这个开源项目 "ImageViewZoom-master" 正是提供了这样的功能,让用户在查看图片时可以通过双击手势来实现图片的缩放操作。下面...
2. **缩放(Zooming)功能**:用户可以通过双指开合或单独手指捏合来放大或缩小图片。这需要利用Android的Matrix类来处理图像变换,通过调整Matrix的scaleX和scaleY值来实现缩放效果。同时,为了防止过度缩放导致图片...
在Android开发中,实现类似微信朋友圈的图片查看功能是一项常见的需求。这个功能允许用户点击图片后进入一个全屏模式,可以放大、缩小图片,并通过左右滑动切换到其他图片。以下将详细介绍如何实现这一功能。 首先...
在Android中,ImageView有一个`setImageMatrix()`方法,可以接受一个Matrix作为参数,从而实现图像的变换。 ```java imageView.setImageMatrix(matrix); ``` 为了响应用户的触摸事件,实现图像的动态缩放和平移,...
在Android开发中,图片的处理是一项非常常见的任务,特别是在用户界面设计中,ImageView组件是展示图片的主要方式。本文将深入探讨如何在Android中实现图片的放大和缩小功能,特别是通过ImageView来实现这一目标。 ...
总结来说,Android中的图片放大缩小涉及到`ImageView`、`Matrix`、`Bitmap`尺寸计算、`ScaleGestureDetector`等多个组件和概念。通过理解和实践这些知识点,开发者可以构建出具有灵活缩放功能的图片查看应用。
在Android开发中,图片的放大和缩小...总的来说,Android图片放大缩小和多触屏的支持涉及到触摸事件处理、Matrix变换以及手势识别等多个知识点。通过理解这些概念并结合实际代码,可以创建出功能完善的图像查看组件。
在Android开发中,实现图片随手势放大缩小的功能是一项常见的需求,尤其在查看照片、阅读电子书或使用图像编辑应用时。这项技术的核心是利用Android提供的触摸事件处理机制和矩阵操作来改变图片的缩放比例。以下将...
"Android-图片浏览器支持缩放下拉缩小退出"这个项目旨在实现一个与微信图片浏览效果类似的图片查看器,具备手势缩放、下拉缩小以及平滑退出的功能。接下来,我们将深入探讨实现这些功能的关键知识点。 首先,我们要...
在图片放大缩小的功能中,我们需要创建一个Matrix对象,然后根据手势的缩放因子调整Matrix,最后使用`Bitmap.createBitmap(Bitmap src, int x, int y, int width, int height, Matrix m, boolean filter)`方法创建一...
以上就是实现Android图片浏览、放大缩小和平移的核心技术点。在实际开发中,还需要注意性能优化,比如使用异步加载图片、处理内存泄漏等,以提供流畅的用户体验。在项目`MyApplication`中,你可以找到具体的代码实现...