Android:在Adapter中的触摸事件上更新GridView中的图像

Android:在Adapter中的触摸事件上更新GridView中的图像,第1张

Android:在Adapter中的触摸事件上更新GridView中的图像

你之后

ImageView
开始处理的TouchEvent,它是唯一的视图允许这样做与触摸手势什么。您想
GridView
拦截TouchEvent,检查TouchEvent(
event.getX();event.getY()
)的X和Y坐标,并查看这些坐标是否在您的
ImageView
对象之一的范围内。如果是,请在
ImageView
或可以触发您的
setImageDrawable()
方法的标记上设置一个标志。我可以在一个项目中实现类似的效果,但是必须创建一个自定义GridView类(
publicclass yourGridView extends GridView
),然后重写以下内容:

@Override    public boolean onInterceptTouchEvent(MotionEvent event) {        int action = event.getAction();        switch(action) { case MotionEvent.ACTION_DOWN:     Log.i(AGL, "InterceptAction = DOWN");     break; case MotionEvent.ACTION_MOVE:     Log.i(AGL, "InterceptAction = MOVE");     break; case MotionEvent.ACTION_CANCEL:     Log.i(AGL, "InterceptAction = CANCEL");     return false;        }        return true;  //returning true tells your main Activity that you want the custom GridView to handle this TouchEvent; It will then send the TouchEvent to your GridView's onTouchEvent() method for handling.    }    @Override    public boolean onTouchEvent(MotionEvent event) {        int action = event.getAction();        switch(action) { case MotionEvent.ACTION_MOVE:     int xCoord = (int) event.getX();     int yCoord = (int) event.getY();     Log.i(AGL, "MOVE EVENT;" + "n" +  "Touch X = " + Integer.toString(xCoord) + "n" +     "Touch Y = " + Integer.toString(yCoord));     for(int i = 0; i < this.getChildCount(); i++) {         ImageView suspect = (ImageView) this.getChildAt(i);         if(suspect.getBounds().contains(xCoord, yCoord)) {  suspect.CHANGE_YOUR_IMAGE_HERE();  suspect.invalidate();         }     }     break;        }        return true;    }

请注意:getBounds()是我在自定义

ImageView
类中编写的一种方法。此方法返回一个Rect对象,该对象表示对象的边界框
ImageView
。您将必须使用自己的逻辑来获取您的的边界
ImageView

另请注意:当我在项目中运行此逻辑时,

ImageView
在触摸手势期间,我会快速更改图像。我还没有想出如何将其限制为每个MotionEvent.ACTION_MOVE一个图像更改。

我希望这有帮助。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5500431.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-13
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存