CountView类似计数器的实现
阅读原文时间:2021年04月20日阅读:1

这里使用的是 github上面第三方的

先看效果图

布局

  <com.github.premnirmal.textcounter.CounterView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/counter_view_margin"
        android:text="@string/zero"
        android:textColor="#ffffff"
        android:textSize="100sp" />

    <com.tescoo.marcwln.widget.ClearEditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="15dp"
        android:background="@android:color/white" />

    <Button
        android:id="@+id/btn"
        android:text="开始"
        android:background="@color/menu_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

然后是activity文件

 CounterView counter;
    ClearEditText edit;
    int value;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.count_view);
        counter = (CounterView) findViewById(R.id.counter);
        Typeface tf = Typeface.createFromAsset(getAssets(),
                "Uni_Sans_Light.otf");

        counter.setTypeface(tf);
        counter.setAutoFormat(false);
        counter.setFormatter(new Formatter() {
            @Override
            public String format(String prefix, String suffix, float value) {
                return prefix
                        + NumberFormat.getNumberInstance(Locale.US).format(
                        value) + suffix;
            }
        });
        counter.setAutoStart(false);//不让它自动开始

        edit = (ClearEditText) findViewById(R.id.edit);
        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                value = Integer.parseInt(edit.getText().toString().trim());
                setScore(value);
            }
        });

    }

    private void setScore(int value) {
        counter.setStartValue(0.0f);
        counter.setEndValue(value);
        counter.setIncrement(1.0f);
        counter.start();
//        startRingAnim(value);
    }