您如何在Android的TabLayout中制作动画?
作者:互联网
我已经用viewPager实现了四个选项卡的TabLayout.
这些选项卡具有customView,因为每个选项卡都有两个textView(文本看不见的计数).
因此,在更改选项卡时,我能够处理很多UI内容,但是,无法为看不见的textView设置动画.
我的自定义XML对于textView及其父级RelativeLayout均具有android:animateLayoutChanges =“ true”.
另外,我尝试在实际标签上设置此功能,尝试制作各种动画,但均无效.
ps-没有动画,我只是设置能见度(消失,可见).
private void updateUnseenOnTab(int position, int unseen) {
ViewGroup vg = (ViewGroup) mTabLayout.getChildAt(0);
ViewGroup vgTab = (ViewGroup) vg.getChildAt(position);
View view = vgTab.getChildAt(0);
if (view != null && view.findViewById(R.id.tab_unseen) instanceof TextView) {
final TextView unseenText = (TextView) view.findViewById(R.id.tab_unseen);
if (unseen <= 0) {
unseenText.setText("0");
Animation fadeOut = new AlphaAnimation(0, 1);
fadeOut.setDuration(1000);
unseenText.setAnimation(fadeOut);
} else {
String currentText = unseenText.getText().toString();
try {
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
unseenText.setAnimation(fadeIn);
unseenText.setText("" + ((TextUtils.isEmpty(currentText) ? 0 : unseen) + Integer.valueOf(currentText)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
解决方法:
我不知道您是否要为每个选项卡或每个选项卡中的子视图设置动画.这将以150ms的延迟使每个选项卡一个接一个地动画.如果要为一个选项卡中的每个孩子设置动画,只需将其置于另一个循环中即可.
tabs = (TabLayout) findViewById(R.id.tabs);
ViewGroup vg = (ViewGroup) tabs.getChildAt(0);
int tabsCount = vg.getChildCount();
for (int i = 0; i < tabsCount; i++)
{
int delay = (i * 150) + 750; //this is starting delay
ViewGroup vgTab = (ViewGroup) vg.getChildAt(i);
vgTab.setScaleX(0f);
vgTab.setScaleY(0f);
vgTab.animate()
.scaleX(1f)
.scaleY(1f)
.setStartDelay(delay)
.setInterpolator(new FastOutSlowInInterpolator())
.setDuration(450)
.start();
}
标签:android-tablayout,android-animation,android 来源: https://codeday.me/bug/20191120/2040517.html