I want to apply certain transforms to a view and it's children during
pressed state, e.g. a color filter.
As far my current knowledge, I can't do this using StateListDrawable or XML
configuration.
The concrete situation: A GridView where the cells have a background
drawable, an ImageView, and text. When the user is pressing a cell, I want
to change the background, to apply a PorterDuff color filter to the image,
and change the color of the text. The images are downloaded from the web.
My first try was to generate bitmaps for both states (pressed and not
pressed) at runtime, inflating the layout twice and using
`getDrawingCache()`. Then create a StateListDrawable and set both states.
Wasn't a complete fail but I started getting some strange issues, and I
think this will also not allow we to recycle views (Edit: But I might
continue that path if the following approach is wrong).
So I tried a completly different thing - set touch listener on each cell,
which works like this:
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int)event.getX();
int y = (int)event.getY();
int space = 10;
switch(event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
moved = false;
pressedX = x;
pressedY = y;
//apply color filter
//do other changes
return true;
case MotionEvent.ACTION_UP:
if (!moved) {
//clear color filter
//undo other changes
return true;
}
case MotionEvent.ACTION_MOVE:
if (Math.abs(x - pressedX) > space || Math.abs(y -
pressedY) > space) {
moved = true;
//clear color filter
//undo other changes
return true;
}
}
return false;
}
});
But the ACTION_MOVE part here is basically a hack, and also doesn't work
well. First, it makes that the pressed state finishes while the user is
moving inside the cell. Not tragic, but doesn't have to be necessarily this
way. But it has the bug, that when the user moves out of the cell very
quickly - or starts at the edges and moves out - such that there's no
ACTION_UP in the cell and sometimes ACTION_MOVE is not even called, or the
value is less than space, the cell keeps pressed!
So, in order to solve that, I thought about adding an OnTouchListener to
the parent view of the grid / the activity, and check on ACTION_MOVE if
it's outside of the grid and then loop through the cells and reset pressed
state... but you see this is ugly and also bad performance.
And I don't know if I'm missing an easy solution and running unnecessarily
in complications.
Does anybody have an advice for this?
Thanks in advance.
--
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
---
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.