• Android -- View setScale, setTranslation 对View矩阵的处理


    参考:

    1、Android Matrix理论与应用详解

    2、2D平面中关于矩阵(Matrix)跟图形变换的讲解

    3、Android中关于矩阵(Matrix)前乘后乘的一些认识

    4、Android Matrix

    View.setPivotX:设置旋转或缩放的基点的X位置,默认是对象的中点(官方API说明:https://developer.android.com/reference/android/view/View.html#setPivotX(float))。

    View.setScaleX:设置缩放比例。一般情况下,View的缩放会产生平移数据。

    相对点P(a,b)的比例[sx,sy]变化矩阵[1]


    这里的点P(a, b)就是前文说的基点。

    注意:

    setTranslationX(),是post模式,即setTranslationX()产生的变换矩阵乘以当前矩阵。

    setScaleX(),使pre模式,即当前矩阵乘以setScaleX()产生的变换矩阵。

    例子:

     

    无论setScalex()在setTanslation()之前或之后调用,都是setTanslation()产生的变换矩阵乘以setScaleX()产生的变换矩阵。

    举例:setScalex(0.9),setTanslation(100),矩阵变换为:

    1   0   100     乘以    0.9    0   (1-0.9)*320  得到   0.9   0   132

    0   1    0                  0      1       0                        0    1     0

    0   0    1                  0      0       1                        0    0     1

    第一个矩阵由setTanslation(100)得到,第二个矩阵由setScalex(0.9)得到,而(1-0.9)*320中的320是Image中点X值,实例图片宽度为640。

    再次调用这两个方法后,都是重新进行计算,而不是在前一次的基础上计算。都是从矩阵 1  0  0 开始。

                                                                                                                  0  1  0

                                                                                                                  0  0  1

    代码:

    public class MatrixActivity extends Activity implements View.OnClickListener {
    
        private static final String TAG = MatrixActivity.class.getSimpleName();
    
        private ImageView mWImageView;
        private Button mBtn_Translation, mBtnScale, mBtnTranslation;
        private int mHeight, mWidth;
        private static float mScale = 1.0f;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_matrix);
    
            mWImageView = (ImageView) findViewById(R.id.welcome_img_view);
            mBtn_Translation = (Button) findViewById(R.id._translation);
            mBtnScale = (Button) findViewById(R.id.scale);
            mBtnTranslation = (Button) findViewById(R.id.translation);
            //改变基点的X值,默认以中点
    //        mWImageView.setPivotX(0);
    //        mWImageView.setPivotX(640);
    
            setOnclickListener();
    
        }
    
        @Override
        protected void onResume() {
            Log.d(TAG, "In onResume ---------------------");
            super.onResume();
            publicLog();
        }
    
        private void setOnclickListener() {
            mBtn_Translation.setOnClickListener(this);
            mBtnScale.setOnClickListener(this);
            mBtnTranslation.setOnClickListener(this);
        }
    
        private void publicLog() {
            Log.d(TAG, "mWImageView x, y, width, height:" + mWImageView.getLeft() + " --- "
                    + mWImageView.getTop() + " --- " + mWImageView.getWidth() + " --- " + mWImageView.getHeight());
            Log.d(TAG, "mWImageView Pivots:" + mWImageView.getPivotX() + " --- " + mWImageView.getPivotY());
            Log.d(TAG, "mWImageView Matrix:" + mWImageView.getMatrix());
        }
    
        @Override
        public void onClick(View v) {
            int id = v.getId();
    
            mHeight = mWImageView.getHeight();
            mWidth = mWImageView.getWidth();
            int dx = mWidth / 2;
    
            switch (id) {
                case R.id._translation:
                    Log.d(TAG, "mWImageView _translation ---------------------");
                    publicLog();
                    Log.d(TAG, "mWImageView TranslationX: " + -dx);
                    mWImageView.setTranslationX(-dx);
                    publicLog();
                    break;
    
                case R.id.scale:
                    Log.d(TAG, "mWImageView scale ---------------------");
                    publicLog();
                    if(mScale - 0.1 > 0){
                        mScale = mScale - 0.1f;
                    }else{
                        mScale = mScale + 0.1f;
                    }
                    mWImageView.setScaleX(mScale);
                    publicLog();
                    break;
    
                case R.id.translation:
                    Log.d(TAG, "mWImageView translation ---------------------");
                    publicLog();
                    Log.d(TAG, "mWImageView TranslationX: " + dx);
                    mWImageView.setTranslationX(dx);
                    publicLog();
                    break;
            }
    
            int[] location = new int[2];
            mWImageView.getLocationInWindow(location);
            Log.d(TAG, "mWImageView LocationInWindow:" + location[0] + " --- " + location[1]);
    
        }
    }
    MatrixActivity.java
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ImageView
            android:id="@+id/welcome_img_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/welcome" />
    
    
        <Button
            android:id="@+id/_translation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=" - Translation " />
    
        <Button
            android:id="@+id/scale"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Scale" />
    
        <Button
            android:id="@+id/translation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Translation " />
    
    </LinearLayout>
    activity_matrix.xml

  • 相关阅读:
    Jquery中addClass方法不起作用的解决方案
    JavaScript 下拉框 左边添加至右边
    电商网站常用放大镜特效
    jQuery 移除事件与模拟事件
    考勤管理系统V1.0.3
    js 严格模式
    service workder
    本地存储之application cache和localstorage
    Node.js内置的文件系统模块(fs)
    Node.js:OS模块
  • 原文地址:https://www.cnblogs.com/yarightok/p/5689798.html
Copyright © 2020-2023  润新知