Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 22nd, 2012  |  syntax: None  |  size: 2.05 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package de.scrolltest;
  2.  
  3. import android.app.Activity;
  4.  
  5. import android.os.Bundle;
  6.  
  7. import android.widget.LinearLayout;
  8.  
  9. import android.view.MotionEvent;
  10.  
  11. import android.view.GestureDetector.OnGestureListener;
  12.  
  13. import android.view.GestureDetector;
  14.  
  15. import android.widget.TextView;
  16.  
  17. import android.graphics.Color;
  18.  
  19. public class ScrollTest extends Activity implements OnGestureListener
  20.  
  21. {
  22.  
  23.         private LinearLayout main;
  24.  
  25.         private TextView viewA;
  26.  
  27.         private GestureDetector gestureScanner;
  28.  
  29.         @Override
  30.         public void onCreate(Bundle savedInstanceState)
  31.  
  32.         {
  33.  
  34.                 super.onCreate(savedInstanceState);
  35.  
  36.                 gestureScanner = new GestureDetector(this);
  37.  
  38.                 main = new LinearLayout(this);
  39.  
  40.                 main.setBackgroundColor(Color.GRAY);
  41.  
  42.                 main.setLayoutParams(new LinearLayout.LayoutParams(320, 480));
  43.  
  44.                 viewA = new TextView(this);
  45.  
  46.                 viewA.setBackgroundColor(Color.YELLOW);
  47.  
  48.                 viewA.setTextColor(Color.BLACK);
  49.  
  50.                 viewA.setTextSize(16);
  51.  
  52.                 viewA.setLayoutParams(new LinearLayout.LayoutParams(320, 80));
  53.  
  54.                 main.addView(viewA);
  55.  
  56.                 setContentView(main);
  57.  
  58.         }
  59.  
  60.         @Override
  61.         public boolean onTouchEvent(MotionEvent me)
  62.  
  63.         {
  64.  
  65.                 return gestureScanner.onTouchEvent(me);
  66.  
  67.         }
  68.  
  69.         @Override
  70.         public boolean onDown(MotionEvent e)
  71.  
  72.         {
  73.  
  74.                 viewA.setText("-" + "DOWN" + "-");
  75.  
  76.                 return true;
  77.  
  78.         }
  79.  
  80.         @Override
  81.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  82.                         float velocityY)
  83.  
  84.         {
  85.  
  86.                 viewA.setText("-" + "FLING" + "-");
  87.  
  88.                 return true;
  89.  
  90.         }
  91.  
  92.         @Override
  93.         public void onLongPress(MotionEvent e)
  94.  
  95.         {
  96.  
  97.                 viewA.setText("-" + "LONG PRESS" + "-");
  98.  
  99.         }
  100.  
  101.         @Override
  102.         public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
  103.                         float distanceY)
  104.  
  105.         {
  106.                 String direction = "RIGHT";
  107.                 if(distanceX > 0){
  108.                         direction = "LEFT";
  109.                 }
  110.                 String upORdown = "up";
  111.                 if(distanceY >0){
  112.                         upORdown = "down";
  113.                 }
  114.                 viewA.setText("-" + "SCROLL" + "-" + direction+"  "+upORdown);
  115.  
  116.                 return true;
  117.  
  118.         }
  119.  
  120.         @Override
  121.         public void onShowPress(MotionEvent e)
  122.  
  123.         {
  124.  
  125.                 viewA.setText("-" + "SHOW PRESS" + "-");
  126.  
  127.         }
  128.  
  129.         @Override
  130.         public boolean onSingleTapUp(MotionEvent e)
  131.  
  132.         {
  133.  
  134.                 viewA.setText("-" + "SINGLE TAP UP" + "-");
  135.  
  136.                 return true;
  137.  
  138.         }
  139.  
  140. }