在 Android 中用动画旋转后如何将圆形进度条居中?

How to center a circular progress bar after rotating it with an animation in Android?

在旋转动画之前它很棒,但我不喜欢圆圈开始的地方,我得到这个:

http://s3.postimg.org/ptnap4h8h/cnocorrido.png

应用旋转动画后我得到这个:

我有一个圆形进度条,我必须旋转它才能使其从底部开始并在底部结束

循环进度条:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0"
            android:useLevel="true">
            <gradient
                android:startColor="#fb0000"
                android:endColor="#00FF00"
                android:centerColor="#fbf400"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>

它在圆圈中间有一个文本视图 (answerCountdown),显示剩余的秒数。效果很好。

layout.xml

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">

    <ProgressBar
        android:id="@+id/barTimer"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="120dp"
        android:layout_height="131dp"
        android:indeterminate="false"
        android:indeterminateOnly="false"
        android:max="100"
        android:progressDrawable="@drawable/circular_progress_bar"
        android:layout_gravity="center_horizontal"
        android:layout_below="@+id/gameCountDown"
        android:layout_alignParentStart="false"
        android:layout_alignParentEnd="false"
        android:layout_alignWithParentIfMissing="false"
        android:layout_alignParentRight="false"
        android:layout_alignParentBottom="false"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:id="@+id/answerCountdown"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="47dp"
        android:layout_below="@+id/gameCountDown"
        android:layout_alignParentStart="false"
        android:layout_alignParentEnd="false"
        android:layout_alignWithParentIfMissing="false"
        android:layout_alignParentRight="false"
        android:layout_alignParentBottom="false"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

但是我应用了一个旋转动画后它没有居中,它会根据你旋转的角度向左或向右移动,倒计时计时器上的代码

 Animation an = new RotateAnimation(0.0f, 90.0f, 250f, 273f);
    an.setFillAfter(true);
    barTimer.startAnimation(an);

textView 已经完全居中,有没有什么方法可以 "center after rotation" 而不必在布局上输入边距值?....我现在只想用一个布局文件解决这个问题。 .......

我相信您的构造函数已关闭:

Animation an = new RotateAnimation(0.0f, 90.0f, 250f, 273f);

第 3 个和第 4 个参数表示旋转枢轴点,应该看起来更像以下内容

Animation an = new RotateAnimation(0.0f, 90.0f, .5f, .5f);

上面的意思是:从 0 度旋转到 90 度,X 和 Y 轴心点位于图像的 50% 处。

来源:Android Documentation: RotateAnimation(float, float, float, float)

试试这个

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@android:color/holo_blue_bright">

    <ProgressBar
        android:id="@+id/barTimer"
        android:progressDrawable="@drawable/circular_progress_bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="120dp"
        android:layout_height="131dp"
        android:layout_gravity="center"
        android:indeterminate="false"
        android:indeterminateOnly="false"
        android:max="100"
        />

    <TextView
        android:id="@+id/answerCountdown"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="jfi"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</FrameLayout>