【问题标题】:How to get a horizontal scroll view for Images in android?如何在android中获得图像的水平滚动视图?
【发布时间】:2011-11-30 11:15:00
【问题描述】:

我计划包含水平滚动视图的功能,该功能会为图库中的图像动态加载。我已经尝试过 IMAGE GALLERY,但其中存在一定的限制。如何通过水平滚动条动态加载图像。谢谢。

【问题讨论】:

    标签: android gallery horizontal-scrolling


    【解决方案1】:

    XML:(设计.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
      <FrameLayout android:layout_width="90px" android:layout_height="90px">
        <RelativeLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent">        
        </RelativeLayout>
    </FrameLayout>
    </FrameLayout>
    

    Java 代码:

    public class Example extends Activity {
      private RelativeLayout container;
      private int currentX;
      private int currentY;
    
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.design);
    
        container = (RelativeLayout)findViewById(R.id.container);
    
        int top = 0;
        int left = 0;
    
        ImageView image1 = ...
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image1, layoutParams);
    
        ImageView image2 = ...
        left+= 100;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image2, layoutParams);
    
        ImageView image3 = ...
        left= 0;
        top+= 100;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image3, layoutParams);
    
        ImageView image4 = ...
        left+= 100;     
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(left, top, 0, 0);               
        container.addView(image4, layoutParams);
      }     
    
      @Override 
      public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                currentX = (int) event.getRawX();
                currentY = (int) event.getRawY();
                break;
            }
    
            case MotionEvent.ACTION_MOVE: {
                int x2 = (int) event.getRawX();
                int y2 = (int) event.getRawY();
                container.scrollBy(currentX - x2 , currentY - y2);
                currentX = x2;
                currentY = y2;
                break;
            }   
            case MotionEvent.ACTION_UP: {
                break;
            }
        }
          return true; 
      }
    }
    

    希望对你有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多