setScale,preScale 和 postScale 的區別


上面講到,Matrix由3*3矩陣中9個值來決定。而我們對Matrix的所有設置,也是對這9個值的各種不同的改變,來達到我們想要的效果。

下面是Matrix3*3的矩陣結構

{MSCALE_X,MSKEW_X,MTRANS_X,  
MSKEW_Y,MSCALE_Y,MTRANS_Y,  
MPERSP_0,MPERSP_1,MPERSP_2}  


一、首先介紹Scale縮放的控制

scale就是縮放,我們調用Matrix的setScale、preScale、postScale,實際在內部,就是通過修改MSCALE_X和MSCALE_Y來實現的。

下面就是一個簡單的例子

 
public class MatrixTestActivity extends Activity {  
    private int screenWidth;  
    private int screenHeight;  
    private int bitmapWidth;  
    private int bitmapHeight;  
    private float baseScale;  
    private float originalScale;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        // 獲得屏幕的寬高  
        screenWidth = getWindow().getWindowManager().getDefaultDisplay().getWidth();  
        screenHeight = getWindow().getWindowManager().getDefaultDisplay().getHeight();  
        // 加載Imageview和獲得圖片的信息  
        final ImageView imageView = (ImageView) findViewById(R.id.imgView);  
        final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);  
        bitmapWidth = bitmap.getWidth();  
        bitmapHeight = bitmap.getHeight();  
  
        // 計算縮放比,因為如果圖片的尺寸超過屏幕,那么就會自動匹配到屏幕的尺寸去顯示。  
        // 那么,我們就不知道圖片實際上在屏幕上顯示的寬高,所以先計算需要全部顯示的縮放比,  
        // 在去計算圖片顯示時候的實際寬高,然后,才好進行下一步的縮放。  
        // 要不然,會導致縮小和放大沒效果,或者內存泄漏等等  
        float scaleX = screenWidth / (float) bitmapWidth;  
        float scaleY = screenHeight / (float) bitmapHeight;  
        baseScale = Math.min(scaleX, scaleY);// 獲得縮放比例最大的那個縮放比,即scaleX和scaleY中小的那個  
        originalScale = baseScale;  
  
        final Matrix matrix = new Matrix();  
        matrix.setScale(originalScale, originalScale);  
        // 關於setScale和preScale和postScale的區別以后再說  
        // matrix.preScale(originalScale, originalScale);  
        // matrix.postScale(originalScale, originalScale);  
        Bitmap bitmap2 = Bitmap  
                .createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, false);  
        imageView.setImageBitmap(bitmap2);  
  
        final Button scale_btn = (Button) findViewById(R.id.scale_btn);  
        final EditText scale_text = (EditText) findViewById(R.id.scale_editView);  
        scale_btn.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {  
                String scaleStr = scale_text.getText().toString();  
                if (scaleStr == null || "".equals(scaleStr))  
                    return;  
                float scale = 0.0f;  
                try {  
                    scale = Float.parseFloat(scaleStr);  
                } catch (NumberFormatException e) {  
                    return;  
                }  
                matrix.reset();  
                originalScale = scale * originalScale;//看  
                if (originalScale < 0.05 ){  
                    originalScale=0.05f;  
                }  
                if(originalScale > baseScale){  
                    originalScale=baseScale;  
                }  
                matrix.setScale(originalScale, originalScale);  
                Bitmap bitmapChange = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight,  
                        matrix, false);  
                imageView.setImageBitmap(bitmapChange);  
            }  
        });  
    }  
}  

  

可以發現,對於Scale的設置,Matrix提供了3中不同的方式來設置。

setScale、preScale、postScale。

這三種方法之間有什么區別呢?看下面的。

 

二、set....、pre....、post...的區別

1、setScale(sx,sy),首先會將該Matrix設置為對角矩陣,即相當於調用reset()方法,然后在設置該Matrix的MSCALE_X和MSCALE_Y直接設置為sx,sy的值

2、preScale(sx,sy),不會重置Matrix,而是直接與Matrix之前的MSCALE_X和MSCALE_Y值結合起來(相乘),M' = M * S(sx, sy)。

3、postScale(sx,sy),不會重置Matrix,而是直接與Matrix之前的MSCALE_X和MSCALE_Y值結合起來(相乘),M' = S(sx, sy) * M。

preScale和post都是與之前的Matrix結合起來,那它們之間又有什么區別呢?

舉幾個例子測試下關於pre....和post....的區別:

對一個Matrix如下設置

1、pre....的執行順序

 
Matrix matrix=new Matrix();  
float[] points=new float[]{10.0f,10.0f};  
  
matrix.preScale(2.0f, 3.0f);//  
matrix.preTranslate(8.0f,7.0f);//  
matrix.mapPoints(points);  
Log.i("test", points[0]+"");  
Log.i("test", points[1]+"");  

  

結果為點坐標為(36.0,51.0)

可以得出結論,進行變換的順序是先執行preTranslate(8.0f,7.0f),在執行的preScale(2.0f,3.0f)。這也是為什么有的人比喻為pre...是向后生長的,即對於一個Matrix的設置中,

所有pre....是倒着向后執行的。

 

2、post...的執行順序

Matrix matrix=new Matrix();  
float[] points=new float[]{10.0f,10.0f};  
  
matrix.postScale(2.0f, 3.0f);//  
matrix.postTranslate(8.0f,7.0f);//  
matrix.mapPoints(points);  
Log.i("test", points[0]+"");  
Log.i("test", points[1]+"");  

  

結果為點坐標為(28.0,37.0)

可以得出結論,進行變換的順序是先執行postScale(2.0f,3.0f),在執行的postTranslate(8.0f,7.0f)。這 也是為什么有的人比喻為post...是向前生長的,即對於一個Matrix的設置中,所有post....是順着向前執行的。

 

3、當pre和post交替出現的執行順序

Matrix matrix=new Matrix();  
float[] points=new float[]{10.0f,10.0f};  
  
matrix.postScale(2.0f, 3.0f);  
matrix.preRotate(90);  
matrix.mapPoints(points);  
Log.i("test", points[0]+"");  
Log.i("test", points[1]+"");  

  

結果為點坐標為(-20.0,30.0)

將pre...和post順序換一下

Matrix matrix=new Matrix();  
float[] points=new float[]{10.0f,10.0f};  
  
matrix.preRotate(90);  
matrix.postScale(2.0f, 3.0f);  
matrix.mapPoints(points);  
Log.i("test", points[0]+"");  
Log.i("test", points[1]+"");  

  

結果為點坐標依舊為為(-20.0,30.0)

可見,總是pre先執行的。在看下面的:

 
Matrix matrix = new Matrix();  
float[] points = new float[] { 10.0f, 10.0f };  
  
matrix.postScale(2.0f, 3.0f);// 第1步  
matrix.preRotate(90);// 第2步  
matrix.postTranslate(8.0f, 7.0f);// 第3步  
matrix.preScale(1.5f, 2.5f);// 第4步  
matrix.mapPoints(points);  
Log.i("test", points[0] + "");  
Log.i("test", points[1] + "");  

  

結果為點坐標依舊為為(-42.0,52.0)
經過前面的結論和推算,可以發現執行的順序是   4----2----1---3

 

 在看下面的,增加了setScale的一段代碼:

Matrix matrix = new Matrix();  
float[] points = new float[] { 10.0f, 10.0f };  
  
matrix.postScale(2.0f, 3.0f);// 第1步  
matrix.preRotate(90);// 第2步  
matrix.setScale(1.4f, 2.6f);// 第3步  
matrix.postTranslate(8.0f, 7.0f);// 第4步  
matrix.preScale(1.5f, 2.5f);// 第5步  
matrix.mapPoints(points);  
Log.i("test", points[0] + "");  
Log.i("test", points[1] + "");  

  

結果為點坐標依舊為為(29.0,72.0)
經過計算,可以發現,在第3步setScale之前的第1、2步根本就沒有用了,直接被第3步setScale覆蓋,在從第3開始執行的。

順序為2---1----3----5----4,因為2、1被覆蓋了,所以沒有效果,相當於直接執行3-----5----4

 

總結:最后可以得出結論,在對matrix該次變換之前的所有設置中,先檢測有沒有setScale,如果有,直接跳到setScale那一步開始 執行變換,然后在倒着執行下面所有的pre...變換,在順着執行所有post....的變換。所以在對Matrix變換設置的時候,一定要注意順序,不 同的順序,會有不同的結果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM