android - 动画添加到 Nexus4 与其他设备上的 Activity 的新 TextView 的问题

标签 android android-layout textview android-animation

我是 java 和 android 编程的新手。

我正在尝试创建一个按钮,当按下该按钮时,文本将出现在点击位置并立即淡出。 目前,问题是单击时 - 文本确实出现,但动画不播放,文本在几秒钟后消失(就像我想要的那样)。

我已经在下面编辑了这个问题几次,在回复的人的大力帮助下解决这个问题取得了一些进展。谢谢!

我试过的代码是这样的:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mClickMe = (Button) findViewById(R.id.button1);
    mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);

    mClickMe.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                int xClickPos = (int) event.getRawX();
                int yClickPos = (int) event.getRawY();

                final TextView scorePop = new TextView(getApplicationContext());                    

                scorePop.setText("+10");
                scorePop.setX(xClickPos);
                scorePop.setY(yClickPos);
                scorePop.setVisibility(TextView.INVISIBLE);

                // Create the score pop animation                   
                Animation scoreAnimation = new AlphaAnimation(1, 0);
                scoreAnimation.setDuration(1000);

                scoreAnimation.setAnimationListener (new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        scorePop.setVisibility(TextView.VISIBLE);

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        mRelativeLayout.removeView(scorePop);

                    }
                });                 
                mRelativeLayout.addView(scorePop);
                scorePop.setAnimation(scoreAnimation);

                scoreAnimation.start();


            }
            return false;
        }
    });



}

编辑:我还注意到,如果在不同的位置快速单击按钮,文本会出现在这些位置并随着“断断续续”的淡入淡出而消失。然而,如果单击一次 - 文本出现和消失但没有动画。 感谢您的帮助!

编辑 2:在我到达此处的帮助下,问题似乎与设备有关。我正在使用 Nexus 4,并运行一个模拟器来测试我的应用程序作为 Nexus 4。奇怪的是,为另一台设备(例如 Galaxy Nexus)运行模拟器似乎可以解决问题。

最佳答案

编辑: 我能够重新创建您的原始错误,它与 setX(),setY() 有关。在执行这些方法中的任何一个后,TextView 变得“ NumPy ”并且不会动画化。我认为的解决方法是设置布局的 边距 而不是 TextView 的坐标:

        // Set the margins instead of the actual position
        ViewGroup.MarginLayoutParams params=(ViewGroup.MarginLayoutParams)scorePop.getLayoutParams();
        params.leftMargin=xClickPos;
        params.topMargin=yClickPos;

关于android - 动画添加到 Nexus4 与其他设备上的 Activity 的新 TextView 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24549035/

相关文章:

android - 像谷歌一样跟踪用户位置

安卓滑动面板

android - 如何避免列表之间出现双边框?

android - 使用自动调整大小时无法以编程方式更改文本大小

android - 带有 LinkMovementMethod 的 justificationMode 使文本被截断

android - 如何以编程方式在android中的xml文件中引用<string>

java - 通过本地网络传输实时数据

用于大型可定制工具应用程序的 Android 架构

android - 更新后 Firebase 重复类错误

java - 无法将 TextView 中的名称从预定义数组添加到 AutoCompleteTextView