项目作者: rasheedsulayman

项目描述 :
🚀 A library that helps you animate change in numeric values in a TextView
高级语言: Kotlin
项目地址: git://github.com/rasheedsulayman/AnimatedCountTextView.git
创建时间: 2019-11-21T12:31:06Z
项目社区:https://github.com/rasheedsulayman/AnimatedCountTextView

开源协议:Apache License 2.0

下载


AnimatedCountTextView

A library that helps you animate change in numeric values in a TextView.

Build Status
Download

Gradle Dependency

Add the dependency to your app’s build.gradle:

  1. implementation 'com.r4sh33d:AnimatedCountTextView:0.0.1'

Usage

Add the AnimatedCountTextView to your layout.

  1. <com.r4sh33d.animatedcounttextview.AnimatedCountTextView
  2. app:startWith="0"
  3. app:endWith="100"
  4. app:duration="4000"
  5. app:suffix="%"
  6. app:numberType="integer"
  7. android:id="@+id/textView"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:text="0"
  11. android:textSize="30sp" ></com.r4sh33d.animatedcounttextview.AnimatedCountTextView>

Then call the start() method to start the count:

  1. countTextView.start()

You can optionally listen for the count end event by setting a CountEndListener

  1. countTextView.countEndListener(object : CountEndListener {
  2. override fun onCountFinish() {
  3. // Do something
  4. }
  5. })

If required, you can also stop the count at any time by calling the stop() method:

  1. countTextView.stop()

That’s all for basic usage. Your AnimatedCountTextView should animate form your startWith value to endWith value within the given time duration.

Customisation

AnimatedCountTextView attempts to use some default values to simplify the usage. The behaviour can be further
customized by setting the following attributes via xml or code.

Start and End values

You can use the xml attributes startWith and the endWith values to specify the value to animate from, and value to animate to, respectively.

  1. <com.r4sh33d.animatedcounttextview.AnimatedCountTextView
  2. ...
  3. app:startWith="0"
  4. app:endWith="100"></com.r4sh33d.animatedcounttextview.AnimatedCountTextView>

or programmatically:

  1. countTextView.startWith(0)
  2. countTextView.endWith(100)

Duration

You can specify the duration(in milliseconds) for the count-up or count-down animation using the duration attribute in xml.

  1. <com.r4sh33d.animatedcounttextview.AnimatedCountTextView
  2. ...
  3. app:duration="4000"></com.r4sh33d.animatedcounttextview.AnimatedCountTextView>

or programmatically:

  1. countTextView.duration(4000)

Number Type

You can use NumberType to specify the type of number you want to animate. You can either specify NumberType.Integer() or NumberType.Decimal(). You can also apply custom formatting to display the animated values. Custom formats can be specified by passing a DecimalFormat to NumberType.Integer() or NumberType.Decimal() constructor. The default NumberType is Integer.

  1. <com.r4sh33d.animatedcounttextview.AnimatedCountTextView
  2. ...
  3. app:numberType="integer"></com.r4sh33d.animatedcounttextview.AnimatedCountTextView>

Programmatically:

  1. countTextView.numberType(NumberType.Decimal(twoDecimalPlacesFormat))
  2. //or
  3. countTextView.numberType(NumberType.Integer())

Prefix and Suffix

You can specify prefix and/or suffix to the animated values. This is useful if you want to specify a currency symbol as a prefix or the percentage sign as the suffix.

  1. <com.r4sh33d.animatedcounttextview.AnimatedCountTextView
  2. ...
  3. app:suffix="%"
  4. app:prefix="$"></com.r4sh33d.animatedcounttextview.AnimatedCountTextView>

or programmatically:

  1. countTextView.prefix("$")
  2. //or
  3. countTextView.suffix("%")

Interpolator

You can specify the Interpolator to use when animating the values. This can only be done programmatically:

  1. countTextView.interpolator(AccelerateDecelerateInterpolator())

License

  1. Copyright (c) 2019 Rasheed Sulayman.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.