`
wang_peng1
  • 浏览: 3955061 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

屏幕滑动几张图片

阅读更多
import android.content.Context;
import android.util.Log;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewConfiguration;
import android.widget.Scroller;

public class DragableSpace extends ViewGroup {
    private Scroller mScroller;
    private VelocityTracker mVelocityTracker;

    private int mScrollX = 0;
    private int mCurrentScreen = 0;

    private float mLastMotionX;

    private static final String LOG_TAG = "DragableSpace";

    private static final int SNAP_VELOCITY = 1000;

    private final static int TOUCH_STATE_REST = 0;
    private final static int TOUCH_STATE_SCROLLING = 1;

    private int mTouchState = TOUCH_STATE_REST;

    private int mTouchSlop = 0;

    public DragableSpace(Context context) {
        super(context);
        mScroller = new Scroller(context);

        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

        this.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.FILL_PARENT));
    }

    public DragableSpace(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);

        mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

        this.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT ,
                    ViewGroup.LayoutParams.FILL_PARENT));

        TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.DragableSpace);
        mCurrentScreen = a.getInteger(R.styleable.DragableSpace_default_screen, 0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        /*
         * This method JUST determines whether we want to intercept the motion.
         * If we return true, onTouchEvent will be called and we do the actual
         * scrolling there.
         */

        /*
         * Shortcut the most recurring case: the user is in the dragging state
         * and he is moving his finger. We want to intercept this motion.
         */
        final int action = ev.getAction();
        if ((action == MotionEvent.ACTION_MOVE)
                && (mTouchState != TOUCH_STATE_REST)) {
            return true;
                }

        final float x = ev.getX();

        switch (action) {
            case MotionEvent.ACTION_MOVE:
                /*
                 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
                 * whether the user has moved far enough from his original down touch.
                 */

                /*
                 * Locally do absolute value. mLastMotionX is set to the y value
                 * of the down event.
                 */
                final int xDiff = (int) Math.abs(x - mLastMotionX);

                boolean xMoved = xDiff > mTouchSlop;

                if (xMoved) {
                    // Scroll if the user moved far enough along the X axis
                    mTouchState = TOUCH_STATE_SCROLLING;
                }
                break;

            case MotionEvent.ACTION_DOWN:
                // Remember location of down touch
                mLastMotionX = x;

                /*
                 * If being flinged and user touches the screen, initiate drag;
                 * otherwise don't.  mScroller.isFinished should be false when
                 * being flinged.
                 */
                mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
                break;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                // Release the drag
                mTouchState = TOUCH_STATE_REST;
                break;
        }

        /*
         * The only time we want to intercept motion events is if we are in the
         * drag mode.
         */
        return mTouchState != TOUCH_STATE_REST;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        }
        mVelocityTracker.addMovement(event);

        final int action = event.getAction();
        final float x = event.getX();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.i(LOG_TAG, "event : down");
                /*
                 * If being flinged and user touches, stop the fling. isFinished
                 * will be false if being flinged.
                 */
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }

                // Remember where the motion event started
                mLastMotionX = x;
                break;
            case MotionEvent.ACTION_MOVE:
                // Log.i(LOG_TAG,"event : move");
                // if (mTouchState == TOUCH_STATE_SCROLLING) {
                // Scroll to follow the motion event
                final int deltaX = (int) (mLastMotionX - x);
                mLastMotionX = x;

                //Log.i(LOG_TAG, "event : move, deltaX " + deltaX + ", mScrollX " + mScrollX);

                if (deltaX < 0) {
                    if (mScrollX > 0) {
                        scrollBy(Math.max(-mScrollX, deltaX), 0);
                    }
                } else if (deltaX > 0) {
                    final int availableToScroll = getChildAt(getChildCount() - 1)
                        .getRight()
                        - mScrollX - getWidth();
                    if (availableToScroll > 0) {
                        scrollBy(Math.min(availableToScroll, deltaX), 0);
                    }
                }
                // }
                break;
            case MotionEvent.ACTION_UP:
                Log.i(LOG_TAG, "event : up");
                // if (mTouchState == TOUCH_STATE_SCROLLING) {
                final VelocityTracker velocityTracker = mVelocityTracker;
                velocityTracker.computeCurrentVelocity(1000);
                int velocityX = (int) velocityTracker.getXVelocity();

                if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                    // Fling hard enough to move left
                    snapToScreen(mCurrentScreen - 1);
                } else if (velocityX < -SNAP_VELOCITY
                        && mCurrentScreen < getChildCount() - 1) {
                    // Fling hard enough to move right
                    snapToScreen(mCurrentScreen + 1);
                } else {
                    snapToDestination();
                }

                if (mVelocityTracker != null) {
                    mVelocityTracker.recycle();
                    mVelocityTracker = null;
                }
                // }
                mTouchState = TOUCH_STATE_REST;
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.i(LOG_TAG, "event : cancel");
                mTouchState = TOUCH_STATE_REST;
        }
        mScrollX = this.getScrollX();

        return true;
    }

    private void snapToDestination() {
        final int screenWidth = getWidth();
        final int whichScreen = (mScrollX + (screenWidth / 2)) / screenWidth;
        Log.i(LOG_TAG, "from des");
        snapToScreen(whichScreen);
    }

    public void snapToScreen(int whichScreen) {         
        Log.i(LOG_TAG, "snap To Screen " + whichScreen);
        mCurrentScreen = whichScreen;
        final int newX = whichScreen * getWidth();
        final int delta = newX - mScrollX;
        mScroller.startScroll(mScrollX, 0, delta, 0, Math.abs(delta) * 2);             
        invalidate();
    }

    public void setToScreen(int whichScreen) {
        Log.i(LOG_TAG, "set To Screen " + whichScreen);
        mCurrentScreen = whichScreen;
        final int newX = whichScreen * getWidth();
        mScroller.startScroll(newX, 0, 0, 0, 10);             
        invalidate();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childLeft = 0;

        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                final int childWidth = child.getMeasuredWidth();
                child.layout(childLeft, 0, childLeft + childWidth, child
                        .getMeasuredHeight());
                childLeft += childWidth;
            }
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("error mode.");
        }

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode != MeasureSpec.EXACTLY) {
            throw new IllegalStateException("error mode.");
        }

        // The children are given the same width and height as the workspace
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
        }
        Log.i(LOG_TAG, "moving to screen "+mCurrentScreen);
        scrollTo(mCurrentScreen * width, 0);      
    }  

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            mScrollX = mScroller.getCurrX();
            scrollTo(mScrollX, 0);
            postInvalidate();
        }
    }
}

 

<?xml version="1.0" encoding="utf-8"?>
<com.matthieu.launcher.DragableSpace xmlns:app="http://schemas.android.com/apk/res/com.matthieu.launcher"
    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/space"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:default_screen="1"
>
<include android:id="@+id/left"  layout="@layout/left_screen" />
<include android:id="@+id/center"  layout="@layout/initial_screen" />
<include android:id="@+id/right"  layout="@layout/right_screen" />
</com.matthieu.launcher.DragableSpace>

 values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="DragableSpace">
        <attr name="default_screen" format="integer"/>
    </declare-styleable>
</resources>

分享到:
评论
3 楼 无忧鱼 2012-03-08  
无图无真相啊!
2 楼 张洪财 2011-08-11  

1.<?xml version="1.0" encoding="utf-8"?>  
2.<com.matthieu.launcher.DragableSpace xmlns:app="http://schemas.android.com/apk/res/com.matthieu.launcher" 
3.    xmlns:android="http://schemas.android.com/apk/res/android" 
4.android:id="@+id/space" 
5.android:layout_width="fill_parent" 
6.android:layout_height="fill_parent" 
7.app:default_screen="1" 
8.>  
9.<include android:id="@+id/left"  layout="@layout/left_screen" />  
10.<include android:id="@+id/center"  layout="@layout/initial_screen" />  
11.<include android:id="@+id/right"  layout="@layout/right_screen" />  
12.</com.matthieu.launcher.DragableSpace> 



可以解释下这个文件吗???
1 楼 张洪财 2011-08-11  
Java代码那个是什么 ????

布局文件吗??? 启什么文件名?

相关推荐

    手机滑动切换图片

    在移动设备上,滑动切换图片是一种常见的交互方式,它为用户提供了一种流畅且直观的方式来浏览多张图片。这种功能广泛应用于手机应用、网站、尤其是相册和产品展示等场景。"手机滑动切换图片"这个插件就是为了实现这...

    纯 js 仿手机桌面左右滑动切换图片

    图片通常会被放在一个容器内,容器的宽度应设定为多张图片宽度之和,以便在用户滑动时能显示出下一张图片。为了实现无缝滑动,最后一张图片后面会添加第一张图片的副本,形成循环。 4. **动画框架**: 虽然纯...

    图片多选、滑动

    滑动浏览是一种通过手指滑动屏幕来查看前后图片的交互方式。这种方式在图片库或相册应用中非常常见,它提供了流畅且直观的用户体验。滑动浏览的实现涉及到以下几个关键点: - 滑动检测:通过监听用户的触摸事件,...

    移动手机端图片自动滑动和定时滑动

    基本思路是通过设置一个容器,将多张图片放入其中,然后通过动态改变图片的位置来模拟滑动效果。滑动可以是手动触发,也可以设置定时器自动进行。 二、JavaScript实现 在JavaScript中,可以使用setInterval函数来...

    jquery图片左右滑动

    这种效果允许用户通过手势或按钮在多张图片之间进行切换,增加了用户体验的互动性和趣味性。在本项目中,开发者已经创建了一个基本的实现,尽管页面样式可能较为简单,但核心功能已经具备,用户可以根据自己的需求...

    支持手机触屏左右拖拽滑动图片_html5左右滑动图片切换插件

    在这个案例中,CSS将用于设置图片的展示方式,滑动过渡效果,以及适应不同屏幕尺寸的响应式设计。 3. **images**文件夹:存放待展示的图片资源。这些图片会被HTML引用,并通过插件实现左右滑动的效果。 4. **js**...

    移动端app html5 图片滑动组件

    可以预先加载当前图片及前后几张图片,避免用户在滑动时因图片加载延迟而出现卡顿。同时,可以利用懒加载技术,只有当图片即将进入视口时才开始加载,减少初始加载时间。 最后,考虑到不同设备和浏览器的兼容性问题...

    图片滑动门

    图片滑动门是一种常见的网页和移动应用设计技术,主要用于展示多张图片或内容,通过滑动操作实现平滑切换,给用户带来丰富的视觉体验。在本文中,我们将深入探讨图片滑动门的设计原理、实现方式以及相关的编程技术。...

    图片滑动源代码

    图片滑动通常由JavaScript、CSS和HTML三部分组成,通过动态改变图片的位置或者透明度来实现平滑过渡的效果,为用户提供一种动态浏览多张图片的方式。 描述中提到“打开html文件就能展示,直接打开就可以哦”,这...

    flash图片展示左右滑动效果

    在图片滑动效果中,每张图片可能对应时间轴上的一个关键帧。 3. **元件和库**:Flash的库是一个存储图形、按钮、影片剪辑等可重用资源的地方。图片可以作为影片剪辑元件导入到舞台上,并且可以设置它们的动作脚本以...

    左右滑动图片查看器小例子

    一个能够左右滑动切换图片的查看器可以提升用户体验,使得查看多张图片变得更加流畅和直观。本项目提供了一个简单的左右滑动图片查看器的小例子,适合初学者学习和参考。 【描述】: 虽然描述信息为空,但根据标题...

    使用ViewPager实现图片的滑动

    `HuadongPic`可能是图片数据的文件名列表,可能包含多张图片,用于演示如何在ViewPager中展示这些图片。在实际应用中,这些图片可能来自网络、本地资源或者SD卡。 总的来说,使用ViewPager实现图片滑动是Android...

    滑动图片展示广告效果.rar

    滑动图片展示广告效果主要涉及以下几个关键知识点: 1. **JavaScript基础**:JavaScript是一种客户端脚本语言,它在浏览器中运行,用于处理用户交互、操作DOM(文档对象模型)、执行动画等。在滑动图片广告中,...

    网页图片滑动

    2. **图片容器管理**:设置一个包含多张图片的容器,通过改变容器的位置来实现图片的滑动效果。 3. **动画效果**:利用CSS3的`transition`属性或其他动画技术实现平滑过渡效果。 #### 四、具体实现步骤 1. **HTML...

    jQuery响应式手机端触屏滑动图片轮播插件

    在现代网页设计中,图片轮播已经成为一种常见的展示方式,特别是在手机端,它能够高效地利用有限的屏幕空间来展示多张图片或内容。jQuery作为一款轻量级的JavaScript库,提供了丰富的API和插件,使得开发响应式图片...

    iSlider手机端图片滑动切换.zip

    例如,我们可以创建一个包含多张图片的`&lt;ul&gt;`列表,然后用iSlider的API进行初始化和设置滑动选项。 在压缩包中的文件,我们可以看到可能包含iSlider的源码文件、示例代码、CSS样式文件等,这些资源可以帮助我们更...

    gif 图片混合滑动切换

    最后,为了实现混合滑动切换,我们需要维护一个图片队列,包含当前显示的图片以及前后几张待切换的图片。根据用户的滑动操作,更新队列中的图片顺序,并相应地更新视图显示。同时,考虑到用户体验,可以添加缓冲图片...

    微信滑动帮助解面

    在移动设备上,由于屏幕空间有限,通常会采用滑动手势来浏览多张图片或页面。用户通过手指在屏幕上向左或向右滑动,可以前后切换图片,这种交互方式已经成为移动应用的标准操作之一。微信中的这一特性,使得用户在...

    mootools精美图片滑动效果

    7. **无缝轮播**:为了达到更好的用户体验,可以实现无缝轮播效果,即最后一张图片滑动到最前面,第一张图片滑动到末尾,使得轮播看起来没有起点和终点。 在实际项目中,我们可能会有一个名为`Slider.js`的脚本文件...

    jquery全屏图片滑动切换带左右按钮控制图片滑动切换效果代码

    2. **CSS样式**:设置全屏背景图片的样式,确保图片适应屏幕大小,并且初始状态下只显示第一张图片。左右按钮也要有适当的样式和位置。 3. **jQuery事件绑定**:使用`.click()`方法绑定按钮的点击事件,当用户点击...

Global site tag (gtag.js) - Google Analytics