Android Matrix深度解析:3X3矩阵的奥秘与操作原理

0 下载量 140 浏览量 更新于2024-09-01 收藏 237KB PDF 举报
深入理解Android中的Matrix是Android开发中一个不可或缺的工具,尤其在图像处理、动画和变换操作中扮演着核心角色。Matrix类实际上是3X3的矩阵,这种设计的选择与线性代数中的基础概念密切相关。 1. **矩阵的维度**: Android Matrix是3X3的原因在于,它不仅支持2D空间的变换(如平移、缩放和旋转),还考虑到了深度信息。在3D图形中,第三个维度(z轴)对于透视变换是至关重要的,因此3X3矩阵能够处理平移在三维空间中的变化,而不仅仅是二维平面。即使在2D应用中,为了保持通用性和未来可能的扩展,使用3X3矩阵提供了更大的灵活性。 2. **矩阵元素的作用**: - 九个元素(0-8)分别对应矩阵的三个行和三个列,它们分别控制了x轴、y轴方向上的变换。比如,第一行(元素0,1,2)对应于x轴的缩放(Sx)、平移(tx),第二行类似地对应y轴。 - 第三行(元素3,4,5)用于存储z轴的变换,尽管在2D应用中通常不使用,但在3D场景中处理深度信息时必不可少。 - 第四行(元素6,7,8)通常用于存储旋转的角度(θ)和可能的额外参数,如倾斜或旋转中心。 3. **API方法的工作原理**: - `setXXX`方法用于设置特定位置的元素值,如`setTranslate(tx, ty)`用于设置平移,`setScale(Sx, Sy)`用于设置缩放比例。 - `preXXX`方法(preMultiply)用于在现有矩阵的基础上进行前后操作,例如先旋转后缩放,这涉及到矩阵乘法。 - `postXXX`方法(postMultiply)则相反,先做后操作,适用于需要先执行某变换然后再应用其他变换的情况。 理解Matrix的内部机制有助于开发者更高效地利用它,实现流畅的动画效果和图像变换。虽然2X2矩阵可能看起来更为直观,但3X3矩阵的设计是为了满足3D和未来的扩展需求。通过学习和实践,开发者将能更好地掌握Matrix在Android开发中的实际应用。

package com.blog.dragview; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.util.Log; import com.blog.R; import org.json.JSONArray; import org.json.JSONException; import java.util.List; public class MainActivity extends Activity{ private DrawingView _view; @Override protected void onCreate(Bundle savedInstanceState) { _view = new DrawingView(this); super.onCreate(savedInstanceState); setContentView(_view); Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.hengzhegou); Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.pie); CustomBitmap customBitmap1 = new CustomBitmap(bmp1); CustomBitmap customBitmap2 = new CustomBitmap(bmp2); customBitmap1.setId(1); customBitmap2.setId(2); if (getSavedMatrix(1) != null){ Log.e("tag", "matrix 1 is not null"); customBitmap1.setMatrix(getSavedMatrix(1)); } if (getSavedMatrix(2) != null){ Log.e("tag", "matrix 2 is not null"); customBitmap2.setMatrix(getSavedMatrix(2)); } _view.addBitmap(customBitmap1); _view.addBitmap(customBitmap2); } //����matrix private void saveMatrix(CustomBitmap customBitmap){ Log.e("tag", "save matrix" + customBitmap.getId()); SharedPreferences.Editor editor = getSharedPreferences("matrix", Context.MODE_PRIVATE).edit(); Matrix matrix = customBitmap.matrix; float[] values = new float[9]; matrix.getValues(values); JSONArray array = new JSONArray(); for (float value:values){ try { array.put(value); } catch (JSONException e) { e.printStackTrace(); } } editor.putString(String.valueOf(customBitmap.getId()), array.toString()); editor.commit(); Log.e("tag", "save matrix id:" + customBitmap.getId() + "---------"+values[Matrix.MPERSP_0] + " , " + values[Matrix.MPERSP_1] + " , " + values[Matrix.MPERSP_2] + " , " + values[Matrix.MSCALE_X] + " , " + values[Matrix.MSCALE_Y] + " , " + values[Matrix.MSKEW_X] + " , " + values[Matrix.MSKEW_Y] + " , " +values[Matrix.MTRANS_X] + " , " + values[Matrix.MTRANS_Y]); } //��ȡmatrix private Matrix getSavedMatrix(int id){ SharedPreferences sp = getSharedPreferences("matrix", Context.MODE_PRIVATE); String result = sp.getString(String.valueOf(id), null); if (result != null){ float[] values = new float[9]; Matrix matrix = new Matrix(); try { JSONArray array = new JSONArray(result); for (int i = 0; i < array.length(); i++) { values[i] = Float.valueOf(String.valueOf(array.getDouble(i))); } matrix.setValues(values); } catch (JSONException e) { e.printStackTrace(); } Log.e("tag", "get matrix id:" + id + "---------"+values[Matrix.MPERSP_0] + " , " + values[Matrix.MPERSP_1] + " , " + values[Matrix.MPERSP_2] + " , " + values[Matrix.MSCALE_X] + " , " + values[Matrix.MSCALE_Y] + " , " + values[Matrix.MSKEW_X] + " , " + values[Matrix.MSKEW_Y] + " , " +values[Matrix.MTRANS_X] + " , " + values[Matrix.MTRANS_Y]); return matrix ; } return null; } @Override public void finish() { List<CustomBitmap> list = _view.getViews(); for (CustomBitmap customBitmap:list){ saveMatrix(customBitmap); } super.finish(); } }

2023-06-05 上传