Home
› Uncategorized
In this exercise, I will implement GestureDetector with OnGestureListener for a ImageView of icon. Once the user fling on the icon, it will be moved (animated using ObjectAnimator).
In order to use android.animation.ObjectAnimator, minSdkVersion="11" is needed.
Download the files.
package com.example.androidanimatefling; import android.os.Bundle; import android.animation.ObjectAnimator; import android.app.Activity; import android.util.DisplayMetrics; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.GestureDetector.OnGestureListener; import android.view.View.OnTouchListener; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { TextView info; ImageView flingObj; FrameLayout mainScreen; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); info = (TextView)findViewById(R.id.info); flingObj = (ImageView)findViewById(R.id.flingobject); mainScreen = (FrameLayout)findViewById(R.id.mainscreen); final GestureDetector myGesture = new GestureDetector(this, new MyOnGestureListener()); flingObj.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { return myGesture.onTouchEvent(event); }}); flingObj.setClickable(true); } class MyOnGestureListener implements OnGestureListener{ int MIN_DIST = 100; @Override public boolean onDown(MotionEvent arg0) { // TODO Auto-generated method stub return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { float e1X = e1.getX(); float e1Y = e1.getY(); float e2X = e2.getX(); float e2Y = e2.getY(); float distX = e2X - e1X; float distY = e2Y - e1Y; info.setText( "e1X e1Y : " + String.valueOf(e1X) + " : " + String.valueOf(e1Y) + "\n" + "e2X e2Y : " + String.valueOf(e2X) + " : " + String.valueOf(e2Y) + "\n" + "velocityX : " + String.valueOf(velocityX) + "\n" + "velocityY : " + String.valueOf(velocityY)); //Get the Y OFfset DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int offsetY = displayMetrics.heightPixels - mainScreen.getMeasuredHeight(); int[] location = new int[2]; flingObj.getLocationOnScreen(location); float orgX = location[0]; float orgY = location[1] - offsetY; float stopX = orgX + distX; float stopY = orgY + distY; if (distX > MIN_DIST) { //Fling Right ObjectAnimator flingAnimator = ObjectAnimator.ofFloat(flingObj, "translationX", orgX, stopX); flingAnimator.setDuration(1000); flingAnimator.start(); }else if(distX < - MIN_DIST){ //Fling Left ObjectAnimator flingAnimator = ObjectAnimator.ofFloat(flingObj, "translationX", orgX, stopX); flingAnimator.setDuration(1000); flingAnimator.start(); }else if (distY > MIN_DIST) { //Fling Down ObjectAnimator flingAnimator = ObjectAnimator.ofFloat(flingObj, "translationY", orgY, stopY); flingAnimator.setDuration(1000); flingAnimator.start(); }else if(distY < - MIN_DIST){ //Fling Up ObjectAnimator flingAnimator = ObjectAnimator.ofFloat(flingObj, "translationY", orgY, stopY); flingAnimator.setDuration(1000); flingAnimator.start(); } return true; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; }}; }
<FrameLayout 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:id="@+id/mainscreen"> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/flingobject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/padding_medium" android:src="@drawable/ic_launcher" tools:context=".MainActivity" /> </FrameLayout>
In order to use android.animation.ObjectAnimator, minSdkVersion="11" is needed.

-- Delivered by Feed43 service
Related Posts
There is no other posts in this category.Popular
-
The Avengers – Iron Man Mark VII 1.1 APKThe Avengers – Iron Man Mark VII 1.1 APK Suit up and Blast off in the First-Ever Fully-Interactive …
-
[Android] The Dark Knight Rises v1.1.1 APK by GameloftThe Dark Knight Rises is one of the hottest games just released by Gameloft in July 2012 on Androi…
-
How To: Copy And Paste In Android Mobile'>The copy, cut, paste and select the features Android has many nuances. In this how-to, we will foc…
-
Angry Birds Space v1.0.1 for AndroidAngry Birds Space v1.0.1 for Android The #1 mobile game of all time blasts off into space! All new …
-
[Android] Battleloot Adventure HD v1.0.7 (apk + data)In Battleloot Adventure HD , let's roll out and your adventure is about to begin. In the kingdo…
-
Facebook for Android v1.8.3Facebook for Android v1.8.3 Requires Android:1.5 and up Share status updates from your home screen,…
-
[Android] Zuma's Revenge HD v1.0.8 Free Download [Mediafire]Zuma’s Revenge Android , similar to Bonsai Blast, is one of the most fascinating puzzle game fr…
Post a Comment
Post a Comment