Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> OnGestureListener圖片的左右滑動實例

OnGestureListener圖片的左右滑動實例

編輯:Android開發實例

  welcome_glide.xml

  <?xml version="1.0" encoding="utf-8"?>

  <LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="match_parent"

  android:layout_height="match_parent">

  <ViewFlipper android:id="@+id/viewflipper"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"/>

  </LinearLayout>

  Java代碼

  import android.app.Activity;

  import android.content.Intent;

  import android.os.Bundle;

  import android.util.DisplayMetrics;

  import android.util.Log;

  import android.view.GestureDetector;

  import android.view.KeyEvent;

  import android.view.MotionEvent;

  import android.view.View;

  import android.view.ViewGroup;

  import android.view.Window;

  import android.view.WindowManager;

  import android.view.GestureDetector.OnGestureListener;

  import android.view.animation.AnimationUtils;

  import android.widget.ImageView;

  import android.widget.ViewFlipper;

  import static android.view.KeyEvent.KEYCODE_BACK;

  public class WelcomeGlide extends Activity implements OnGestureListener{

  private static final String TAG = "Fuchangle.AggHelp";

  private int mScreenWidth ;

  private int mScreenHeight ;

  private GestureDetector detector;

  private ViewFlipper flipper;

  private int mIndexHelpPic = 0;

  private int[] mBgList = {

  R.drawable.guide01,

  R.drawable.guide02,

  R.drawable.guide03,

  R.drawable.guide04,

  R.drawable.guide05

  };

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  requestWindowFeature(Window.FEATURE_NO_TITLE);

  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

  setContentView(R.layout.welcome_glide);

  DisplayMetrics dm = new DisplayMetrics();

  getWindowManager().getDefaultDisplay().getMetrics(dm);

  mScreenWidth = dm.widthPixels;

  mScreenHeight = dm.heightPixels;

  if(mScreenWidth <240){

  mScreenWidth = 240;

  }

  if(mScreenHeight < 320){

  mScreenHeight = 320;

  }

  flipper = (ViewFlipper)this.findViewById(R.id.viewflipper);

  flipper.addView(addImageByID(mBgList[mIndexHelpPic]), new ViewGroup.LayoutParams

  (ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));

  detector = new GestureDetector(this);

  }

  @Override

  public boolean onDown(MotionEvent e) {

  return false;

  }

  @Override

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

  float velocityY) {

  if(e1.getX()-e2.getX() > 120){

  if(++mIndexHelpPic >= mBgList.length){

  mIndexHelpPic = mBgList.length - 1;

  backToMainDesk();

  return true;

  }

  this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));

  this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));

  flipper.addView(addImageByID(mBgList[mIndexHelpPic]),new ViewGroup.LayoutParams(

  ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

  this.flipper.showNext();

  }else if(e1.getX() - e2.getX()< -120){

  if(--mIndexHelpPic < 0){

  mIndexHelpPic = 0;

  return true;

  }

  this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));

  this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));

  this.flipper.showPrevious();

  return true;

  }

  return true;

  }

  private void backToMainDesk(){

  startActivity(new Intent(this,QQActivity.class));

  finish();

  }

  @Override

  public boolean onKeyDown(int keyCode, KeyEvent event) {

  switch (keyCode) {

  case KEYCODE_BACK:

  backToMainDesk();

  return true;

  default:

  break;

  }

  return super.onKeyDown(keyCode, event);

  }

  private View addImageByID(int id){

  ImageView img = new ImageView(this);

  img.setImageResource(id);

  img.setAdjustViewBounds(true);

  img.setMaxWidth(mScreenWidth);

  img.setMaxHeight(mScreenHeight);

  img.setScaleType(ImageView.ScaleType.FIT_XY);

  return img;

  }

  @Override

  public boolean onTouchEvent(MotionEvent event) {

  Log.i("Fling", "Activity onTouchEvent!");

  return this.detector.onTouchEvent(event);

  }

  @Override

  public void onLongPress(MotionEvent e) {

  }

  @Override

  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {

  return false;

  }

  @Override

  public void onShowPress(MotionEvent e) {

  }

  @Override

  public boolean onSingleTapUp(MotionEvent e) {

  return false;

  }

  }

  下面的文件放在res/anim裡面

  push_left_in.xml

  Xml代碼

  <?xml version="1.0" encoding="utf-8"?>

  <set xmlns:android="http://schemas.android.com/apk/res/android">>

  <translate android:fromXDelta="100%p" android:toXDelta="0"

  android:duration="500"/>

  <alpha android:fromAlpha="1.0" android:toAlpha="1.0"

  android:duration="500"/>

  </set>

  push_left_out.xml

  Xml代碼

  <?xml version="1.0" encoding="utf-8"?>

  <set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate android:fromXDelta="0" android:toXDelta="-100%p"

  android:duration="500" />

  <alpha android:fromAlpha="1.0" android:toAlpha="1.0"

  android:duration="500" />

  </set>

  push_right_in.xml

  Xml代碼

  <?xml version="1.0" encoding="utf-8"?>

  <set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate android:fromXDelta="0" android:toXDelta="100%p"

  android:duration="500" />

  <alpha android:fromAlpha="1.0" android:toAlpha="1.0"

  android:duration="500" />

  </set>

  push_right_out.xml

  Xml代碼

  <?xml version="1.0" encoding="utf-8"?>

  <set xmlns:android="http://schemas.android.com/apk/res/android">

  <translate android:fromXDelta="-100%p" android:toXDelta="0"

  android:duration="500" />

  <alpha android:fromAlpha="1.0" android:toAlpha="1.0"

  android:duration="500" />

  </set>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved