Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 围绕it'旋转图像视图;s中心_Java_Android_Imageview_Android Imageview - Fatal编程技术网

Java 围绕it'旋转图像视图;s中心

Java 围绕it'旋转图像视图;s中心,java,android,imageview,android-imageview,Java,Android,Imageview,Android Imageview,我想创建一个模拟时钟,我有一个问题。我使用3ImageView表示秒、分钟和小时指针。现在我想让它们都绕着中心旋转,但我做不到。如何通过提供围绕中心的角度来旋转图像视图?假设您的目标是API 11,最简单的方法是: view.setPivotX(view.getWidth() / 2); view.setPivotY(view.getHeight() / 2); float rotation = //some value between 0f and 360f view.setRotation

我想创建一个模拟时钟,我有一个问题。我使用3ImageView表示秒、分钟和小时指针。现在我想让它们都绕着中心旋转,但我做不到。如何通过提供围绕中心的角度来旋转图像视图?

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

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

float rotation = //some value between 0f and 360f
view.setRotation(rotation);

View类有一个可以完美工作的
.setrotation()
方法。只需将其旋转若干度,默认情况下,它将围绕中心旋转

编辑:


要在API级别低于11的情况下旋转图像,请参阅文章。

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

公共类VerticalTextView扩展了ImageView { 最终布尔自上而下=真

    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);

        }
}

您真的需要每个元素的imageview吗?