4

我想创建一个模拟时钟,但我遇到了问题。我使用 3 imageview 来表示秒针、分针和时针。现在我想让他们每个人都围绕中心旋转,但我不能。如何通过给出围绕中心的角度来旋转图像视图?

4

3 回答 3

7

假设您的目标是 API 11,最简单的方法是:

view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight() / 2);

float rotation = //some value between 0f and 360f
view.setRotation(rotation);
于 2013-06-12T17:05:48.287 回答
0

下面是旋转 textview 的代码。它也适用于 API 8。它只是您需要创建它的对象的自定义文本视图。并且需要设置旋转角度(如果需要也可以平移。)

公共类 VerticalTextView 扩展 ImageView { final boolean topDown=true;

    float iRotateAngel, iSetX, iSetY;

    int iIndex;

    Context context;

    public int getiIndex()
        {
            return iIndex;
        }

    public void setiIndex(int iIndex)
        {
            this.iIndex = iIndex;
        }

    public float getiRotateAngel()
        {
            return iRotateAngel;
        }

    public void setiRotateAngel(float iRotateAngel)
        {
            this.iRotateAngel = iRotateAngel;
        }

    public float getiSetX()
        {
            return iSetX;
        }

    public void setiSetX(float iSetX)
        {
            this.iSetX = iSetX;
        }

    public float getiSetY()
        {
            return iSetY;
        }

    public void setiSetY(float iSetY)
        {
            this.iSetY = iSetY;
        }

    public boolean isTopDown()
        {
            return topDown;
        }



    public VerticalTextView(Context context)
        {
            super(context);
            this.context = context;
        }

    public VerticalTextView(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            super.onMeasure(heightMeasureSpec, widthMeasureSpec);
            setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
        }

    @Override
    protected boolean setFrame(int l, int t, int r, int b)
        {
            return super.setFrame(l+32, t+12, l + (b - t)+32, t + (r - l)+12);
        }
    @Override
    public void draw(Canvas canvas)
        {
            if (topDown)
                {

                    canvas.translate(this.iSetX, this.iSetY);
                    canvas.rotate(this.iRotateAngel);
                }
            else
                {

                    canvas.translate(this.iSetX, this.iSetY);
                    canvas.rotate(this.iRotateAngel);
                }
            canvas.clipRect(0,0,getWidth(), getHeight(), android.graphics.Region.Op.REPLACE);
            super.draw(canvas);

        }
}
于 2014-03-04T10:20:48.957 回答
-1

View 类有一个.setRoatation()可以完美运行的方法。只需将其传递几度即可旋转,默认情况下,它将围绕中心旋转。

编辑:

对于 API 级别低于 11 的旋转图像,请参阅这篇文章。

于 2013-06-12T17:05:57.177 回答