2

例如,必须绘制图像布局。

我把一个膨胀ImageView成一个布局。当我在屏幕上滑动时,我需要将每个圆圈的颜色更改为绿色。我该怎么做。我尝试使用手势检测器,但我无法得到我需要的东西。

截屏

这是我的代码..我通过膨胀图像视图来创建这个视图..

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(1024);
        setContentView(R.layout.test);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        height = displaymetrics.heightPixels;
        width = displaymetrics.widthPixels;
        count = width/30;
        totalCircles = count * (height/30);
        horizontalSpace = width%30;
        verticalSpace = height%30;
        mRelativeLayout = (RelativeLayout)findViewById(R.id.fl);
        mRelativeLayout.setPadding(horizontalSpace/2, verticalSpace/2, horizontalSpace/2, verticalSpace/2);
        for(int i=1;i<totalCircles+1;i++){
             myButton = new ImageView(this);
             myButton.setId(i);
             myButton.setImageResource(R.drawable.circle_grey);
             LayoutParams lp = new LayoutParams(0,0);
             if(i%count != 1){
                 lp.addRule(RelativeLayout.RIGHT_OF, imgViews.get(i-2).getId()); 
                 if(imView != null){
                     lp.addRule(RelativeLayout.BELOW, imView.getId()); 
                 }
                 if(i%count == 0){
                     imView = myButton;
                 }
             }else{
                 if(imView != null){
                     lp.addRule(RelativeLayout.BELOW, imView.getId()); 
                 }
             }

             mRelativeLayout.addView(myButton,lp);
             myButton.getLayoutParams().width = 30;
             myButton.getLayoutParams().height = 30;
             imgViews.add(myButton);
             myButton.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                     if(event.getAction() == MotionEvent.ACTION_DOWN){
                         System.out.println("Inside touch listener");
                            ImageView imV = (ImageView)v;
                            imV.setImageResource(R.drawable.circle_green);
                     }

                    return false;
                }
            });

        }
    }
4

3 回答 3

2

Jack K Fouani 的回答部分正确。而不是imagView.onTouch你需要使用gridview.onTouch. 请检查这个样本。

gv.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float currentXPosition = event.getX();
            float currentYPosition = event.getY();
            int position = gv.pointToPosition((int)currentXPosition, (int) currentYPosition);
            //using the position obtained you can change the imageview color.           
            }

            return true;
        }
    });
于 2013-11-06T07:26:27.620 回答
0

而不是膨胀一个 ImageView。您应该填充一个 GridView,其中每个 GridCell 都包含一个圆形图像。现在设置这些 Gridcell 的 OnTouchEvent 以更改 GridCell 中的 ImageView。

于 2013-11-04T12:47:48.303 回答
0

我的建议是GrideView与适配器一起使用,在适配器内部position,每个imagView位置都允许您检查图像是否被触摸,具体取决于您可以将ImageView颜色更改为白色或绿色

于 2013-11-04T13:10:19.010 回答