1

我有一个 FrameLayout,其中放置了两个相同的 TextView。我希望能够将第一个视图翻译到左侧(我已经完成并且正在像魅力一样工作)。但是我希望能够单击它下面的 TextView 来执行操作。

当我尝试单击底部的 TextView 时,顶部的 TextView 会再次被单击。我有一种感觉,这是因为动画的渲染方式和实际 x,y 位置的变化没有生效。

这就是我到目前为止所拥有的。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:text="@string/hello" 
        android:id="@+id/unbutt"
        android:gravity="right|center_vertical"
        />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:text="@string/hello" 
        android:id="@+id/butt" />

</FrameLayout>

编码:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class Main extends Activity implements AnimationListener, OnClickListener
{
    /** Called when the activity is first created. */

    private class BottomViewClick implements OnClickListener
{

    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(), "Second Click", 5).show();
    }

}

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

    TextView tv = (TextView)findViewById(R.id.butt); 
    tv.setBackgroundColor(0xffb71700);
    tv.setOnClickListener(this);

    TextView tv2 = (TextView)findViewById(R.id.unbutt); 
    tv2.setBackgroundColor(0xffb700ff);
    tv2.setOnClickListener(new BottomViewClick());

}

    private boolean revealed = false;

    @Override
    public void onClick(View v) {
        Animation a ;
        if(!revealed)
            a = new TranslateAnimation(0f, -200f, 0f, 0f);
        else
            a = new TranslateAnimation(-200f, 0f, 0f, 0f);
        a.setDuration(500);
        a.setFillAfter(true);
        a.setAnimationListener(this);
        v.startAnimation(a);
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        if(revealed)
            revealed = false;
        else
            revealed = true;
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationStart(Animation animation) {
    }
}
4

1 回答 1

4

您的标签中有 ICS,所以我认为这就是您的目标。在这种情况下,Animation您使用的对象基本上已被弃用,取而代之的是Animator类。旧的做法只是移动了视觉位置,View而物理位置保持不变。您必须自己通过操纵视图的边距来移动它。ObjectAnimator另一方面,使用 an可以让您物理地移动对象及其视觉组件。

http://android-developers.blogspot.com/2011/05/introducing-viewpropertyanimator.html

于 2012-02-07T21:34:24.427 回答