【问题标题】:Circular reveal over top of other views gives error "cannot start this animator on a detached view"在其他视图之上的圆形显示给出错误“无法在分离视图上启动此动画师”
【发布时间】:2015-09-18 02:11:27
【问题描述】:

我已经研究了几天,但没有得到解决。 this Question 中的问题与我的类似,但我也无法使用它解决我的问题。

我曾尝试像 whatsapp 一样获得圆形显示效果,其中显示的视图将在其他视图的顶部膨胀。在我的情况下,当我在当前java类的layout.xml中已经存在的布局上应用动画时,它工作正常,但问题是在我的例子中是 LInearLayout 的显示布局将推送其他内容(在它下面) 放下并为它腾出位置。

我在这种情况下的布局如下,

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<io.codetail.widget.RevealFrameLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
       <LinearLayout
           android:id="@+id/reveal_items"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"  >
           <LinearLayout
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:gravity="center"
               android:orientation="vertical">
               <ImageButton
                   android:layout_width="70dp"
                   android:layout_height="70dp"
                   android:background="@drawable/icon_camera" />
               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="16dp"
                   android:text="Gallery" />
           </LinearLayout>
           <!-- Other 2 icons here-->

       </LinearLayout>
   </io.codetail.widget.RevealFrameLayout>

   <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter Name" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="OK" />

线性布局/>

我的显示布局将按下 EditText 和按钮为其腾出位置,但我想显示在它们的顶部..

然后我尝试将显示视图代码放在另一个reveal-layout.xml 文件中,并将该布局扩展为在javacode 中创建的视图(在我的情况下为LinearLayout)。然后尝试在此 LinearLayout 上应用显示效果,但它给了我这个“无法在分离视图上启动此动画师” 异常。

我的revealing-layout.xml 看起来像

<io.codetail.widget.RevealFrameLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content">
       <LinearLayout
           android:id="@+id/reveal_items"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal"  >
           <LinearLayout
               android:layout_width="0dp"
               android:layout_height="wrap_content"
               android:layout_weight="1"
               android:gravity="center"
               android:orientation="vertical">
               <ImageButton
                   android:layout_width="70dp"
                   android:layout_height="70dp"
                   android:background="@drawable/icon_camera" />
               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="16dp"
                   android:text="Gallery" />
           </LinearLayout>
           <!-- Other 2 icons here-->

       </LinearLayout>
   </io.codetail.widget.RevealFrameLayout>

在按钮点击我调用函数revealView(); 如下所示,

public void revealView()
{
    int cx = (mRevealView.getLeft() + mRevealView.getRight());
    int cy = mRevealView.getTop();
    int radius = Math.max(mRevealView.getWidth(), mRevealView.getHeight());

    // and all animations here, the animator is working fine so i did not put the code here
} 

如果我在 MainActivity onCreate() 方法中初始化 mRevealView,mRevealView=(LinearLayout)findviewbyid(R.id.reveal_items) 并膨胀并为其分配视图,然后动画师将在此视图上正常工作。但是当我尝试创建新的布局时, mRevealView=new LinearLayout(this) 并设置其参数等所有内容,然后当我在其上应用 animator 时会出现此错误。 无法在分离视图上启动此动画。

我在这里做错了什么有人可以建议我。

【问题讨论】:

    标签: android animation circularreveal


    【解决方案1】:

    尝试将您的显示代码放在预期显示布局的ViewTreeObserver 中,如下所示:

    boolean reveal = false;
    button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reveal = true;
            }
        });
    
    LinearLayout layout = (LinearLayout) findViewById(R.id.reveal_items);
    layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {                        
                if(reveal) {
                    reveal = false;
                    // start reveal animation here
                    // ...
                }
                // for SDK >= 16
               ll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
     });
    

    【讨论】:

      【解决方案2】:

      为了使用你提到的whatsapp库之类的循环效果来实现类似whatsapp的效果,我首先将我的父视图设置为FrameLayout。这个父 Framelayout 包括两个孩子,第一个是 LinearLayout,第二个是 FrameLayout(这里是您要在用 LinearLayout 编写的视图顶部显示的代码)。正如我提到的,在第一个 LinearLayout 中编写您想要在屏幕上显示的所有代码,在第二个 FrameLayout 中编写显示效果代码。它只是使用 FrameLayout 属性。你已准备好出发。 您可以使用库描述中提供的相同 java 代码。

      【讨论】:

        猜你喜欢
        • 2015-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多