您当前的位置: 首页 >  android

命运之手

暂无认证

  • 2浏览

    0关注

    747博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【Android】【滑屏】scrollTo,scrollBy

命运之手 发布时间:2016-03-23 14:20:08 ,浏览量:2

Android中View视图是没有边界的,Canvas是没有边界的,我们可以将这种无边界的视图称为“视图坐标”,它不受物理屏幕限制。

通常我们所理解的一个Layout布局文件只是该视图的显示区域,超过了这个显示区域将不能显示到父视图的区域中,我们可以将这种有边界的视图称为“布局坐标”。

一个视图的在屏幕的起始坐标位于视图坐标起始处。

由于该布局位置是只能显示特定的一块视图内容 ,因此我们需要通过scrollTo()或者scrollBy()方法将我们期望的视图滚动至布局坐标上。

public void scrollTo(int x, int y){}
//视图偏移至(x , y)坐标处,即显示区域位从(x , y)开始

public void scrollBy(int x, int y){}
//视图内容继续偏移(x , y)个单位

public final int getScrollX(){}
public final int getScrollY(){}
//获取视图偏移量

在这里插入图片描述 这是一个自定义的MyScrollViewGroup,总共包含3个View,每个都和屏幕一样宽,只能通过滑动才能显示下一个

//自定义ViewGroup
public class MyScrollViewGroup extends ViewGroup
{  
    private Context mContext;  
    private static String TAG = "MultiViewGroup";  
  
    public MyScrollViewGroup(Context context)
    {  
        super(context);  
        mContext = context;  
        init();  
    }  
  
    public MultiViewGroup(Context context,AttributeSet attrs)
    {  
        super(context, attrs);  
        mContext = context;  
        init();  
    }  
  
    private void init()
    {  
        // 初始化3个 LinearLayout控件  
        LinearLayout oneLL = new LinearLayout(mContext);  
        oneLL.setBackgroundColor(Color.RED);  
        addView(oneLL);  
          
        LinearLayout twoLL = new LinearLayout(mContext);  
        twoLL.setBackgroundColor(Color.YELLOW);  
        addView(twoLL);  
          
        LinearLayout threeLL = new LinearLayout(mContext);  
        threeLL.setBackgroundColor(Color.BLUE);  
        addView(threeLL);  
    }  
  
    // measure过程   
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {  
        // 设置该ViewGroup的大小  
        int width = MeasureSpec.getSize(widthMeasureSpec);  
        int height = MeasureSpec.getSize(heightMeasureSpec);  
        setMeasuredDimension(width, height);  
  
        int childCount = getChildCount();  
        for (int i = 0; i < childCount; i++)
        {  
            View child = getChildAt(i);  
            child.measure(MainActivity.screenWidth, MainActivity.screenHeight);  
        }  
    }  
  
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {  
        int startLeft = 0; // 每个子视图的起始布局坐标  
        int startTop = 10; // 间距设置为10px 相当于 android:marginTop= "10px"  
        int childCount = getChildCount();  

        for (int i = 0; i < childCount; i++)
        {  
            View child = getChildAt(i);  
            child.layout(startLeft, startTop, startLeft + MainActivityActivity.screenWidth, startTop + MainActivity.screenHeight);  
            startLeft = startLeft + MainActivity.screenWidth ; //校准每个子View的起始布局位置  
            //三个子视图的在屏幕中的分布如下[x1 , x2] [0 , 320] - [320,640] - [640,960]  
        }  
    }  
}  
关注
打赏
1654938663
查看更多评论
立即登录/注册

微信扫码登录

0.0617s