2012-05-18

A Smater Way to Convert MotionEvent#getRawY() into Relative

I faced a kind of problem. The problem is that MotionEvent#getY() doesn't returns expected values, while MotionEvent#getRawY() keeps returning expected, sequential positions. So I tried to use MotionEvent#getRawY() instead of MotionEvent#getY(). However, values getRawY() sent are based on the absolute position of displays. Some sites show hacks to exclude unnecessary height of, such as the status bar. But the heights of the bars are not same on all platforms and sometimes the bars are even got hidden.

From Android API Level 1(This is not my mistake, indeed from level ONE!), the View class provide getLocationOnScreen(int[]) method. Using this method, you can convert the result of MotionEvent#getRawY() into relative to your views.

class MyView implements OnTouchListener
{
 @Override
 public boolean onTouch(View pmView, MotionEvent pmEvt)
 {
  int[] cViewPosXY = new int[2];
  getLocationOnScreen(cViewPosXY);
  int cTouchX = cEvt.getRawX() - cViewPosXY[0];
  int cTouchY = cEvt.getRawY() - cViewPosXY[1];
 }
}

2012-05-06

BitmapFactory.decodeStream(InputStream)

I desired to generate a new bitmap image with a subclass of java.nio.Buffer for some reason.
I found a simple way to convert Buffer into InputStrem in a post on StackOverFlow titled "java - Wrapping a ByteBuffer with an InputStream"(http://stackoverflow.com/questions/4332264/wrapping-a-bytebuffer-with-an-inputstream). However, I encountered a NPE with the below message on my logcat after integrating my own wrapping ByteBuffer class.
SkImageDecoder::Factory returned null

I investigated this problem and found an essential reason of the message.