android坐标系简记

在对View进行属性变化时候:
ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleX", 2);

  • 对视图进行放大或者缩小,view.getWidth()始终不变,变换后的实际宽度为view.getWidth()*view.getScaleX()
  • view.getLeft(),view.getTop(),view.getRight(),view.getBottom()取得值如下,并且它们的值不会因为平移或者缩放等操作而变化。
    android坐标系简记_第1张图片
    653161-20171123143730727-229301910.png
  • view.getX()以及view.getY()的值不会随着视图的放大或者缩小而改变。
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleX", 2);
                AnimatorSet set = new AnimatorSet();
                set.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        Log.i("hhhd", "onAnimationStart: view.getX(): "+view.getX() +"  view.getWidth():  "+view.getWidth()+ "  v.getTranslationX():"+view.getTranslationX());
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Log.i("hhhd", "onAnimationStart: view.getX(): "+view.getX() +"  view.getWidth():  "+view.getWidth()+ "  v.getTranslationX():"+view.getTranslationX()) ;
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                set.play(animator1);
                set.setDuration(2000);
                set.start();
            }
        });

结果为

hhhd: onAnimationStart: view.getX(): 260.0  view.getWidth():  200  v.getTranslationX():0.0
hhhd: onAnimationStart: view.getX(): 260.0  view.getWidth():  200  v.getTranslationX():0.0


  • 如果同时对视图进行缩放并且平移。
 findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                ObjectAnimator animator1 = ObjectAnimator.ofFloat(view, "scaleX", 2);
                ObjectAnimator animator2 = ObjectAnimator.ofFloat(view, "translationX",view.getTranslationX()+100);

                AnimatorSet set = new AnimatorSet();
                set.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        Log.i("hhhd", "onAnimationStart: view.getX(): "+view.getX() +"  view.getWidth():  "+view.getWidth()+ "  v.getTranslationX():"+view.getTranslationX());
                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Log.i("hhhd", "onAnimationStart: view.getX(): "+view.getX() +"  view.getWidth():  "+view.getWidth()+ "  v.getTranslationX():"+view.getTranslationX()) ;
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                set.play(animator1).with(animator2);
                set.setDuration(2000);
                set.start();
            }
        });

结果如下:

hhhd: onAnimationStart: view.getX(): 260.0  view.getWidth():  200  v.getTranslationX():0.0
hhhd: onAnimationStart: view.getX(): 360.0  view.getWidth():  200  v.getTranslationX():100.0

结果可以知道,只有平移才可以改变View.getX()或者View.getY()的值,视图的放大缩小无法影响任何坐标值。
如果此时需要获取到视图缩放之后的值:

view.getWidth() * view.getScaleX()

如果此时需要获取放大之后的视图的左边实际X坐标:

view.getX() - (view.getWidth() * view.getScaleX() - view.getWidth())

你可能感兴趣的:(android坐标系简记)