0

I am developing an animation app like Mine Sweeper. Upon tapping on the buttons, the buttons will be pressed and the user may tap on the other button. On the third tap on the chosen button, there will be an incoming image (which uses AlphaAnimation/ScaleAnimation) from that button.

enter image description here

Buttons

private int[] right_lung = { R.id.lungs_106, R.id.lungs_113,
        R.id.lungs_114, R.id.lungs_115, R.id.lungs_116, R.id.lungs_121,
        R.id.lungs_122, R.id.lungs_123, R.id.lungs_124, R.id.lungs_125,
        R.id.lungs_129, R.id.lungs_130, R.id.lungs_131, R.id.lungs_132,
        R.id.lungs_133, R.id.lungs_134, R.id.lungs_137, R.id.lungs_138,
        R.id.lungs_139, R.id.lungs_140, R.id.lungs_141, R.id.lungs_142,
        R.id.lungs_145, R.id.lungs_146, R.id.lungs_147, R.id.lungs_148,
        R.id.lungs_149, R.id.lungs_150, R.id.lungs_151, R.id.lungs_152,
        R.id.lungs_153, R.id.lungs_154, R.id.lungs_155, R.id.lungs_156,
        R.id.lungs_157, R.id.lungs_158, R.id.lungs_159, R.id.lungs_160,
        R.id.lungs_161, R.id.lungs_162, R.id.lungs_163, R.id.lungs_164,
        R.id.lungs_165, R.id.lungs_166, R.id.lungs_167, R.id.lungs_168,
        R.id.lungs_169, R.id.lungs_170, R.id.lungs_171, R.id.lungs_172,
        R.id.lungs_173, R.id.lungs_174, R.id.lungs_175, R.id.lungs_176,
        R.id.lungs_177, R.id.lungs_178, R.id.lungs_179, R.id.lungs_180,
        R.id.lungs_181, R.id.lungs_182, R.id.lungs_183, R.id.lungs_184,
        R.id.lungs_185, R.id.lungs_186, R.id.lungs_187, R.id.lungs_188,
        R.id.lungs_189, R.id.lungs_190, R.id.lungs_191, R.id.lungs_192,
        R.id.lungs_194, R.id.lungs_195, R.id.lungs_196, R.id.lungs_197,
        R.id.lungs_198, R.id.lungs_199, R.id.lungs_200, R.id.lungs_205,
        R.id.lungs_206, R.id.lungs_207 };

private Button[] button_right_lung = new Button[right_lung.length];

MainActivity

for (int i = 0; i < button_right_lung.length; i++) {
    final int b = i;
    button_right_lung[i].setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) { 
                if (counter == 1) {
                    show_first_page();

                    int[] startPosition = new int[2];
                    button_right_lung[b].getLocationOnScreen(startPosition);

                    int[] endPosition = new int[2];
                    button_right_lung[b].getLocationOnScreen(endPosition);

                    Animation anim = new ScaleAnimation(startPosition[1], 2000, endPosition[1], 2000);
                    anim.setDuration(3000);
                    img_Message1.setVisibility(View.VISIBLE);
                    img_Message1.startAnimation(anim);
          }
});

}

Now, my question is, how can I get the coordinates of the selected button and will animate the image after the button is pressed?

androidBoomer
  • 3,357
  • 6
  • 32
  • 42
  • I hope you know how many buttons you need to place. If this is case then simply you can assign index number to it and then on click get that index number. – AndroidHacker Nov 27 '13 at 12:32
  • Yeah I know how many buttons I have. Just edited my code. – androidBoomer Nov 27 '13 at 13:25
  • See http://stackoverflow.com/questions/2224844/how-to-get-the-absolute-coordinates-of-a-view to get the coordinates of the button. – Qw4z1 Nov 28 '13 at 04:54

2 Answers2

0

Now you know how many butouns you need to put. You also have id for all of them. You have placed same in an array.

Now I do believe that you can find array length.

Just put all buttouns in loop.

for ex:

for(int i = 0; i < right_lung.length; i++){
button.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Button btn = (Button)v;
                                        int btn_value = btn.getid;
                                   // Some thing like that..
                }
            });

}
AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
-1

you can use OnTouchListener on your button

button.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

                       int X=(int) event.getX();
                       int Y=(int) event.getY();
                  }
      }
Amit
  • 31
  • 3