繁体   English   中英

创建按钮,添加事件,并添加一些动画

[英]create button, add event, and adding some animation

我正在尝试创建类似于下图的内容:

图片

当我单击按钮1时:

  • 正方形2显示字体“ h”和蓝色背景,
  • 正方形3显示字体“ e”和蓝色背景,
  • 方形4显示字体“ l”和蓝色背景,
  • 正方形5显示字体“ l”和蓝色背景,
  • 方形6显示字体“ o”和蓝色背景,

字体和背景依次出现在第2、3、4、5、6号正方形中。

当我单击正方形2时,则:

  • 正方形3显示字体“ E”和红色背景,
  • 方形4显示字体“ L”和红色背景,
  • 正方形5显示字体“ L”和红色背景,
  • 方形6显示字体“ O”和红色背景,

字体和背景以3、4、5、6号正方形顺序显示。

当我单击另一个正方形时发生了同样的事情。例如,当我单击正方形3时,则:

  • 正方形2显示字体“ H”和红色背景,
  • 方形4显示字体“ l”和蓝色背景,
  • 正方形5显示字体“ l”和蓝色背景,
  • 方形6显示字体“ o”和蓝色背景,

字体和背景以2、4、5、6号正方形顺序显示。

有没有一种方法可以在不使用opengl的Android应用程序中做到这一点?

编辑:

按顺序,我的意思是按顺序排列,每平方增加一些延迟(例如1秒)

字体“ h,e,l,l,o”和“ H,E,L,L,O”存储在数组中。

正如我在评论中所说,您可以通过在所需时间发布适当的Runnable对象的Handler来实现。

Handler handler = new Handler(); // the handler
String[] text = { "h", "e", "l", "l", "o" }; // the text
TextView[] views = new TextView[5]; // the views 2, 3, 4, 5, 6

当我单击按钮1时:

您可以使用的代码:

button1.setOnClickListener(new OnClickListener() {

        int counter = 0; // needed so we know where we are in the arrays

        @Override
        public void onClick(View v) {
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // when this Runnable is done we set the background
                    // color, the text, update the counter(to update the
                    // next TextView) and repost the same variable
                    views[counter].setBackgroundColor(Color.BLUE);
                    views[counter].setText(text[counter]);
                    views[counter].setTextColor(Color.WHITE);
                    counter++;
                    if (counter < views.length) {
                        handler.postDelayed(this, 1000); // keep it in the
                                                            // arrays bounds
                    } else {
                        handler.removeCallbacks(this); // we finished
                                                        // iterating over
                                                        // the arrays so
                                                        // reset stuff and
                                                        // remove the
                                                        // Runnable
                        counter = 0;
                    }
                }

            }, 1000); // 1 second delay!
        }
    });

当我单击正方形2时,则:

这基本上与上述相同,您只需要确保不更新触发更改的视图即可:

// set as the tag for the 2,3,4,5,6 views their position in the array
for (int i = 0; i < views.length; i++) {
    views[i].setTag(i);
    views[i].setOnClickListener(mListener);
//...

您可以使用该标记和计数器来仅更新正确的视图:

OnClickListener mListener = new OnClickListener() {

        int counter = 0;// needed so we know where we are in the arrays

        @Override
        public void onClick(View v) {
            final int whichOne = (Integer) v.getTag(); // gett he views
                                                        // position in the
                                                        // arrays
            // check if we clicked 2
            if (counter == whichOne) {
                counter++;
            }
            handler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    // on every iteration check to see if we aren't trying
                    // to update the view that was actually clicked
                    // if it's the view that was clicked then increment the
                    // counter
                    if (counter == whichOne) {
                        counter++;
                    }
                    views[counter].setBackgroundColor(Color.RED);
                    views[counter].setText(text[counter]);
                    views[counter].setTextColor(Color.WHITE);
                    counter++;
                    if (counter < views.length) {
                        handler.postDelayed(this, 1000);
                    } else {
                        handler.removeCallbacks(this);
                        counter = 0;
                    }
                }

            }, 1000);
        }
    };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM