我正在尝试使用幻灯片进/出动画实现片段事务.我正在开发至少14 sdk所以ObjectAnimator对我来说是唯一的选择(还有其他方法吗?根据我的理解,翻译动画不可用).
守则很简单:
AnimationView.java - 包装类
public class AnimationView extends LinearLayout {
public AnimationView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public float getYFraction() {
return getHeight();
}
public void setYFraction(float yFraction) {
final int height = this.getHeight();
setY((height > 0) ? (yFraction * height) : -9999);
}
}
Run Code Online (Sandbox Code Playgroud)
滑入和滑出xml
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="yFraction"
android:valueType="floatType"
android:valueFrom="-1"
android:valueTo="0"
android:duration="600"/>
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="yFraction"
android:valueType="floatType"
android:valueFrom="0"
android:valueTo="1"
android:duration="600"/>
Run Code Online (Sandbox Code Playgroud)
动画效果很好,你可以看到我使用了自定义属性yFraction.但性能非常糟糕......在某些平板电脑中,我注意到动画期间有一条水平的白线(大约3像素高),在其他智能手机设备中,它只是变慢(不是持续时间慢而是闪烁).
如果我将valueFrom&valueTo设置为适合视图维度,我找到了修复它的方法,但它不是通用的,并且需要0到1才能对所有视图都通用 …
android android-animation android-fragments objectanimator fragmenttransaction
我正在使用ObjectAnimator并且无法弄清楚如何仅在一个方向上向x轴缩放,例如向右侧.因为当我缩放它时,它可以两种方式缩放,然后我的ImageView不在屏幕上.我知道我可以添加transitionX以防止离开屏幕,但更好的解决方案是在一个方向上缩放.
ObjectAnimator headAnim = ObjectAnimator.ofFloat(imageHead, "scaleX", 1f, 1.5f);
headAnim.setDuration(1000);
headAnim.start();
Run Code Online (Sandbox Code Playgroud) 一旦我的应用程序同时运行~4 +动画,动画开始滞后一点.有什么办法可以解决/优化吗?我正在使用ObjectAnimator和ValueAnimator.
我想在我的应用程序中添加一些动画.请找到附带的照片以获得动画创意.
我用过CoordinatorLayout.我的搜索布局和工具栏托管在AppBarLayout/CollapsingToolbarLayout中.下面是我的NestedScrollView托管片段.
当用户使用滚动标志滚动时,我能够隐藏搜索布局.我正在尝试实现一个CoordinatorLayout.Behavior,它将开始向左移动购物车图标并开始搜索图标从0到1缩放; 一旦用户滚动并且搜索布局开始隐藏.当搜索布局完全隐藏时,我想在第二个屏幕中显示工具栏.当用户向下滚动并且通过向右移动推车并缩小搜索图标开始显示搜索布局时,它将返回到原始状态,如第一个屏幕所示.
最初我将工具栏上的搜索图标设为隐藏,购物车图标和搜索图标位于线性布局内.
请找到我的xml activity_home_page.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.ui.customviews.CustomDrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/ab_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="131dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:id="@+id/ll_search_layout"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_gravity="bottom"
android:orientation="vertical"
android:visibility="visible"
app:layout_scrollFlags="scroll|snap">
<LinearLayout
android:id="@+id/ll_search_box"
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_marginBottom="@dimen/dimen_12_dp"
android:layout_marginLeft="@dimen/dimen_8_dp"
android:layout_marginRight="@dimen/dimen_8_dp"
android:layout_marginTop="@dimen/dimen_12_dp"
android:background="@drawable/round_corner_layout_white"
android:elevation="@dimen/dimen_5_dp"
android:focusableInTouchMode="true"
android:orientation="horizontal">
<EditText
android:id="@+id/et_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/dimen_15_dp"
android:layout_weight="80"
android:background="@android:color/transparent"
android:hint="Search for Products"
android:padding="@dimen/dimen_5_dp"
android:textColor="@color/black"
android:textColorHint="@color/hint_text_color"
android:textSize="@dimen/dimen_text_16_sp" />
<ImageView
android:id="@+id/iv_search_icon"
android:layout_width="@dimen/dimen_22_dp"
android:layout_height="@dimen/dimen_24_dp"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/dimen_17_dp"
android:layout_marginRight="@dimen/dimen_20_dp"
android:src="@drawable/ic_search_grey" />
</LinearLayout>
</LinearLayout>
<include
layout="@layout/layout_primary_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_primary_height"
android:layout_gravity="top" …Run Code Online (Sandbox Code Playgroud) 我有一个动画师infinite_rotation,定义为:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="rotation"
android:repeatCount="infinite"
android:valueFrom="0"
android:valueTo="360"
android:duration="2000" />
</set>
Run Code Online (Sandbox Code Playgroud)
当时间(时间不确定)到来时我不再需要这个,我打电话infinite_animator.cancel().然后在其布局容器上播放fade_out动画:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:propertyName="alpha"
android:repeatCount="0"
android:valueTo="0.0"
android:duration="1000" />
</set>
Run Code Online (Sandbox Code Playgroud)
生成的动画是,旋转按预期停止,但在淡出时停顿.我错过了什么?
以下是它的外观:
更新:我在使用Kitkat OS的旧三星Tab 4上测试了上述问题.我刚刚使用Marshmallow OS测试了一款相当新的三星Tab 4.动画效果很好.所以我想更好的问题是如何修复旧设备/操作系统上的草率动画?
这是动画调用:
private void animateRefreshButton() {
ImageView iv_refresh = (ImageView) findViewById(R.id.iv_refresh);
if(infinite_animator == null) {
infinite_animator = AnimatorInflater.loadAnimator(this, R.animator.refresh_rotate);
}
infinite_animator.setTarget(iv_refresh);
infinite_animator.start();
}
Run Code Online (Sandbox Code Playgroud)
hideRefreshButton()应用程序确定刷新完成时启动.这是取消和淡出动画的调用:
private void hideRefreshButton() {
if(infinite_animator != null) {
infinite_animator.cancel();
}
Animator anim = AnimatorInflater.loadAnimator(this, R.animator.refresh_fadeout);
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator …Run Code Online (Sandbox Code Playgroud) 我目前正在尝试实现类似于“格子”中的SearchView动画,但是遇到一个问题,因为在我的情况下,该应用程序应保持相同的活动,因此我需要使用ObjectAnimator将搜索图标移到后退图标。Animated Vector Drawable工作正常。
我确实尝试了到目前为止实现移动的方法:首先,我将Animated Vector Drawable放在MenuItem内。如果我告诉ObjectAnimator将该项目移到左侧,则动画开始后它将被隐藏(可能在某些工具栏元素的后面)。由于无法解决此问题,我决定尝试将ImageButton放在带有FrameLayout的工具栏上方,并告诉该ImageButton移至右侧,同时启动AVD动画,该方法可以正常工作。现在的问题是,如果我尝试让MenuItems X和Y将ImageButton放在MenuItem上方,我的应用程序将崩溃,并显示NullPointerException,指出找不到X和Y。
您看到我的问题的解决方案了吗?我要实现的动画是,搜索可绘制对象移至汉堡包图标(当打开搜索视图时,当按下“搜索”按钮时,它会显示一个搜索图标)。
我ObjectAnimator用来滑动背景图像以显示listView下面的图像.该listView具有weight的0.7f这样,这将是对所有屏幕尺寸相同的比例.
使用ObjectAnimator是否可以以相同的0.7比例向上滑动我的背景图像,在所有屏幕上都相同?
背景图片和listView:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3" >
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"
android:background="#303030"
android:divider="#555555"
android:dividerHeight="1dp" >
</ListView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
动画代码:
FrameLayout mainView = (FrameLayout)findViewById(R.id.mainView);
ObjectAnimator mover = ObjectAnimator.ofFloat(mainView, "translationY", !historyShown ? -500 : 0);
mover.setDuration(300);
mover.start();
Run Code Online (Sandbox Code Playgroud) 我有一个Sliding Fragments DevByte的实现.除了将片段滑入视图之外,我想在它遮挡的内容上画一个阴影.我已经将屏幕宽度的两倍FractionalLinearLayout从视频改为measure自身,layout而右半部分的子节点也是如此.在动画循环期间,我在左半部分绘制一个增加alpha的黑色矩形,并将我的X坐标设置为负值,使右半部分进入视图.
我的问题是,当我将内容滑入视图时,这可以正常工作,但是当我将内容滑回视图时失败.在路上,我得到了我想要的行为:翻译和变暗的阴影.在出路的时候,我只得到了翻译,阴影仍然只是动画中的第一帧颜色.
我在日志记录中看到的是,在进入的过程中,setPercentOnScreen()和onDraw()方法被交替调用,正如预期的那样.然而,在出路的时候,我接到一个电话setPercentageOnScreen(),接着是一个电话onDraw(),然后只接听电话setPercentOnScreen().Android正在优化绘图,但我无法弄清楚原因.
更新:有趣的是,我只在运行Android 4.4的模拟器上看到此行为.运行4.0.3和4.3的仿真器在两个方向上按预期运行动画.一个旧的Nexus 7出现了问题,另一个不同的仿真器4.4.它似乎在设备上是一致的,但在设备之间有所不同.
再次更新:我已经提取了一个示例项目并将其放在GitHub上:barend/android-slidingfragment.GitHub上的自述文件包含十几个设备上的测试结果.对于模拟器,问题与"启用主机GPU"功能相关,但仅限于Jelly Bean和KitKat; 不在ICS上.
再次更新:进一步测试显示问题发生在运行Jelly Bean及更高版本的物理设备上,以及启用了"使用主机GPU"运行Jelly Bean或更高版本的ARM仿真器上.无论Android版本如何,在没有"使用主机GPU"的x86仿真器和ARM仿真器上都不会发生这种情况.我的测试的确切表可以在上面链接的github项目中找到.
// Imports left out
public class HorizontalSlidingLayout extends FrameLayout {
/**
* The fraction by which the content has slid into view. Legal range: from 0.0 (all content
* off-screen) to 1.0 (all content visible).
*/
private float percentOnScreen;
private int screenWidth, screenHeight, …Run Code Online (Sandbox Code Playgroud) 我在这里有这个自定义的“底部操作栏”:https : //youtu.be/TPi5jtcs2wE,它在某些类型的网页(例如,文章/非文章)中出现和消失。我将最外面的设置为LinearLayout,animateLayoutTransition并制作了另一个LayoutTransition对象,但是我希望条形图在Web视图调整其高度的同时消失。需要明确的是,条形图(relativeLayout)设置为,View.GONE并且由于条形图消失了,因此webView应该扩展为匹配父级(由于layout_weight),但是两者不能同时进行。我尝试更改LayoutTransition.setDuration()和.setStartDelay()。
articleActivity xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/container_article"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app.morningsignout.com.morningsignoff.ArticleActivity"
tools:ignore="MergeRootFrame"
android:background="@android:color/white"
android:animateLayoutChanges="true">
<app.com.morningsignout.morningsignout.CustomWebView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/webView_article"
android:layout_gravity="center"
android:layout_weight="9" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_weight="0"
Run Code Online (Sandbox Code Playgroud)
LayoutTransition的配置:
LinearLayout container = (LinearLayout) findViewById(R.id.container_article);
LayoutTransition customTransition = new LayoutTransition();
customTransition.enableTransitionType(LayoutTransition.CHANGING);
customTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
customTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
customTransition.setAnimator(LayoutTransition.APPEARING, showArticleBar);
customTransition.setAnimator(LayoutTransition.DISAPPEARING, hideArticleBar);
customTransition.setStartDelay(LayoutTransition.APPEARING, 0);
customTransition.setStartDelay(LayoutTransition.DISAPPEARING, 0);
customTransition.setStartDelay(LayoutTransition.CHANGING, 0);
customTransition.setDuration(LayoutTransition.APPEARING, showArticleBar.getDuration());
customTransition.setDuration(LayoutTransition.DISAPPEARING, hideArticleBar.getDuration());
customTransition.setDuration(LayoutTransition.APPEARING, showArticleBar.getDuration());
container.setLayoutTransition(customTransition);
Run Code Online (Sandbox Code Playgroud)
hide_action_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:propertyName="translationY"
android:duration="300" …Run Code Online (Sandbox Code Playgroud) 另一个我似乎无法解决的简单问题.我想要一个使用ObjectAnimator的动画从中心向上缩放.但是,我不确定如何设置PivotX/Y属性,因为我应用的任何值似乎都不会影响视图.当我使用scaleanimation时,它工作正常,但我必须在这里使用ObjectAnimator.
我试过了
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view,"scaleX",0.0f,1.0f);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view,"scaleY",0.0f,1.0f);
//I've tried a wide range of values from 0,0 to 0,0.5, to 0.5, 0.5 but none of them do anything
ObjectAnimator pivotX = ObjectAnimator.ofFloat(view,"pivotX",0,1f);
ObjectAnimator pivotY = ObjectAnimator.ofFloat(view,"pivotY",0,1f);
//I've also tried view.setPivotX(0.5f) but that didn't do anything either
animatorSet.playTogether(scaleX,scaleY,pivotX,pivotY);
animatorSet.start();
Run Code Online (Sandbox Code Playgroud)
我只是不确定如何让它从中心扩展.我已经尝试过甚至不使用枢轴,但也没有做任何事情.
任何帮助表示赞赏,
谢谢
**编辑**
以下类型的作品,除了它没有完全居中,而是向左上方增长但仍然有点居中.很难描述.我尝试使用0.5f,它也没用
ObjectAnimator pivotX = ObjectAnimator.ofFloat(view,"pivotX",1f);
ObjectAnimator pivotY = ObjectAnimator.ofFloat(view,"pivotY",1f);
Run Code Online (Sandbox Code Playgroud)