Android:圆形progessbar的倒计时不是时钟

时间:2015-12-14 12:19:02

标签: android progress-bar countdowntimer

我用开始和停止按钮创建了一个cotuntdown。 textview应位于屏幕中间,并且此textview周围显示一个循环进度条。

我的问题是,进度条的进度是逆时针方向,并且不是从顶部开始(手表的12点钟位置),并且不显示背景。

进度条的设计应该是浅灰色背景和暗灰色进度。

这是我的main_activity:



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="letscho.timer.MainActivity">


    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="300dip"
        android:layout_height="300dip"
        android:indeterminate="false"
        android:progressDrawable="@drawable/circularprogressbar"
        android:background="@drawable/circularprogressbar_background"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:max="60"
        android:progress="0" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewTime"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="80sp"
        />


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnStart"
            android:layout_marginRight="12dp"
            android:layout_marginLeft="12dp"
            android:layout_marginTop="12dp"
            android:textAlignment="center"
            android:text="Start"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnStop"
            android:layout_below="@+id/textViewTime"
            android:layout_marginRight="12dp"
            android:layout_marginLeft="12dp"
            android:layout_marginBottom="12dp"
            android:text="Stop"
            />
    </LinearLayout>
</RelativeLayout>
&#13;
&#13;
&#13;

我的进度条画面:

&#13;
&#13;
<?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="2.5"
            android:shape="ring"
            android:thicknessRatio="19.0"
            android:useLevel="true"

            >
            <gradient
                android:startColor="#444444"
                android:endColor="#444444"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>
&#13;
&#13;
&#13;

&#13;
&#13;
<?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="2.5"
            android:shape="ring"
            android:thicknessRatio="19.0"
            android:useLevel="true"
            >
            <gradient
                android:startColor="#bbbbbb"
                android:endColor="#bbbbbb"
                android:centerColor="#bbbbbb"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>
&#13;
&#13;
&#13;

java main:

&#13;
&#13;
package letscho.timer;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.concurrent.TimeUnit;


@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
public class MainActivity extends AppCompatActivity {

    int i=-1;
    Button btnStart, btnStop;
    TextView textViewTime;
    ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = (Button) findViewById(R.id.btnStart);
        btnStop = (Button) findViewById(R.id.btnStop);
        textViewTime = (TextView) findViewById(R.id.textViewTime);
        mProgressBar = (ProgressBar) findViewById(R.id.progressbar);

        textViewTime.setText("60");

        final CounterClass timer = new CounterClass(60000, 1000);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.start();
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.cancel();
            }
        });

    }

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    public class CounterClass extends CountDownTimer {
        /**
         * @param millisInFuture    The number of millis in the future from the call
         *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
         *                          is called.
         * @param countDownInterval The interval along the way to receive
         *                          {@link #onTick(long)} callbacks.
         */
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @SuppressLint("NewApi")
        @TargetApi(Build.VERSION_CODES.GINGERBREAD)
        @Override
        public void onTick(long millisUntilFinished) {

            long millis = millisUntilFinished;
            String s = String.format("%02d", TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            System.out.println(s);
            textViewTime.setText(s);
            mProgressBar.setProgress((int)(millisUntilFinished / 1000));

        }

        @Override
        public void onFinish() {
            textViewTime.setText("00");

        }
    }

}
&#13;
&#13;
&#13;

谢谢你的帮助!

0 个答案:

没有答案