Android實現左右滑動效果的方法詳解

來源:互聯網
上載者:User

本樣本示範在Android中實現圖片左右滑動效果。

關於滑動效果,在Android中用得比較多,本樣本實現的滑動效果是使用ViewFlipper來實現的,當然也可以使用其它的View來實現。接下來就讓我們開始實現這種效果。為了方便大家理解,我們先來看一下:
主要如:

 

接下來我們看一下程式結構圖:
 

MainActivity檔案中代碼:

複製代碼 代碼如下:package com.android.flip;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
/**
* Android實現左右滑動效果
* @Description: Android實現左右滑動效果
* @File: MainActivity.java
* @Package com.android.flip
* @Author Hanyonglu
* @Date 2012-02-12 上午10:44:04
* @Version V1.0
*/
public class MainActivity extends Activity implements OnGestureListener {
private ViewFlipper flipper;
private GestureDetector detector;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

detector = new GestureDetector(this);
flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);
flipper.addView(addImageView(R.drawable.one));
flipper.addView(addImageView(R.drawable.two));
flipper.addView(addImageView(R.drawable.three));
flipper.addView(addImageView(R.drawable.four));
flipper.addView(addImageView(R.drawable.five));
}

private View addImageView(int id) {
ImageView iv = new ImageView(this);
iv.setImageResource(id);
return iv;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return this.detector.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 120) {
this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
this.flipper.showNext();
return true;
} else if (e1.getX() - e2.getX() < -120) {
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 false;
}

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

布局介面相對比較簡單,我們只需要加入ViewFlipper就可以,代碼如下:
複製代碼 代碼如下:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewFlipper android:id="@+id/ViewFlipper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ViewFlipper>
</LinearLayout>

為了使其滑動時有一定的特效,我們需要加入Animation效果,說到Animation,我們先看下如何在Android中實現自訂Animation。自訂的Animation是以XML格式定義的,定義好的XML檔案存放在res/anim中。

  一般的Animation有以下四種類型:
  1. Alpha:漸層透明度動畫效果
  2. Scale:漸層尺寸伸縮動畫效果
  3. Translate:畫面轉換位置移動動畫效果
  4. Rotate:畫面轉換位置移動動畫效果

push_left_in.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="0.1" android:toAlpha="1.0"
android:duration="500" />
</set>

push_left_out.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="0.1"
android:duration="500" />
</set>

push_right_in.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="0.1" android:toAlpha="1.0"
android:duration="500" />
</set>

push_right_out.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="0.1"
android:duration="500" />
</set>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.