1

我正在学习 Java 和 Android SDK。我想创建一个简单的应用程序,它交换 2 个按钮,所以左边的按钮移动到右边的位置,并且 vv 带有简单的动画。我已经尝试过 ObjectAnimator,因为我已经读到它正在永久移动视图对象。但它不是:-( 对象停留在那里我的意思是左边的右边和 vv 但它们的 getLeft()、getTop() 值是相同的,并且在下一个动画开始后,对象会立即返回到起始位置。我已经读到 ObjectAnimator 需要一些额外的功能才能正常工作,但在文档中没有示例 :-( 我尝试添加 setTranslationX 功能,但没有奏效。有人可以提供一些简单的示例来说明如何做这个?

http://developer.android.com/guide/topics/graphics/prop-animation.html#object-animator

“您正在制作动画的对象属性必须具有 set() 形式的 setter 函数(在骆驼情况下)。因为 ObjectAnimator 在动画期间会自动更新属性,所以它必须能够使用此 setter 方法访问该属性。对于例如,如果属性名称是 foo,则需要有一个 setFoo() 方法。如果此 setter 方法不存在,则有三种选择: 如果您有权限,则将 setter 方法添加到类中。使用“

提前致谢。

4

1 回答 1

11

试试这个代码:

公共类 ActivityStartup 扩展 Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("hello");
    setContentView(R.layout.main);

    SlidingMenu menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.SLIDING_WINDOW);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setBehindOffset(100);
    menu.setFadeDegree(0.35f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);

    View view = G.layoutInflater.inflate(R.layout.menu, null);
    view.findViewById(R.id.layout_crop).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(G.context, "Crop Clicked", Toast.LENGTH_SHORT).show();
        }
    });

    view.findViewById(R.id.img_logo).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://example.com"));
            startActivity(browserIntent);
        }
    });

    menu.setMenu(view);
}

}

公共类 G 扩展应用程序 {

public static LayoutInflater layoutInflater;
public static Context        context;


@Override
public void onCreate() {
    super.onCreate();
    layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    context = getApplicationContext();
}

}

main.xml 是:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

menu.xml 是:

<ImageView
    android:id="@+id/img_logo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dip"
    android:scaleType="centerInside"
    android:src="@drawable/hlogo" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="2dip"
    android:background="#00ff00"
    android:src="@drawable/ic_launcher" />


<LinearLayout
    android:id="@+id/layout_crop"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dip"
        android:scaleType="centerInside"
        android:src="@android:drawable/ic_menu_crop" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Crop"
        android:textColor="#000000" />
</LinearLayout>


<LinearLayout
    android:id="@+id/layout_day"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dip"
        android:scaleType="centerInside"
        android:src="@android:drawable/ic_menu_day" />

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Day"
        android:textColor="#000000" />
</LinearLayout>


<LinearLayout
    android:id="@+id/layout_delete"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:gravity="center_vertical" >

    <ImageView
        android:id="@+id/ImageView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dip"
        android:scaleType="centerInside"
        android:src="@android:drawable/ic_menu_delete" />

    <TextView
        android:id="@+id/TextView03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete"
        android:textColor="#000000" />
</LinearLayout>

于 2014-06-08T20:04:48.320 回答