触摸如何检测ImageViews如果用户在不同的图像视图上滑动手指

时间:2012-12-06 07:34:14

标签: android

这背后的基本思想是在手指滑动到一定数量的图像视图后,如果它们具有相似的颜色会破坏,则在扫描用户的手指时检测不同视图的触摸事件

1 个答案:

答案 0 :(得分:0)

试试这个..

public class SimpleActivity extends Activity implements SimpleGestureListener{

    private SimpleGestureFilter detector;
    private ImageView imgview;  
    private Bitmap bm; 
    private String image_URL="http://4.bp.blogspot.com/-k8Zxa7NXx5c/UAdrZw92MpI/AAAAAAAAAWI/KZ8n8sziFb4/s1600/Adobe-PDF-Alternative.jpg";
    private File file;
    private File folder;
    private int no=1;
    private int totalfiles;
    private int count;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
             imgview=(ImageView)findViewById(R.id.imageView1);

             //Download from URL
             Downloadfromurl(image_URL);

            detector = new SimpleGestureFilter(this,this);
        }


     public boolean dispatchTouchEvent(MotionEvent me){ 
       this.detector.onTouchEvent(me);
      return super.dispatchTouchEvent(me); 
     }


     public void onSwipe(int direction) {
      String str = "";

      switch (direction) {

      case SimpleGestureFilter.SWIPE_RIGHT :
          {
              str = "Swipe Right";
              imgfrwd();
               break;
          }
      case SimpleGestureFilter.SWIPE_LEFT :
      {
          str = "Swipe Left";
          imgbkwd();
           break;
      }
      case SimpleGestureFilter.SWIPE_DOWN : 
          {
              str = "Swipe Down";
              imgbkwd();
               break;
          }
      case SimpleGestureFilter.SWIPE_UP : 
      {
          str = "Swipe Up";
          imgfrwd();
          break;
      }


      } 
       Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
     }


     public void onDoubleTap() {
        Toast.makeText(this, "Double Tap", Toast.LENGTH_SHORT).show(); 
     }


     private Bitmap LoadImage(String URL, BitmapFactory.Options options)
      {     
       Bitmap bitmap = null;
       InputStream in = null;     
          try {
              in = OpenHttpConnection(URL);
              bitmap = BitmapFactory.decodeStream(in, null, options);
              in.close();
          } catch (IOException e1) {
          }
          return bitmap;             
      }

    private InputStream OpenHttpConnection(String strURL) throws IOException{
     InputStream inputStream = null;
     URL url = new URL(strURL);
     URLConnection conn = url.openConnection();

     try{
      HttpURLConnection httpConn = (HttpURLConnection)conn;
      httpConn.setRequestMethod("GET");
      httpConn.connect();

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
       inputStream = httpConn.getInputStream();
      }
     }
     catch (Exception ex)
     {
     }
     return inputStream;
    }


    void dispimg()
    {
          Bitmap bmp = BitmapFactory.decodeFile(folder + "/page"+no+".PNG");
          imgview.setImageBitmap(bmp);

    }

    void loadfromurl()
    {
         BitmapFactory.Options bmOptions;
           bmOptions = new BitmapFactory.Options();
           bmOptions.inSampleSize = 1;
           bm = LoadImage(image_URL, bmOptions);
          imgview.setImageBitmap(bm);
    }


void imgfrwd()

{

no+=1;
    count=countfiles();
    if(count>=no)
        dispimg();
}
void imgbkwd()
{

    no-=1;
    if(no>=0)
    dispimg();
}

int countfiles()
{
    String[] fileNames = folder.list();
    totalfiles = 0;
    for (int i = 0; i< fileNames.length; i++)
    {
      if (fileNames[i].contains(".PNG"))

          totalfiles++;

      }

    return totalfiles;
}
}