Android的:其中,包括>与RippleEffect&安培; StateListAnimator [英] Android: <include> with RippleEffect & StateListAnimator

查看:225
本文介绍了Android的:其中,包括>与RippleEffect&安培; StateListAnimator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,包括其他布局:

I have a layout, that includes another layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:id="@+id/layout1">

    <include layout="@layout/my_layout"/>
</LinearLayout>

我需要一个RippleEffect以及一个StateListAnimator添加到包含布局

I need to add a RippleEffect as well as a StateListAnimator to the included layout.

例如:

<include layout="@layout/my_layout"
          android:stateListAnimator="@anim/lift_up"
          android:background="@drawable/ripple_effect"/>

无论是RippleEffect和StateListAnimator工作100%。我不能改变包括布局。因此,为什么我需要做的影响无论是在包括标签或父布局本身的原因。

Both the RippleEffect and StateListAnimator work 100%. I cannot alter the included layout. Thus the reason why I need to do the effects either on the include tag or the parent layout itself.

我已经试过这两种技术,其中没有一个是成功的。

I have tried both techniques, none of which have been successful.

更新

如果可能的话,这应该是向下编程。

If possible, this should be down programmatically.

更新2

其次,我将如何去保持视图升高,一旦动画?

推荐答案

根据该线程<一个href=\"http://stackoverflow.com/questions/2631614/does-android-xml-layouts-include-tag-really-work\">Does Android的XML布局的'包括'标签真的有用吗?和<一个href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/org.robolectric/android-all/4.4_r1-robolectric-1/android/view/LayoutInflater.java#777\"相对=nofollow> LayoutInflater.java 的似乎&LT;&包括GT; 标签仅支持的android:ID 布局_ * 的android:能见度属性。所以,你的code设置背景 stateListAnimator 没有任何效果。

Based on the thread Does Android XML Layout's 'include' Tag Really Work? and LayoutInflater.java it seems <include> tag only supports android:id, layout_* and android:visibility attributes. So your code to set background and stateListAnimator have no effect.

要解决这个问题,下面用 @Stepane 的code:

To fix the below issue with @Stepane's code :

你的方法正确启用的连锁反应,但是SLA不
  被解雇了。我看不到任何标高正在发生

your method enables the ripple effect properly, however the SLA isn't fired. I cannot see any elevation taking place

如果膨胀的观点是透明的,那么抬高是不可见的,你必须设置 viewOutline 或者使用一些非透明色彩的观点看到阴影。

If the inflated view is transparent, then the elevation is not visible, you have to set viewOutline or use some non-transparent color for the view to see the shadow.

ViewOutlineProvider 文档提取物

接口,通过该视图建立它的轮廓,用于阴影投射
  裁剪。

Interface by which a View builds its Outline, used for shadow casting and clipping.

要设置 outlineProvider 您可以使用<一个href=\"http://developer.android.com/reference/android/view/View.html#setOutlineProvider(android.view.ViewOutlineProvider)\"相对=nofollow>查看#setOutlineProvider(ViewOutlineProvider提供商)方法,也可以通过的android:outlineProvider XML标记

To set the outlineProvider you can make use of View#setOutlineProvider (ViewOutlineProvider provider) method or you can set via android:outlineProvider xml tag.

更新code:

LinearLayout root =(LinearLayout) findViewById(R.id.container);
StateListAnimator sla = AnimatorInflater.loadStateListAnimator(this, R.animator.lift_up);
root.setStateListAnimator(sla);
root.setClickable(true);
root.setOutlineProvider(ViewOutlineProvider.PADDED_BOUNDS);
root.setBackground(ContextCompat.getDrawable(this, R.drawable.ripple_effect));

ripple_effect.xml

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:color="?android:colorAccent"
    tools:ignore="NewApi">
  <item>
    <shape
        android:shape="rectangle">
        <solid android:color="@android:color/transparent"/>            
        <!-- Doesn't require outlineProvider
        <solid android:color="@android:color/darker_gray"/>
        -->
    </shape>
  </item>
</ripple>

RES /动画/ lift_up.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item
      android:state_enabled="true"
      android:state_pressed="true">
    <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueTo="48dp"/>
  </item>
  <item>
    <objectAnimator
        android:duration="@android:integer/config_shortAnimTime"
        android:propertyName="translationZ"
        android:valueTo="0dp"/>
  </item>
</selector>

这篇关于Android的:其中,包括&GT;与RippleEffect&安培; StateListAnimator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆