请问关于Matrix的preTranslate和postTranslate

请问这两个方法的具体用法和用处是什么?

我查看了百度,说是可以旋转后摆正图片,我试了一下感觉要不要这两个方法都没区别,以下是我的代码,不知道有没有用错

        final View view=findViewById(R.id.view_animation);
        view.post(new Runnable() {
            @Override
            public void run() {
                Log.d(TAG,"width:"+view.getX()+" height:"+view.getHeight());
                CustomViewAnimation customViewAnimation=new CustomViewAnimation(view.getWidth()/2,view.getHeight()/2,10,50,80,true);
                customViewAnimation.setDuration(3000);
                customViewAnimation.setFillAfter(true);
                view.startAnimation(customViewAnimation);
            }
        });
    public CustomViewAnimation(float centerX,float centerY,float fromDegree,float toDegree,float depthZ,boolean reserve){
        this.centerX=centerX;
        this.centerY=centerY;
        this.fromDegree=fromDegree;
        this.toDegree=toDegree;
        this.depthZ=depthZ;
        this.reserve=reserve;
    }
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        float degree=fromDegree+(toDegree-fromDegree)*interpolatedTime;
        Matrix matrix=t.getMatrix();
        camera.save();
        Log.d(TAG,"interpolatedTime:"+interpolatedTime);
        if(reserve){
            camera.translate(0.0f,0.0f,depthZ*interpolatedTime);
        }else{
            camera.translate(0.0f,0.0f,depthZ*(1.0f-interpolatedTime));
        }
        camera.rotateY(degree);
        camera.getMatrix(matrix);
        camera.restore();
        matrix.preTranslate(-centerX,-centerY);
        matrix.postTranslate(centerX,centerY);
    }
阅读 3.1k
1 个回答

用法用错了,推荐看看 HenCoder 的 这篇文章

    canvas.save();
    Matrix matrix = new Matrix();
    //平移
    matrix.postTranslate(mFanCx-fanLen,mFanCy-fanLen);
    canvas.drawBitmap(mFanBitmap,matrix,paint);
    canvas.restore();
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题