首页 文章

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

提问于
浏览
1

在旋转动画之前它很棒,但我不喜欢圆圈在哪里,我得到了这个:

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已经完美居中,有没有办法“旋转后居中”而不必在布局上输入边距值?....我只是想用一个布局文件来解决它...... ..

2 回答

  • 0

    我相信你的构造函数是关闭的:

    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)

  • 2

    试试这个

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

相关问题