java - 如何水平滚动图像?

标签 java android

我在水平 ScrollView 方面遇到问题。

这是我的 XML 代码:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/home_title_id"
        android:text="@string/home_title"
        android:layout_marginTop="@dimen/forty_text_size"
        android:textColor="@android:color/white"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textSize="@dimen/forty_text_size" />

    <LinearLayout
        android:id="@+id/linear_layout_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/home_title_id">

        <HorizontalScrollView
            android:id="@+id/horizontal_view_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/home_image_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/abc" />
        </HorizontalScrollView>

         </LinearLayout>
        </RelativeLayout>

在我的 Java 类中,扩展 Activity 我声明了所有小部件,然后创建了一个类 ScrollView ,扩展了水平 ScrollView 。

Java代码如下:

class scrollview extends HorizontalScrollView 
    {
        private static final int SWIPE_MIN_DISTANCE = 5;
        private static final int SWIPE_THRESHOLD_VELOCITY = 300;
        private ArrayList mItems = null;
        private GestureDetector mGestureDetector;
        private int mActiveFeature = 0;


        public scrollview(Context context) 
        {
            super(context);
        }

        public void setFeatureItems(ArrayList items)
        {
            LinearLayout internalWrapper = new LinearLayout(getContext());
            internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
            addView(internalWrapper);
            this.mItems = items;
            for(int i = 0; i< items.size();i++)
            {
                LinearLayout featureLayout = (LinearLayout) View.inflate(this.getContext(),R.layout.activity_main,null);
                //...
              //Create the view for each screen in the scroll view
                //...
                internalWrapper.addView(featureLayout);
            }
            setOnTouchListener(new View.OnTouchListener() 
            {
                @Override
                public boolean onTouch(View v, MotionEvent event) 
                {
                    //If the user swipes
                    if (mGestureDetector.onTouchEvent(event)) 
                    {
                        return true;
                    }
                    else if(event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL )
                    {
                        int scrollX = getScrollX();
                        int featureWidth = v.getMeasuredWidth();
                        mActiveFeature = ((scrollX + (featureWidth/2))/featureWidth);
                        int scrollTo = mActiveFeature*featureWidth;
                        smoothScrollTo(scrollTo, 0);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            });
            mGestureDetector = new GestureDetector(new MyGestureDetector());
        }
            class MyGestureDetector extends SimpleOnGestureListener 
            {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
            {
                try 
                {
                    //right to left
                    if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
                    {
                        int featureWidth = getMeasuredWidth();
                        mActiveFeature = (mActiveFeature < (mItems.size() - 1))? mActiveFeature + 1:mItems.size() -1;
                        smoothScrollTo(mActiveFeature*featureWidth, 0);
                        return true;
                    }
                    //left to right
                    else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) 
                    {
                        int featureWidth = getMeasuredWidth();
                        mActiveFeature = (mActiveFeature > 0)? mActiveFeature - 1:0;
                        smoothScrollTo(mActiveFeature*featureWidth, 0);
                        return true;
                    }
                } catch (Exception e) {
                        Log.e("Fling", "There was an error processing the Fling event:" + e.getMessage());
                }
                return false;
            }
        }
    }

但没有任何反应,请帮忙。

或者在 ONTOUCH 监听器功能中添加 SCROLL 类会更好吗???

最佳答案

我认为您的 layout.xml 文件中的标签位置错误。将具有 ImageViewLinearLayout 放置在 Horizo​​ntalScrollView 中,而不是将 Horizo​​ntalScrollView 放置在 LinearLayout 中,如下所示:

   <HorizontalScrollView
        android:id="@+id/horizontal_view_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/home_title_id" >
   <LinearLayout
    android:id="@+id/linear_layout_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/home_image_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/abc" />
    </LinearLayout>
    </HorizontalScrollView>

关于java - 如何水平滚动图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15105791/

相关文章:

java - Spring MVC/hibernate 表单验证,不返回表单

android - 如何在android中使用日期和时间选择器,我有什么错误以及如何解决

android - 如何将 HashMap 保存到共享首选项?

java - 如何可以单击透明 Activity 后面的按钮

java - #java.lang.NoClassDefFoundError : org/apache/commons/digester/Digester

java - JTextArea 不动态更新

android - 如何将我的 getEmptyForeignCollection() 对象与我的父对象链接起来?

Android:MVP:恢复状态。将数据存储在演示器中以供查看是一个很好的解决方案吗?

java - 将位图转换为 ASCII 艺术

java - 如何将原始 html 传递给 Play 框架 View ?