Andorid gesture exercise

Source: Internet
Author: User
Tags gety
Package com. llm; import util. countDownButton; import util. countDownButton. onCountDownListener; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. gestureDetector; import android. view. gestureDetector. onGestureListener; import android. view. motionEvent; import android. view. view; import android. view. view. onClickListener; import android. view. view. onTouchListener; imp Ort android. widget. button; import android. widget. linearLayout; import android. widget. toast; public class AddViewDemoActivity extends Activity implements OnClickListener, OnTouchListener, OnGestureListener {/** Called when the activity is first created. */private static final int FLING_MIN_DISTANCE = 100; private static final int FLING_MIN_VELOCITY = 200; private GestureDetector mGestureDetector; pr Ivate Toast toast; // One-dimensional array private String [] strs = new String [] {"Haha 1", "Sres2", "fdfd3", "dsdsd4", "fdfd5 ", "dsdsd6"}; // two-dimensional array private int [] [] numseven = new int [] [] {10, 20, 30}, {40, 50 }, {60 },{ 70, 80, 90 },{ 100 },{ 101,102,103,104,105,106 };; private int [] [] nums = new int [4] [5]; // 4 and 5 indicate that the length of the array is 4 to 0 ~ 3, 5 indicates 0 ~ 4 @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. main); mGestureDetector = new GestureDetector (this); toast = ToastUtils. getToastInstance (AddViewDemoActivity. this); LinearLayout layout = new LinearLayout (this); layout. setOrientation (LinearLayout. VERTICAL); Button button1 = new Button (this); Button button2 = new Button (this); CountDownButton button3 = new CountDownButton (this); button1.setText ("Button 1 "); button1.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub toast. setText (String. valueOf (numseven [5] [4]); toast. show () ;}}); button2.setText ("button 2"); button3.setCount (15); layout. addView (button1); layout. addView (button2); layout. addView (button3); button3.setOnCountDownListener (new OnCountDownListener () {@ Override public void onTick () {// TODO Auto-generated method stub Log. I ("lilongmin", "onTick") ;}@ Override public void onFinish () {// TODO Auto-generated method stub Log. I ("lilongmin", "onFinish"); AddViewDemoActivity. this. finish () ;}}); setContentView (layout); button2.setOnTouchListener (this); button2.setLongClickable (true) ;}@ Override public void onClick (View v) {// TODO Auto-generated method stub} @ Override public boolean onTouch (View v, MotionEvent event) {// TODO Auto-generated method stub // toast. setText ("onTouch"); // toast. show (); return mGestureDetector. onTouchEvent (event);} // you can touch the touch screen, triggered by one MotionEvent ACTION_DOWN @ Override public boolean onDown (MotionEvent e) {// TODO Auto-generated method stub toast. setText ("onDown"); toast. show (); return false;} // you can press the touch screen and move it quickly to release it. One MotionEvent ACTION_DOWN, multiple ACTION_MOVE, one ACTION_UP trigger @ Override public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {// parameter explanation: // e1: 1st ACTION_DOWN MotionEvent // e2: last ACTION_MOVE MotionEvent // velocityX: moving speed on the X axis, pixel/second // velocityY: moving speed on the Y axis, pixel/second // trigger condition: the coordinate displacement of the X axis is greater than FLING_MIN_DISTANCE, and the movement speed is greater than FLING_MIN_VELOCITY pixels/S. // if (e1.getX ()-e2.getX ()> FLING_MIN_DISTANCE & Math. abs (velocityX)> FLING_MIN_VELOCITY) {// Fling left // toast. setText ("Fling Left"); // toast. show (); //} // else if (e2.getX ()-e1.getX ()> FLING_MIN_DISTANCE & Math. abs (velocityX)> FLING_MIN_VELOCITY) {// Fling right // toast. setText ("Fling Right"); // toast. show (); //} if (e1.getY ()-e2.getY ()> 100) {toast. setText ("Move Up With gesture"); toast. show (); return true;} else if (e1.getY ()-e2.getY () <-100) {toast. setText ("gesture sliding down"); toast. show (); return true;} else if (e1.getX ()-e2.getX ()> 100) {toast. setText ("sliding left with a gesture"); toast. show (); return true;} else if (e1.getX ()-e2.getX () <-100) {toast. setText ("Move to the right"); toast. show (); return true;} return false;} // you can press the touch screen for a long time. Multiple MotionEvent ACTION_DOWN triggers @ Override public void onLongPress (MotionEvent e) {// TODO Auto-generated method stub toast. setText ("onLongPress"); toast. show () ;}// press the touch screen and drag it. One MotionEvent ACTION_DOWN and multiple ACTION_MOVE triggers @ Override public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {// TODO Auto-generated method stub toast. setText ("onScroll"); toast. show (); return false;} // touch the touch screen of the user, which has not been released or dragged yet, triggered by a MotionEvent ACTION_DOWN // note the difference between it and onDown, it is emphasized that the status is not released or dragged. @ Override public void onShowPress (MotionEvent e) {// TODO Auto-generated method stub toast. setText ("onShowPress"); toast. show ();} // the user (after touching the touch screen) is released, triggered by one MotionEvent ACTION_UP @ Override public boolean onSingleTapUp (MotionEvent e) {// TODO Auto-generated method stub toast. setText ("onSingleTapUp"); toast. show (); return false ;}}
 
:http://files.cnblogs.com/llm-android/AddViewDemo.rar
 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.