用手指移动视图Acivate [英] Acivate view with moving finger

查看:240
本文介绍了用手指移动视图Acivate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造充满矩形的字段,如果你移动你的手指在矩形下它会改变颜色。我已经尝试过很多方法,但我没有成功,我没有任何其他的想法。现在我的code看起来像这样,有了它的问题是,着色(在ontouch)只激活时,我的手指在屏幕的左上角,它的颜色所有的矩形。

I'm trying to create a field filled with rectangles and if you move your finger the rectangles under it changes color. I've tried it many ways but i did not succeed and i don't have any other ideas. Now my code looks like this, and the problem with it is that the coloring (in the ontouch) only activates when my finger is in the top left corner of the screen and it colors all the rectangles.

public class MainActivity extends Activity implements OnTouchListener {
ImageView[][] buttons;
Button b;
TableLayout ButtonContainer; 
RelativeLayout linar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);    
    buttons=new ImageView[5][5];
    linar=new RelativeLayout(this);
    linar.setOnTouchListener(this);

    setPos(0,0,100);
    setContentView(linar);
}

void setPos(float startX, float startY, int size)
{
    float actposX,actposY;
    actposX=startX;
    actposY=startY;
    for (int i=0;i<5;i++){

        for (int j=0;j<5;j++){
            buttons[i][j]=new ImageView(this);
            buttons[i][j].setLayoutParams(new RelativeLayout.LayoutParams(size, size));
            buttons[i][j].setX(actposX);
            buttons[i][j].setY(actposY);
            if (j % 2 ==0){
                buttons[i][j].setBackgroundColor(Color.YELLOW);
            }
            else
            {   buttons[i][j].setBackgroundColor(Color.BLUE);   }
            actposX+=size;
            linar.addView(buttons[i][j]);
        }
        actposY+=size;
        actposX=startX;
    }

}

@Override
public boolean onTouch(View v, MotionEvent event) {

    for(int i =0; i< 5; i++)
        {
           for (int j=0; j< 5; j++)
           {
               Rect outRect = new Rect(buttons[i][j].getLeft(), buttons[i][j].getTop(), buttons[i][j].getRight(), buttons[i][j].getBottom());
               if(outRect.contains((int)event.getX(), (int)event.getY()))
                {
                    buttons[i][j].setBackgroundColor(Color.RED);
                }
           }
        }

    return true;
}

}

推荐答案

我想通了......在getLeft(),共达()..还给0值,所以我这个改变了它:

I figured it out... the getLeft(), getTop().. gives back 0 values so i changed it with this:

                   outRect.top=(int) (buttons[i][j].getY());
               outRect.bottom=(int) (buttons[i][j].getY()+buttons[i][j].getLayoutParams().height);
               outRect.left=(int) (buttons[i][j].getX());
               outRect.right=(int) (buttons[i][j].getX()+buttons[i][j].getLayoutParams().height);

这篇关于用手指移动视图Acivate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆