Friday, May 16, 2014

Get touch pressure

MotionEvent provide getPressure() and getPressure(int pointerIndex) to get the touch pressure.

Example:

MyView.java
package com.example.androiddrawinview;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class MyView extends View {
 
 private ArrayList<xy> drawList;
 private boolean touching = false;
 
 Paint paintTouchPointer_ONE;
 Paint paintTouchPointer;

 public MyView(Context context) {
  super(context);
  init();
 }

 public MyView(Context context, AttributeSet attrs) {
  super(context, attrs);
  init();
 }

 public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init();
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  setMeasuredDimension(
   MeasureSpec.getSize(widthMeasureSpec),
   MeasureSpec.getSize(heightMeasureSpec));
 }
 
 private void init(){
  paintTouchPointer = new Paint();
  paintTouchPointer.setColor(Color.RED);
  paintTouchPointer.setStyle(Paint.Style.FILL);
  
  paintTouchPointer_ONE = new Paint();
  paintTouchPointer_ONE.setColor(Color.BLUE);
  paintTouchPointer_ONE.setStyle(Paint.Style.STROKE);
 }

 @Override
 protected void onDraw(Canvas canvas) {
  canvas.drawColor(Color.GRAY);
  if(touching){
   for(xy pt : drawList){
    
    float PRESSURE_ONE_RADIUS = 50;
    float radius = pt.getPressure() * PRESSURE_ONE_RADIUS;
    
    canvas.drawCircle(
      pt.getX(), 
      pt.getY(), 
      radius, 
      paintTouchPointer);
    
    canvas.drawCircle(
      pt.getX(), 
      pt.getY(), 
      PRESSURE_ONE_RADIUS, 
      paintTouchPointer_ONE);
   }
  }
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  
  int action = event.getAction() & MotionEvent.ACTION_MASK;
  switch(action){
  case MotionEvent.ACTION_MOVE:
  case MotionEvent.ACTION_DOWN:
   
   ArrayList<xy> touchList = new ArrayList<xy>();
   int pointerCount = event.getPointerCount();
   
   for(int i=0; i < pointerCount; i++){
    touchList.add(
     new xy(
      event.getX(i), 
      event.getY(i),
      event.getPressure(i)));
   }
   drawList = touchList;
   touching = true;
   break;
  default:
   touching = false;
  }
  
  invalidate();
  return true;
 }
 
 class xy{
  float x;
  float y;
  float pressure;
  
  public xy(float x, float y, float pressure){
   this.x = x;
   this.y = y;
   this.pressure = pressure;
  }
  
  public float getX(){
   return x;
  }
  
  public float getY(){
   return y;
  }
  
  public float getPressure(){
   return pressure;
  }
 }
 
}

/res/layout/fragment_main.xml, to include MyView in layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.androiddrawinview.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <com.example.androiddrawinview.MyView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:layout_margin="10dp" />
    </LinearLayout>

</LinearLayout>

MainActivity.java, actually it is the default generated by Eclipse.
package com.example.androiddrawinview;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  if (savedInstanceState == null) {
   getSupportFragmentManager().beginTransaction()
     .add(R.id.container, new PlaceholderFragment()).commit();
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {

  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 /**
  * A placeholder fragment containing a simple view.
  */
 public static class PlaceholderFragment extends Fragment {

  public PlaceholderFragment() {
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View rootView = inflater.inflate(R.layout.fragment_main, container,
     false);
   return rootView;
  }
 }

}

/res/layout/activity_main.xml, auto generated by Eclipse.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.androiddrawinview.MainActivity"
    tools:ignore="MergeRootFrame" />

download filesDownload the files.

No comments: