0

主要的.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     
android:layout_width="match_parent"    android:layout_height="match_parent"     
android:gravity="center_horizontal"     android:orientation="vertical" >
  <Button   android:id="@+id/button_add"         
     android:layout_width="wrap_content"   android:layout_height="wrap_content" 
     android:onClick="onAddClick" android:text="Click To Add Item" 
  />  
<LinearLayout  android:id="@+id/verticalContainer"         
android:layout_width="match_parent"  android:layout_height="match_parent"         
android:animateLayoutChanges="true"   android:orientation="vertical" 
/>  
</LinearLayout> 
**MainActivity.java in src directory**

       package com.example.p171;
    import android.os.Bundle;
    import android.animation.Animator;
    import android.animation.LayoutTransition;
    import android.animation.ObjectAnimator;
    import android.animation.PropertyValuesHolder;
    import android.app.ActionBar.LayoutParams;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.LinearLayout;

    public class MainActivity extends Activity 
    {      
        LinearLayout mContainer;      

        @Override     
        public void onCreate(Bundle savedInstanceState) 
        {         
            super.onCreate(savedInstanceState);         
            setContentView(R.layout.main);          

            // Layout Changes Animation         
            mContainer = (LinearLayout) findViewById(R.id.verticalContainer);         
            LayoutTransition transition = new LayoutTransition();         

            mContainer.setLayoutTransition(transition);                  

            // Override the default appear animation with a flip in         
            Animator appearAnim = ObjectAnimator.ofFloat(null, "rotationY", 90f, 0f).setDuration(transition.getDuration(LayoutTransition.APPEARING));         
            transition.setAnimator(LayoutTransition.APPEARING, appearAnim);                  

            // Override the default disappear animation with a flip out         
            Animator disappearAnim = ObjectAnimator.ofFloat(null, "rotationX", 0f, 90f).setDuration(transition.getDuration(LayoutTransition.DISAPPEARING));     transition.setAnimator(LayoutTransition.DISAPPEARING,disappearAnim);                  

            // Override the default change animation with a more animated slide 
        }

    // We are animating several properties at once, so we create an animation         
    // out of multiple PropertyValueHolder objects.  This animation slides the         
    // views in and temporarily shrinks the view to half size.         
    PropertyValuesHolder pvhSlide = PropertyValuesHolder.ofFloat("y", 0, 1);
    PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f, 1f);
    PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f, 1f); 

    Animator changingAppearingAnim = ObjectAnimator.ofPropertyValuesHolder(this, pvhSlide, pvhScaleY, pvhScaleX);

    changingAppearingAnim.setDuration(
            transition.getDuration(LayoutTransition.CHANGE_DISAPPEARING)
            );
    transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,changingAppearingAnim); 
    }


    public void onAddClick(View v) 
    {         
        Button button = new Button(this);         
        button.setText("Click To Remove");         
        button.setOnClickListener(new View.OnClickListener() 
        {             
            @Override             
            public void onClick(View v) 
            {                 
                mContainer.removeView(v);
                }         
            });          

        mContainer.addView(button, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));     
        } 
    } 

但是下面的行显示错误:

changingAppearingAnim.setDuration(transition.getDuration(
                                      LayoutTransition.CHANGE_DISAPPEARING)
                                   );

setDuration我正在使用 eclipse 并且它在and上显示红线LayoutTransition。它显示的错误是:

 *Syntax error on token "setDuration", Identifier expected after this token*

任何人都请帮我解决它。

4

1 回答 1

0

首先,您的代码有很多问题。

  1. 方法应该在一个类中。他们不可能无处不在:)(onAddClick方法)
  2. 你不能在方法之外做任何事情。你在方法之外大约 10 行onCreate

我认为您遇到问题是因为右括号错误}导致日食变暗并向您大喊:p

这是带有右缩进的固定代码:

import android.os.Bundle;
import android.animation.Animator;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity 
{      
    LinearLayout mContainer;      

    @Override     
    public void onCreate(Bundle savedInstanceState) 
    {         
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.main);          

        // Layout Changes Animation         
        mContainer = (LinearLayout) findViewById(R.id.verticalContainer);         
        LayoutTransition transition = new LayoutTransition();         

        mContainer.setLayoutTransition(transition);                  

        // Override the default appear animation with a flip in         
        Animator appearAnim = ObjectAnimator.ofFloat(null, "rotationY", 90f, 0f).setDuration(transition.getDuration(LayoutTransition.APPEARING));         
        transition.setAnimator(LayoutTransition.APPEARING, appearAnim);                  

        // Override the default disappear animation with a flip out         
        Animator disappearAnim = ObjectAnimator.ofFloat(null, "rotationX", 0f, 90f).setDuration(transition.getDuration(LayoutTransition.DISAPPEARING));     transition.setAnimator(LayoutTransition.DISAPPEARING,disappearAnim);                  

        // Override the default change animation with a more animated slide 
        // We are animating several properties at once, so we create an animation         
        // out of multiple PropertyValueHolder objects.  This animation slides the         
        // views in and temporarily shrinks the view to half size.         
        PropertyValuesHolder pvhSlide = PropertyValuesHolder.ofFloat("y", 0, 1);
        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f, 1f);
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f, 1f); 

        Animator changingAppearingAnim = ObjectAnimator.ofPropertyValuesHolder(this, pvhSlide, pvhScaleY, pvhScaleX);

        changingAppearingAnim.setDuration(
                transition.getDuration(LayoutTransition.CHANGE_DISAPPEARING)
                );
        transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,changingAppearingAnim); 
    }
    public void onAddClick(View v) 
    {         
        Button button = new Button(this);         
        button.setText("Click To Remove");         
        button.setOnClickListener(new View.OnClickListener() 
        {             
            @Override             
            public void onClick(View v) 
            {                 
                mContainer.removeView(v);
            }         
        });          

        mContainer.addView(button, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));     
    } 
} 
于 2013-07-04T19:12:54.003 回答