的 main.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)
);
我正在使用eclipse并在setDuration
和LayoutTransition
上显示红线。它显示的错误是:
*Syntax error on token "setDuration", Identifier expected after this token*
任何人都可以帮我解决。
答案 0 :(得分:0)
首先你的代码有很多问题。
onAddClick
方法)onCreate
方法之外大约10行。我认为你有问题,因为只是一个错误的右括号}
使得日食变暗并且对你大喊: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));
}
}