android - 向上/向下滑动动画 Textview onClickListener 后无法正常工作

标签 android android-animation

我正在为我的四个 TextView 使用向上/向下滑动动画。以下是我的 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/shadow"
    android:orientation="vertical"
    android:weightSum="2.0" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0" >

        <TextView
            android:id="@+id/textViewblue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_marginLeft="25dp"
            android:background="@drawable/blue"
            android:gravity="center"
            android:text="blue" />

        <TextView
            android:id="@+id/textVieworange"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:layout_marginRight="25dp"
            android:background="@drawable/orange"
            android:gravity="center"
            android:text="orange" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0">

        <TextView
            android:id="@+id/textViewpink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="25dp"
            android:background="@drawable/pink"
            android:gravity="center"
            android:text="pink" />

        <TextView
            android:id="@+id/textViewgreen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerInParent="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="25dp"
            android:background="@drawable/green"
            android:gravity="center"
            android:text="green" />
    </RelativeLayout>
</LinearLayout>

下面是我的类(class)

    public class HomeScreenActivity extends Activity implements OnClickListener {

    TextView textViewblue, textVieworange, textViewpink,
            textViewgreen;
    Animation mAnimation_up, mAnimation_down;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        mAnimation_up = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 1.6f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f);
        mAnimation_up.setDuration(1000);
        mAnimation_up.setRepeatCount(-1);
        mAnimation_up.setRepeatMode(Animation.REVERSE);
        mAnimation_up.setInterpolator(new LinearInterpolator());

        mAnimation_down = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0f,
                TranslateAnimation.RELATIVE_TO_SELF, 1.6f);
        mAnimation_down.setDuration(1000);
        mAnimation_down.setRepeatCount(-1);
        mAnimation_down.setRepeatMode(Animation.REVERSE);
        mAnimation_down.setInterpolator(new LinearInterpolator());
       initialize();
    }

    private void initialize() {

        textViewblue = (TextView) findViewById(R.id.textViewblue);
        textVieworange = (TextView) findViewById(R.id.textVieworange);
        textViewpink = (TextView) findViewById(R.id.textViewpink);
        textViewgreen = (TextView) findViewById(R.id.textViewgreen);

        textViewblue .setOnClickListener(this);
        textVieworange .setOnClickListener(this);
        textViewpink .setOnClickListener(this);
        textViewgreen .setOnClickListener(this);

        textViewblue .setAnimation(mAnimation_down);
        textViewpink .setAnimation(mAnimation_down);
        textVieworange .setAnimation(mAnimation_up);
        textViewgreen .setAnimation(mAnimation_up);
    }



    @Override
    public void onClick(View v) {
        Intent intentmenu;
        switch (v.getId()) {
        case R.id.textViewblue:
            Toast.makeText(this, "blue", Toast.LENGTH_LONG).show();
            break;
        case R.id.textVieworange:
             Toast.makeText(this, "orange", Toast.LENGTH_LONG).show();
            break;
        case R.id.textViewpink:
            Toast.makeText(this, "pink", Toast.LENGTH_LONG).show();
            break;
        case R.id.textViewgreen:
            Toast.makeText(this, "reeng", Toast.LENGTH_LONG).show();
            break;
        }
    }

}

问题是,当任何 TextViewslide for int position 意味着当动画开始时,onClickListener 仅在其 XML 位置而不是在其更新位置工作。 请给我建议解决方案,我如何检测更新的位置以检测 TextView 上的 onClickListener。

谢谢。

最佳答案

也许做起来有点复杂。当你使用 View animation system , 行为在预期之内。虽然 View 被翻译到其他地方,但它们只是被 在那里。从系统的角度来看,它们实际上始终位于您在 xml 文件中定义的原始位置。如果你想在新的地方点击事件,你应该使用新的property animation system .

关于android - 向上/向下滑动动画 Textview onClickListener 后无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25697264/

相关文章:

java - 从位图 rgb_565 转换为 Mat

android - 适用于 Android 的通用补间引擎

Android:正确处理蓝牙接收的文件

java - Android - 从服务器下载文件被终止

android - 查看翻转(日历翻转)3d 动画 android

android - 使用 ObjectAnimator 为 weightSum 属性设置动画

Android 从下到上淡入/淡出动画

android - 动画切换按钮

java - 如何从 Android 中的 JSON 解析中深入提取对象/数组

android - 检查 sdk 版本,如果