android样式学习(三) 手势滑动切换图片

使用ViewFlipper和Animation在滑动查看图片效果,比gallery的滚动查看图片好用多了,这个也是实现滚动屏幕切换图片,但是用户体验要好很多!

package cn.gwss.huap;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Gravity;
import android.view.GestureDetector.OnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity implements OnGestureListener {

 private GestureDetector detector;
 private ViewFlipper flipper;
 Intent getMainActivity = null;
 int count = 1;
 int[] imgs = { R.drawable.bg1, R.drawable.bg2, R.drawable.bg3 };

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  drawBackground();
  LayoutInflater inflater = LayoutInflater.from(this);
  final View layout = inflater.inflate(R.layout.view_flipper, null);
  setContentView(layout);
  flipper = (ViewFlipper) findViewById(R.id.view_flipper);
  for (int i = 0; i < imgs.length; i++) {
   int imgId = imgs[i];
   flipper.addView(getView(imgId), i);
  }
  detector = new GestureDetector(this);
 }

 public View getView(int imgId) {
  LinearLayout ll = new LinearLayout(MainActivity.this);
  ll.setOrientation(LinearLayout.HORIZONTAL);
  ll.setPadding(20, 5, 0, 5);
  ll.setGravity(Gravity.LEFT);
  ImageView iv = new ImageView(MainActivity.this); // 创建ImageView对象
  iv.setAdjustViewBounds(true);
  iv.setLayoutParams(new LinearLayout.LayoutParams(480, 480));
  iv.setImageResource(imgId); // 设置ImageView的
  ll.addView(iv); // 将ImageView添加到线性布局中
  return ll;
 }

 public void drawBackground(){
  GradientDrawable grad = new GradientDrawable(
  Orientation.TL_BR,
  new int[] { Color.rgb(0, 0, 127),
  Color.rgb(0, 0, 255),
  Color.rgb(127, 0, 255),
  Color.rgb(127, 127, 255),
  Color.rgb(127, 255, 255),
  Color.rgb(255, 255, 255)}
  );
  this.getWindow().setBackgroundDrawable(grad);
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  return this.detector.onTouchEvent(event);
 }

 public boolean onDown(MotionEvent arg0) {
  return false;
 }

 /**
  * 滑动有一段距离后离开造作界面,触发的时间
  */
 public boolean onFling(MotionEvent e1, MotionEvent e2, float arg2,
 float arg3) {

  Log.i("Fling", "Fling Happened!");
  if (e1.getX() - e2.getX() > 5) {
   this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
   R.anim.push_left_in));
   this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
   R.anim.push_left_out));
   if (count < 3) {
    this.flipper.showNext();
    count++;
   }
   return true;
  } else if (e1.getX() - e2.getX() < -5) {
   this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
   R.anim.push_right_in));
   this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
   R.anim.push_right_out));
   if (count > 1) {
    this.flipper.showPrevious();
    count--;
   }
   return true;
  }
  return true;
 }

 public void onLongPress(MotionEvent arg0) {
 }

 public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
   float arg3) {
  return false;
 }

 public void onShowPress(MotionEvent arg0) {
 }

 public boolean onSingleTapUp(MotionEvent arg0) {
  return false;
 }
}

 

二、view_flipper.xml布局文件

 
 
 
    android:id="@+id/layout" android:orientation="horizontal" 
 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
 
    > 
 
     
        android:layout_width="fill_parent" android:layout_height="fill_parent" 
 
        android:persistentDrawingCache="animation" android:flipInterval="1000" 
 
        android:inAnimation="@anim/push_left_in" android:outAnimation="@anim/push_left_out">
 
 
  

三、anim动画布局文件:

1、push_left_in.xml布局文件:

 
 
 
 
         
                android:duration="500" /> 
 
         
                android:duration="500" /> 
 

2、push_left_out.xml布局文件


 
 
         
                android:duration="500" /> 
 
         
                android:duration="500" /> 
 

3、push_right_in.xml布局文件


 
 
         
                android:duration="500" /> 
 
         
                android:duration="500" /> 
 

4、push_right_out.xml布局文件

 
 
 
 
         
                android:duration="500" /> 
 
         
                android:duration="500" /> 
 

 

《完结》

 

你可能感兴趣的:(android开发)