【问题标题】:android what should be pivot point to rotate image around its center of baseandroid什么应该是枢轴点以围绕其基础中心旋转图像
【发布时间】:2015-03-26 13:21:34
【问题描述】:

请在标记重复或关闭之前仔细阅读整个问题

我想围绕其中心点旋转图像(特别是箭头图像)。

例如一开始,我的形象就像 9 点钟的秒针。 假设我将该图像旋转 30 度,它应该看起来像 10 上的时钟秒针,如果 120 度,它应该看起来像 1 上的时钟秒针。

所以我想围绕它的中心(沿 x 轴)旋转该图像。

如果我第一次编码,我应该将什么作为枢轴(X & Y) 传递

imageView.setPivotX(1f);
            imageView.setPivotY(1f);
            imageView.setRotation(-30);

或第二个代码

Matrix matrix = new Matrix();
    imageView.setScaleType(ScaleType.MATRIX);
    matrix.postRotate((float) 20, 0f, 0f);
    imageView.setImageMatrix(matrix);

或第三个代码

Bitmap myImg = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_0_degree);
    Matrix matrix = new Matrix();
    matrix.postRotate(30);
    Bitmap rotated = Bitmap.createBitmap(myImg, 0, 1, myImg.getWidth(), myImg.getHeight(), matrix, true);
    imageView.setImageBitmap(rotated);

或第四个代码

final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f,
        RotateAnimation.RELATIVE_TO_SELF, 0.5f);

rotateAnim.setDuration(0);
rotateAnim.setFillAfter(true);
imgview.startAnimation(rotateAnim);

为了更好地理解顺时针旋转了 90 度,添加了一张图片。

我希望未来谷歌会添加更多关于枢轴点的清晰文档。

提前致谢。

【问题讨论】:

    标签: android image rotation image-rotation rotateanimation


    【解决方案1】:

    第四个代码你几乎是对的^^

    你可以这样实现:

        final RotateAnimation rotateAnim = new RotateAnimation(0.0f, 30,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 1f);
        rotateAnim.setDuration(0);
        rotateAnim.setFillAfter(true);
        mImageView.setAnimation(rotateAnim);
        rotateAnim.start();
    

    【讨论】:

    • setFillAfter(true) 完成了什么?
    • 动画结束后,视图停留在动画结束的地方
    • 我试过这段代码。不幸的是,它不适合我。你能建议为什么它不工作吗?
    猜你喜欢
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 2011-07-08
    • 2014-01-05
    相关资源
    最近更新 更多