更改手指在手指上移动时的颜色在Gridlayout中?

时间:2019-02-17 14:46:31

标签: android

我需要在“手指”移动中更改网格布局中按钮的颜色。在触摸按钮上更改颜色有效,但在手指上,移动无效。谁能帮我吗?

这是我的活动

public class Layoutactivity extends AppCompatActivity {

    LinearLayout ln1;
    android.widget.GridLayout myGridLayout;
    demoview[] myViews;
    demoview tView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layoutactivity);

        myGridLayout = (android.widget.GridLayout) findViewById(R.id.mygrid);
        int numOfCol = myGridLayout.getColumnCount();
        int numOfRow = myGridLayout.getRowCount();
        myViews = new demoview[numOfCol*numOfRow];


        for(int yPos=0; yPos<numOfRow; yPos++){
            for(int xPos=0; xPos<numOfCol; xPos++){
                tView = new demoview(this, xPos, yPos,yPos);
                tView.setId(yPos);
                // tView.setOnToggledListener(this);
                myViews[yPos*numOfCol + xPos] = tView;
                //   myGridLayout.addView(tView);
                myGridLayout.addView(tView,yPos);
            }
        }
        myGridLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {


                final float X =  event.getRawX();
                final float Y =  event.getRawY();
                Log.d("Rowcordinate",""+X+" @#%$ " + Y);
                return true;
            }
        });


        myGridLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener(){

                    @Override
                    public void onGlobalLayout() {

                        final int MARGIN = 5;

                        int pWidth = myGridLayout.getWidth();
                        int pHeight = myGridLayout.getHeight();
                        int numOfCol = myGridLayout.getColumnCount();
                        int numOfRow = myGridLayout.getRowCount();
                        int w = pWidth/numOfCol;
                        int h = pHeight/numOfRow;

                        for(int yPos=0; yPos<numOfRow; yPos++){
                            for(int xPos=0; xPos<numOfCol; xPos++){
                                android.widget.GridLayout.LayoutParams params =
                                        (GridLayout.LayoutParams)myViews[yPos*numOfCol + xPos].getLayoutParams();
                                params.width = w - 2*MARGIN;
                                params.height = h - 2*MARGIN;
                                params.setMargins(MARGIN, MARGIN, MARGIN, MARGIN);
                                myViews[yPos*numOfCol + xPos].setLayoutParams(params);
                            }
                        }

                    }});
    }
    public class MyGridLayout extends GridLayout {
        public MyGridLayout(Context context) {
            super(context);
        }
        @Override
        public boolean dispatchTouchEvent(MotionEvent ev) {
            for (int i = 0; i < getChildCount(); i++) {
                View child = getChildAt(i);

                Log.d("childid",""+getChildCount());
                try {
                    child.dispatchTouchEvent(ev);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return true;
        }
    }

///////////////////////////////////////////////// ////////////////////

这是我的自定义视图代码:

public class demoview extends View {

    int idX = 0; //default
    int idY = 0;
    //default
    int iview = 0;

    public demoview(Context context, int x, int y,int vid) {
        super(context);
        idX = x;
        idY = y;
        iview=vid;

       // init();
    }

    public demoview(Context context) {
        super(context);
       // init();
    }

    public demoview(Context context, AttributeSet attrs) {
        super(context, attrs);
        //init();
    }

    public demoview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
      //  init();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
                MeasureSpec.getSize(heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.RED);
    }

    public int getIdX(){
        return idX;
    }

    @Override
    public float getX() {
        return super.getX();
    }

    @Override
    public float getY() {
        return super.getY();
    }

    public int getIview() {
        return iview;
    }

    public int getIdY(){
        return idY;
    }
}

1 个答案:

答案 0 :(得分:0)

您可以覆盖OnTouchEvent,然后检查所有子对象的坐标

 @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE: {

                for (int i = 0; i < gridLayout.getChildCount(); i++) {
                    if (gridLayout.getChildAt(i) instanceof Button) {
                        Rect rect = new Rect();
                        gridLayout.getChildAt(i).getGlobalVisibleRect(rect);
                        if (rect.contains((int) event.getX(), (int) event.getY())) {
                            Button btn = (Button) gridLayout.getChildAt(i);
                            btn.setBackgroundColor(Color.RED);
                        }
                    }
                }

                break;
            }

        }

        return super.onTouchEvent(event);
    }