Umair Kapoor
- Forum posts: 2
Jul 5, 2016, 10:36:42 PM via Website
Jul 5, 2016 10:36:42 PM via Website
Hello I need to count pixels that are covered by each fingers in multi-touch.When fingers down one by one it should plus pixels in count and finger up it should subtract pixels of that finger.I have did this but it is not getting exact counts i don't know what's wrong with it.please help me i am sucked at it.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Image img = new Image(getApplicationContext());
setContentView(R.layout.activity_main);
final ImageView MyimageView = (ImageView)findViewById(R.id.imageView);
final TextView LeftPixelView = (TextView)findViewById(R.id.LeftPixelView);
final TextView RightPixelView = (TextView)findViewById(R.id.RightPixelView);
final TextView PressureView = (TextView)findViewById(R.id.PressureView);
final TextView OrientationView = (TextView)findViewById(R.id.OrientationView);
final Display display = getWindowManager().getDefaultDisplay();
final int ScreenWidth = display.getWidth();
if (MyimageView != null) {
MyimageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// get pointer index from the event object
// int pointerIndex = event.getActionIndex();
mScaleDetector.onTouchEvent(event);
// get pointer ID
// int pointerId = event.getPointerId(pointerIndex);
// get masked (not specific to a pointer) action
int maskedAction = event.getActionMasked();
switch (maskedAction) {
case MotionEvent.ACTION_DOWN: {
final float x = event.getX();
final float y = event.getY();
mLastTouchX = x;
mLastTouchY = y;
///Toast toast = Toast.makeText(getApplicationContext(), "Action down", Toast.LENGTH_SHORT);
//toast.show();
int pointerIndex = event.getActionIndex();
mActivePointerId = event.getPointerId(0);
TouchMajor = event.getTouchMajor(pointerIndex);
TouchMinor = event.getTouchMinor(pointerIndex);
Pressure = event.getPressure(pointerIndex);
PressureView.setText("Pressure :"+Float.toString(Pressure));
//Orientaation =Double.toString( event.getOrientation(mActivePointerId));
//left Side of scren
if(event.getX(mActivePointerId) < ScreenWidth/2)
{
LeftPixChange = (TouchMajor + TouchMinor) / 2;
LeftPixSize = LeftPixChange;
LeftPixelView.setText(Float.toString(LeftPixSize));
OrientationView.setText("Left Side Clicked");
}
else if(event.getX(mActivePointerId) > ScreenWidth/2)
{
RightPixChange = (TouchMajor + TouchMinor) / 2;
RightPixSize = RightPixChange;
RightPixelView.setText(Float.toString(RightPixSize));
OrientationView.setText("Right Side Clicked");
}
break;
}
case MotionEvent.ACTION_POINTER_DOWN: {
final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = event.getX(newPointerIndex);
mLastTouchY = event.getY(newPointerIndex);
mActivePointerId = event.getPointerId(newPointerIndex);
}
TouchMajor = event.getTouchMajor(pointerIndex);
TouchMinor = event.getTouchMinor(pointerIndex);
Pressure =Pressure + event.getPressure(pointerIndex);
PressureView.setText("Cummulative pressure :"+Float.toString(Pressure));
//Orientaation = Orientaation+","+ event.getOrientation(mActivePointerId);
// OrientationView.setText(Orientaation);
//we take pointerindex here because pointerId remains same but index will be increased
if(event.getX(pointerIndex) < ScreenWidth/2)
{
LeftPixChange = (TouchMajor + TouchMinor) / 2;
LeftPixSize =LeftPixSize + LeftPixChange;
LeftPixelView.setText(Float.toString(LeftPixSize));
OrientationView.setText("Multi Left side clicked");
IsLeftSideActive = true;
}
else if(event.getX(pointerIndex) > ScreenWidth/2)
{
RightPixChange = (TouchMajor + TouchMinor) / 2;
RightPixSize =RightPixSize + RightPixChange;
RightPixelView.setText(Float.toString(RightPixSize));
OrientationView.setText("Multi Right side clicked");
IsRightSideActive = true;
}
else
{
OrientationView.setText("Both Sidse Clicked simultaneously");
}
break;
}
case MotionEvent.ACTION_MOVE: { // a pointer was moved
// TODO use data
final int pointerIndex = event.findPointerIndex(mActivePointerId);
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);
if (!mScaleDetector.isInProgress()) {
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
MyimageView.invalidate();
}
mLastTouchX = x;
mLastTouchY = y;
break;
}
case MotionEvent.ACTION_UP:{
mActivePointerId = INVALID_POINTER_ID;
//Toast toast = Toast.makeText(getContext(), "Action UP", Toast.LENGTH_SHORT);
//toast.show();
RightPixelView.setText(Float.toString(0.0f));
LeftPixelView.setText(Float.toString(0.0f));
PressureView.setText("Pressure : 0.0");
OrientationView.setText("");
break;
}
case MotionEvent.ACTION_POINTER_UP:{
final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.getPointerId(pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = event.getX(newPointerIndex);
mLastTouchY = event.getY(newPointerIndex);
mActivePointerId = event.getPointerId(newPointerIndex);
}
if(event.getX(pointerIndex) > ScreenWidth/2)
{
RightPixSize = RightPixSize - RightPixChange;
RightPixelView.setText(Double.toString(RightPixSize));
}
else if(event.getX(pointerIndex) < ScreenWidth/2){
LeftPixSize = LeftPixSize - LeftPixChange;
LeftPixelView.setText(Double.toString(LeftPixSize));
IsRightSideActive = false;
}
Pressure = Pressure - event.getPressure(event.getActionIndex());
PressureView.setText("Pressure :"+ Double.toString(Pressure));
break;
}
case MotionEvent.ACTION_CANCEL: {
// TODO use data
mActivePointerId = INVALID_POINTER_ID;
MyimageView.invalidate();
break;
}
}
MyimageView.invalidate();
return true;
}});
}`
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.