我正在研究一个基本计数器,它将显示从 1 到 999,999 的数字。一个经过Button的onClick,认为应该更新,但我不知道怎么做出这样的动画(见图片)。
我对动画一无所知,我能找到的关于类似的东西是这样的:
您可以使用 ViewAnimator 类向计数器添加动画。它易于使用。您可以扩展它,在 onCreate() 中添加所有数字文本视图。实现处理增量操作的方法增量。只需调用 showNext 即可翻转到下一个视图。
虽然上面描述的内容似乎准确地描述了我的需要,但我不知道如何制作,因为我从未编写过任何动画。
我有 6 个TextView,一个挨着一个,每个都显示一个数字。问题本身是:
文本视图示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:gravity="center">
//This is a sample, consider 5 identical TextViews
<TextView
android:id="@+id/digit0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="@string/_0"
android:textSize="64sp"
android:textColor="#ffffff"
android:background="@drawable/gradient_number"
android:gravity="center"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
主活动截图
I ran into a problem while coding in Kotlin. I copy-pasted a java code sample that converts DP to Pixels, in order to place it as a parameter for setting padding programatically. I was expecting the IDE to automatically transform it all to Kotlin, however it failed in the process.
The code in Java looks like the following:
float scale = getResources().getDisplayMetrics().density;
int dpAsPixels = (int) (sizeInDp*scale + 0.5f);
Run Code Online (Sandbox Code Playgroud)
After the translation to Kotlin:
val scale = resources.displayMetrics.density
val …Run Code Online (Sandbox Code Playgroud) “无法智能地强制转换为“ GridLayoutManager”,因为“ ViewManager”是一个可变属性,可能此时已更改”
是AndroidStudio在尝试向我的RecyclerView添加分频器时显示的错误。
PixelsFragment.kt
class PixelsFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var viewManager: RecyclerView.LayoutManager
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private var listener: OnFragmentInteractionListener? = null
private lateinit var pixels: List<Pixel>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var numberOfColumns = 1
viewManager = GridLayoutManager(this.context, numberOfColumns)
viewAdapter = MyRecyclerViewAdapter(pixels)
recyclerView = view!!.findViewById<RecyclerView>(R.id.RVJanuary).apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
addItemDecoration(DividerItemDecoration(recyclerView.context, viewManager.getOrientation())) //Error on this line, underlining viewManager
}
}
}
Run Code Online (Sandbox Code Playgroud)
我找到了这个答案,它说这是可能引起的,因为变量可能恰好在null执行该行时出现,然后它们提供了3个解决方案。但是,据我所知, …