如果用户在不同的ImageView上滑动手指,如何检测ImageView是否被触摸。

3
这背后的基本想法是,在用户滑动手指经过一定数量的图像视图后,如果它们的颜色相似,则检测不同视图的触摸事件将被销毁。
1个回答

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;
}
}

这个想法是基于宝石迷阵游戏的,用户触摸不同颜色的方块,如果它们的颜色匹配,就会被销毁。如何应用这个想法? - akash yadav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接