Canvas animate"关闭这本书"

时间:2014-05-22 07:11:28

标签: android canvas android-canvas android-animation

首先,我之前从未使用过画布转换。但这是我必须做的:

说我有一个包含图像的画布。我需要像这样转换它

original image到此enter image description here

我可以如下旋转画布:

enter image description hereenter image description here

以下是我用于左边的代码:

public void transformCanvas(Canvas canvas, float percentOpen) {
    Camera camera = new Camera();
    canvas.translate(0, canvas.getHeight()/2);
    camera.translate(0, 0, Math.abs(90 * percentOpen - 90));
    camera.rotateY(Math.abs(90 * percentOpen - 90));
    Matrix matrix = new Matrix();
    camera.getMatrix(matrix);
    canvas.concat(matrix);
    canvas.translate(0, -canvas.getHeight()/2);
}

percentOpen保存从01的值,以便当percentOpen减少时画布旋转直至变为不可见,并且当percentOpen增加时画布返回到原来的状态。

我能够为左右两种变体设置动画,我只是无法弄清楚我应该如何将画布分成两部分(或者我应该以某种方式将它划分?)来独立地为左右部分设置动画。

我尝试拍摄视图的位图,将其分成两个Bitmap并单独旋转它们,但这非常慢。 我会很高兴得到任何帮助。

如何独立旋转左右两侧?

1 个答案:

答案 0 :(得分:2)

试试这个自定义动画:

class V extends View implements OnClickListener {

    private A animation;
    private Paint paint0;
    private Paint paint1;
    private RectF rect;
    private Bitmap bitmap;

    public V(Context context) {
        super(context);
        paint0 = new Paint();
        paint0.setColor(0xffaa0000);
        paint1 = new Paint();
        paint1.setColor(0xffffffff);
        rect = new RectF();
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d(TAG, "onClick ");
        animation = new A(getWidth(), getHeight());
        startAnimation(animation);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int w = getWidth();
        int h = getHeight();
        if (animation != null && animation.hasStarted() && !animation.hasEnded()) {
            canvas.save();
            canvas.clipRect(0, 0, w / 2, h, Op.REPLACE);
            canvas.concat(animation.leftMatrix);
            drawStuff(canvas, w, h);
            canvas.restore();

            canvas.save();
            canvas.clipRect(w / 2, 0, w, h, Op.REPLACE);
            canvas.concat(animation.rightMatrix);
            drawStuff(canvas, w, h);
            canvas.restore();
        } else {
            drawStuff(canvas, w, h);
        }
    }

    public void drawStuff(Canvas canvas, int w, int h) {
        int s = Math.min(w, h);
        rect.set(0, 0, s, s);
        rect.offset((w - s) / 2, (h - s) / 2);
        canvas.drawRect(rect, paint0);

        rect.left += 64;
        rect.top += 64;
        canvas.drawOval(rect, paint1);
        canvas.drawBitmap(bitmap, rect.left, rect.top, null);
    }

    class A extends Animation {
        private Camera camera;
        private int w2;
        private int h2;
        public Matrix leftMatrix;
        public Matrix rightMatrix;

        public A(int w, int h) {
            camera = new Camera();
            leftMatrix = new Matrix();
            rightMatrix = new Matrix();
            w2 = w / 2;
            h2 = h / 2;
            setDuration(2000);
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            camera.save();
            float angle = 80 * interpolatedTime;

            camera.rotateY(angle);
            camera.getMatrix(leftMatrix);
            leftMatrix.preTranslate(-w2, -h2);
            leftMatrix.postTranslate(w2, h2);

            camera.rotateY(-2 * angle);
            camera.getMatrix(rightMatrix);
            rightMatrix.preTranslate(-w2, -h2);
            rightMatrix.postTranslate(w2, h2);

            camera.restore();
        }
    }
}