首页 > 代码库 > ViewFlipper动态加载View

ViewFlipper动态加载View

知识点

     1.ViewFlipper基本用法及动态添加View;

     2.ViewFlipper结合手势翻页+特效;

     3.WindowManager的基本用法。

任意张图片实现循环滑动,其实PageFlipper当前最多的子View个数(ChildViewCount)小于等于2.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ViewFlipper
        android:id="@+id/myViewFlipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        
    </ViewFlipper>

</LinearLayout>
package com.xyz.pagefilter;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class MainActivity extends Activity implements OnTouchListener,
        OnGestureListener {

    private LayoutInflater mInflater;
    private WindowManager wm = null;
    private WindowManager.LayoutParams wmParams = null;

    private ImageView leftbtn = null;
    private ImageView rightbtn = null;

    private int mAlpha = 0;
    private boolean isHide;

    private int mCurrPos = 0;

    private ViewFlipper viewFlipper = null;
    private GestureDetector mGestureDetector;

    private int[] mImages = new int[] { R.drawable.img_0, R.drawable.img_1,
            R.drawable.img_2, R.drawable.img_3, R.drawable.img_4,
            R.drawable.img_5, R.drawable.img_6, R.drawable.img_7,
            R.drawable.img_8, R.drawable.img_9 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        viewFlipper = (ViewFlipper) this.findViewById(R.id.myViewFlipper);
        setView(mCurrPos, 0);
       &nbsp;initFloatView();

        // viewFlipper.setLongClickable(true);
        viewFlipper.setOnTouchListener(this);
        mGestureDetector = new GestureDetector(this);

    }

    private void initFloatView() {
        wm = (WindowManager) getApplicationContext().getSystemService(
                Context.WINDOW_SERVICE);
        wmParams = new WindowManager.LayoutParams();

        wmParams.type = LayoutParams.TYPE_PHONE; 
        wmParams.format = PixelFormat.RGBA_8888; 
        
        wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
                | LayoutParams.FLAG_NOT_FOCUSABLE;

        wmParams.x = 0;
        wmParams.y = 0;

        wmParams.width = 50;
        wmParams.height = 50;

        createLeftFloatView();
        createRightFloatView();
    }

    private void createLeftFloatView() {
        leftbtn = new ImageView(this);
        leftbtn.setImageResource(R.drawable.prev);
        leftbtn.setAlpha(0);
        leftbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                movePrevious();
            }
        });
        
        wmParams.gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
        
        wm.addView(leftbtn, wmParams);
    }

    private void createRightFloatView() {
        rightbtn = new ImageView(this);
        rightbtn.setImageResource(R.drawable.next);
        rightbtn.setAlpha(0);
        rightbtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                moveNext();
            }
        });
        
        wmParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
        
        wm.addView(rightbtn, wmParams);
    }

    private void setView(int curr, int next) {
        View v = (View) mInflater.inflate(R.layout.flipper_item, null);
        ImageView iv = (ImageView) v.findViewById(R.id.img);
        // iv.setScaleType(ImageView.ScaleType.FIT_XY);
        if (curr < next && next > mImages.length - 1)
            next = 0;
        else if (curr > next && next < 0)
            next = mImages.length - 1;
        iv.setImageResource(mImages[next]);
        if (viewFlipper.getChildCount() > 1) {
            viewFlipper.removeViewAt(0);
        }
        viewFlipper.addView(v, viewFlipper.getChildCount());
        mCurrPos = next;

    }

    private void movePrevious() {
        setView(mCurrPos, mCurrPos - 1);
        viewFlipper.setInAnimation(MainActivity.this, R.anim.in_leftright);
        viewFlipper.setOutAnimation(MainActivity.this, R.anim.out_leftright);
        viewFlipper.showPrevious();
    }

    private void moveNext() {
        setView(mCurrPos, mCurrPos + 1);
        viewFlipper.setInAnimation(MainActivity.this, R.anim.in_rightleft);
        viewFlipper.setOutAnimation(MainActivity.this, R.anim.out_rightleft);
        viewFlipper.showNext();
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1 && mAlpha < 255) {
                mAlpha += 50;
                if (mAlpha > 255)
                    mAlpha = 255;
                leftbtn.setAlpha(mAlpha);
                leftbtn.invalidate();
                rightbtn.setAlpha(mAlpha);
                rightbtn.invalidate();
                if (!isHide && mAlpha < 255)
                    mHandler.sendEmptyMessageDelayed(1, 100);
            } else if (msg.what == 0 && mAlpha > 0) {
                mAlpha -= 10;
                if (mAlpha < 0)
                    mAlpha = 0;
                leftbtn.setAlpha(mAlpha);
                leftbtn.invalidate();
                rightbtn.setAlpha(mAlpha);
                rightbtn.invalidate();
                if (isHide && mAlpha > 0)
                    mHandler.sendEmptyMessageDelayed(0, 100);
            }
        }
    };

    private void showFloatView() {
        isHide = false;
        mHandler.sendEmptyMessage(1);
    }

    private void hideFloatView() {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(1500);
                    isHide = true;
                    mHandler.sendEmptyMessage(0);
                } catch (Exception e) {
                }
            }
        }.start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        wm.removeView(leftbtn);
        wm.removeView(rightbtn);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        int x = (int) (e2.getX() - e1.getX());
        if (x > 0) {
            movePrevious();
        } else {
            moveNext();
        }
        return false;
    }

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

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_DOWN:
            showFloatView();
            break;
        case MotionEvent.ACTION_UP:
            hideFloatView();
            break;
        }
        mGestureDetector.onTouchEvent(event);
        return true;
    }
}

附加特效文件:

 

in_leftright.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toXDelta="0" />

    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

</set>

in_rightleft.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0" />
    
    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

out_leftright.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="100%p" />
    
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

out_rightleft.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>