怎么让nestedscrollvieww 最上方空出一段距离

ScrollView广告栏 - 简书
下载简书移动应用
写了66356字,被3人关注,获得了2个喜欢
ScrollView广告栏
第二天学习爱限免的专题适配,以及通过两种思路来解决ScrollView适配的问题。一是代码实现,代码实现也有区别无论那种代码,都避不开一个问题就是:如何在适配的时候确定ContenSize的值!先说一下我是如何通过Xib实现ScrollView的适配。理一理我当时的思路,要想三张View视图拉长ScrollView的Contensize。要想拉长ContenSize就必须满足一个调节键就是,必须有一个view视图即连接着ScrollView又连接着ScrollView的右边,当然也不是一定直接发生联系,间接的也是可以的。间接就是说这个连接ScrollView右边的视图的左边不一定非得是一个有多少距离的约束,这中间也可以放入视图作为一段约束线的替代。所以只要满足这个条件,ScrollView便具有了滚动的功能。可是我们还要实现一个效果就是,如何才能让这里ScrollView里的三张View视图都是以屏幕的宽度来出现。而且无论什么屏幕都会自动伸缩,首先想到什么,当然首先想到的就是设置View视图的宽度为屏幕的宽度,这样就无论什么样的屏幕都是可以自动伸缩了嘛!所以在Xib里面如何找到屏幕的宽度呢?自然而然就是先约束ScrollView的宽度等于屏幕的宽度(也就是限制左边距和右边距都为0),当然高度通常都是由整体的比例而设定,而且通常情况下,高度都是固定的,只需要将ScrollView的顶部限制设置为0就可以使得ScrollView自动对顶了。接着只需设置View视图的时候将第一个View视图的左边距(与ScrollView的左边)设置为0即可,当然上边距同理,现在就是设置View的宽度等价与屏幕宽度了,只不过这里没有屏幕宽度,只有ScrollView的宽度,但因为之前所作出的一系列限制,其实现在的ScrollView宽度等价与屏幕宽度,因此,只需设置等价于ScrollView宽度即可。那么一次添加视图,让所有的View视图等宽等高,接着一定要注意一点就是,一定要设置好每两个View视图间的水平距离和最后一个视图的右边距和第一个视图的左边距,这是使得ScrollView可以拉伸的必要条件。那么现在思考ScrollView里最关键的地方了,通过代码来实现ScrollView的适配。这里面依然要考虑两个问题,问题一:如何拉伸ContenSize。问题二:如何让子视图View自适应屏幕宽度。问题一的解决方案有两种,方案一是通过先在ScrollView上添加ContentViewd的方式。方案二是直接在ScrollView上添加UIImageView的方式来解决。那么你会问了,这两种方案有什么区别呢?先说说联系吧!都是一个本质,就是一个视图既连接ScrollView左边,又连接ScrollView的右边。唯有这样ScrollView才可以滚动。两者的差别在于方案一相当于将抽象的ContenSize实例化,直接令这个ContenSize与ScrollView的左边距和右边距都为0,就相当于提前设置好了ContenSize有没有?只不过这个ContenSize到底有多大?还是得等ContenSize上面添加完视图再说!当然在添加每一个子视图View的时候务必需要注意的一点就是在为每一个视图添加约束时,一定要特别考虑到第一个视图的左边和最后一个视图的右边。至于左边,那么又如何区分第一个子视图的其它子视图的左边呢?当然,最好的方式就是通过三目运算符判断lastView的值是否为空。如果为空,自然这就是第一次创建子视图嘛!所以必须这个视图的左边不是用来设置与上一个视图lastView的水平间距,而是应该设置与ContenSize的水平间距。所以通过三目运算符(lastView ? lastView.right : @0),你可能又会问了,@0是什么意思,为什么都没写ContenSize.left呢?我只想说,不要问我,我不想说。如果固定每一子视图的左边,还有上边,还有下边,还有宽度(或者说子视图等高等宽)。那么按理说不需要右边距也就可以将每一个子视图固定住了。因为有固定的宽度嘛!等于屏幕的宽度。但是的但是,其它子视图没有右边距还行,唯独最后一个子视图没有右边距不行!因为最后一个子视图的右边距确认之后,系统才能够确定ContenSize到底为多大!所以重要性你懂的!意义明确后,可是怎么实现呢?难道还在约束每一个子视图的时候添加判断,判断这个子视图是否是最后一个子视图,然后再设置最后一个视图的右边距么?当然不是,因为这样带来一个问题就是这个判断的条件执行的前提是必须提前知道这个for循环究竟会被执行多少次。然后判断当 i 的值变成了最后一次循环执行时就判断为真自然就确定了ContenSize的大小!所以这样如果如果改变子视图的个数就需要该两个地方。是不是很麻烦,还有一种设置右边距的解决方案就是在循环结束后再对ContenSize或说ScrollView进行与最后子视图lastView的约束!这样不同的地方就在于不用考虑for循环到底执行了多少次。只是在for循环结束后增添了一个父视图与最后子视图的右边约束罢了!
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
选择支付方式:今天看啥 热点:
速递花新特性界面滑动一段后scrollView才滚动的 实现方法,新特性scrollview
1.让 scrollView不可以根据用户手指滚动
self.scrollView.scrollEnabled =
2.给 scrollView添加手势
& & UIGestureRecognizer *gesture = [[UIPanGestureRecognizer
initWithTarget:self
action:@selector(dragView:)];
& & [self.scrollView
addGestureRecognizer:gesture];
3.处理scrollView的手势
#pragma mark 手势识别器回调方法
- (void)dragView:(UIPanGestureRecognizer *)gesture{
switch (gesture.state) {
& & & & case
UIGestureRecognizerStateBegan:
& & & & & &
_startPoint = [gesture
translationInView:gesture.view];
& & & & & &
&& & & & & &
& & & & case
UIGestureRecognizerStateEnded:
& & & & & &
CGPoint endPoint = [gesture
translationInView:gesture.view];
& & & & & &
if (_startPoint.x - endPoint.x &
& & & & & & & &
CGPoint start = self.scrollView.contentOffset;
& & & & & & & &
CGPoint end = CGPointMake(start.x +
& & & & & & & & [self.scrollView
setContentOffset:end
animated:YES];
& & & & & & }
& & & & & &
相关搜索:
相关阅读:
相关频道:
Android教程最近更新13:36 提问
ScrollView中控件获得焦点后滑动距离控制
请教一个问题:
例如在一个scrollview里面有一个button1,位于本屏幕最下方,然后我按下方向键"下",这时焦点会转移到button1下面的button2,然后scrollview会自动向下滚动一点,把button2完全显示出来
我想做的事,让scrollview多滚动一段距离,让button2位于屏幕的中间,请问如何做到
按赞数排序
监听下按的按钮事件,然后scrollView.scrollTo( x, y ); 不过你需要得到button2的坐标,然后代入前面即可
其他相似问题
相关参考资料&?xml version="1.0" encoding="utf-8"?&
&ScrollView xmlns:android="/apk/res/android"
android:id="@+id/sv_controll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#b5555555"
android:gravity="center"
android:orientation="vertical" &
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" &
&ImageView
android:id="@+id/iv_exit"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_gravity="right"
android:layout_marginRight="2dip"
android:layout_marginTop="5dip"
android:baselineAlignBottom="true"
android:src="@drawable/menu_delete" /&
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="水印信息" /&
android:id="@+id/tv_wm1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:1" /&
android:id="@+id/tv_artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌名:夜曲" /&
android:id="@+id/tv_songName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌手:同杰伦" /&
android:id="@+id/tv_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="专辑:十一月的肖邦" /&
android:id="@+id/tv_trackNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="相册:XXXX" /&
android:id="@+id/tv_wm2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:2" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
android:id="@+id/tv_wm1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:1" /&
android:id="@+id/tv_artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌名:夜曲" /&
android:id="@+id/tv_songName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌手:同杰伦" /&
android:id="@+id/tv_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="专辑:十一月的肖邦" /&
android:id="@+id/tv_trackNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="相册:XXXX" /&
android:id="@+id/tv_wm2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:2" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
android:id="@+id/tv_wm1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:1" /&
android:id="@+id/tv_artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌名:夜曲" /&
android:id="@+id/tv_songName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌手:同杰伦" /&
android:id="@+id/tv_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="专辑:十一月的肖邦" /&
android:id="@+id/tv_trackNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="相册:XXXX" /&
android:id="@+id/tv_wm2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:2" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
&/LinearLayout&
&/ScrollView&
怎么样实现ScrollView滚动的时候ImageView还在屏幕当中的右上角,而不随ScrollView滚动?各位 有没有什么好的思路??告诉我下...------解决方案--------------------
&?xml version="1.0" encoding="utf-8"?&&FrameLayout xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" &
&LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="top|right"
android:layout_gravity="top|right"&
&ImageView
android:id="@+id/iv_exit"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_gravity="right"
android:layout_marginRight="2dip"
android:layout_marginTop="5dip"
android:baselineAlignBottom="true"
android:src="@drawable/menu_delete" /&
&/LinearLayout&&ScrollView xmlns:android="/apk/res/android"
android:id="@+id/sv_controll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#b5555555"
android:gravity="center"
android:orientation="vertical" &
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" &
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="水印信息" /&
android:id="@+id/tv_wm1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:1" /&
android:id="@+id/tv_artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌名:夜曲" /&
android:id="@+id/tv_songName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌手:同杰伦" /&
android:id="@+id/tv_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="专辑:十一月的肖邦" /&
android:id="@+id/tv_trackNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="相册:XXXX" /&
android:id="@+id/tv_wm2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:2" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
android:id="@+id/tv_wm1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:1" /&
android:id="@+id/tv_artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌名:夜曲" /&
android:id="@+id/tv_songName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌手:同杰伦" /&
android:id="@+id/tv_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="专辑:十一月的肖邦" /&
android:id="@+id/tv_trackNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="相册:XXXX" /&
android:id="@+id/tv_wm2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:2" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
android:id="@+id/tv_wm1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:1" /&
android:id="@+id/tv_artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌名:夜曲" /&
android:id="@+id/tv_songName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="歌手:同杰伦" /&
android:id="@+id/tv_album"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="专辑:十一月的肖邦" /&
android:id="@+id/tv_trackNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="相册:XXXX" /&
android:id="@+id/tv_wm2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:text="WM1:2" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
android:id="@+id/tv_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="公司/单位:金光艺科技有限公司" /&
android:id="@+id/tv_tel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginTop="2dip"
android:text="电话:" /&
&/LinearLayout&&/ScrollView&&/FrameLayout&
怎么对ListView的某些行特殊显示
如何对ListView的某些行特殊显示?
&asp:ListView ID="GuardLog_List" runat="server" DataKeyNames="FAMILY_ID" DataSourceID="GuardLog_Datasource"
OnSorting="GuardLog_Data_List_Sorting"&
&LayoutTemplate&
&table id="UserData" class="TableSub"&
&th scope="col"&
&%--拆迁户档案号--%&
&asp:LinkButton ID="sortBy_DocNum" runat="server" Text="档案号" CommandName="Sort" CommandArgument="DOCUMENT_NUM"
ToolTip="排序" /&
&th scope="col"&
&%--封房号--%&
&asp:LinkButton ID="ImageButton1" runat="server" Text="封房号" CommandName="Sort" CommandArgument="SEAL_NO"
ToolTip="排序" /&
&th scope="col"&
&%--选房号--%&
&asp:LinkButton ID="LinkButton1" runat="server" Text="选房号" CommandName="Sort" CommandArgument="CHECK_NO"
ToolTip="排序" /&
协议书编号
&asp:LinkButton ID="sortBy_ContractNo" runat="server" Text="协议书编号" CommandName="Sort"
CommandArgument="CONTRACT_NO" ToolTip="排序" /&
&th scope="col"&
&%--房屋所有权人--%&
&asp:LinkButton ID="sortBy_OwnerName" runat="server" Text="产权人" CommandName="Sort"
CommandArgument="OWNER_NAME" ToolTip="排序" /&
&th scope="col"&
&%--房屋确权人--%&
&asp:LinkButton ID="sortBy_Confirm_Name" runat="server" Text="确权人" CommandName="Sort"
CommandArgument="CONFIRMER_NAME" ToolTip="排序" /&
&th scope="col"&
房屋使用人--%&
&asp:LinkButton ID="orderBy_ResidentName" runat="server" Text="使用人" CommandName="Sort"
CommandArgument="RESIDENT_NAME" ToolTip="排序" /&
&th scope="col"&
产权所属情况--%&
&asp:LinkButton ID="orderBy_PropertyType" runat="server" Text="产权" CommandName="Sort"
CommandArgument="PROPERTY_TYPE" ToolTip="排序" /&
&th scope="col"&
&%--房屋门牌--%&
&asp:LinkButton ID="orderBy_Address" runat="server" Text="房屋名称" CommandName="Sort"
CommandArgument="ADDRESS" ToolTip="排序" /&
&th scope="col"&
&%--安置方式--%&
&asp:LinkButton ID="LinkButton2" runat="server" Text="安置方式" CommandName="Sort" CommandArgument="REAL_SETTLE_WAY"
ToolTip="排序" /&
&th scope="col"&
&%--当前位置--%&
&asp:LinkButton ID="orderBy_ArarNname" runat="server" Text="当前位置" CommandName="Sort"
CommandArgument="AREA_NAME" ToolTip="排序" /&
&th scope="col"&
&asp:Label ID="modify_lbl2" runat="server" Text="详细轨迹信息"&&/asp:Label&
&asp:PlaceHolder ID="itemPlaceholder" runat="server"&&/asp:PlaceHolder&
&/LayoutTemplate&
&ItemTemplate&
&td align="center"&
&%#Eval("DOCUMENT_NUM")%&
&td align="center"&
&%#Eval("CONTRACT_NO")%&
&td align="center"&
&%#Eval("SEAL_NO")%&
&td align="center"&
&%#Eval("CHECK_NO") == "" ? "未抽号":Eval("CHECK_NO")%&
&td align="center"&
&%#Eval("OWNER_NAME")%&
&td align="center"&
&%#Eval("CONFIRMER_NAME")%&
急求高手改程序!光线追踪算法的实现,用vc++上的opengl
急!!求高手改程序!光线追踪算法的实现,用vc++下的opengl!
我想写一个画两个不透明的球的光线追踪算法,但不知道哪里出了问题,画出来的是黑白相间的直线条纹。求高手帮忙看一下!不胜感谢!
//下面针对反射实现光线跟踪算法
#include "stdafx.h"
//*******************************************************************
//三维向量的定义、两种构造函数及运算符+、-、*、/的重载
class vector
vector(){x=0;y=0;z=0;}
vector(float a,float b,float c){x=a;y=b;z=c;}
friend vector operator +(vector &c1,vector &c2)//重载+使其能完成两个向量的求和
return vector(c1.x+c2.x,c1.y+c2.y,c1.z+c2.z);
friend float operator *(vector &c1,vector &c2)//重载*使其能完成两个向量的点积
return c1.x*c2.x+c1.y*c2.y+c1.z*c2.z;
friend vector operator *(vector &c1,float a)//重载*使其能完成向量和数的相乘
return vector(c1.x*a,c1.y*a,c1.z*a);
friend vector operator /(vector &c1,float a)//重载/使其能完成向量和数值的除法
return vector(c1.x/a,c1.y/a,c1.z/a);
friend vector operator -(vector &c1,vector &c2)//重载-使其能完成向量相减
return vector(c1.x-c2.x,c1.y-c2.y,c1.z-c2.z);
//颜色定义、两种构造函数及运算符+、-的重载
class color
color(){R=0;G=0;B=0;}//黑
color(float a,float b,float c){R=a;G=b;B=c;}
friend color operator +(color &c1,color &c2)//重载+使其能完成两个向量的求和
return color(c1.R+c2.R,c1.G+c2.G,c1.B+c2.B);
friend color operator -(color &c1,color &c2)
return color(c1.R-c2.R,c1.G-c2.G,c1.B-c2.B);
friend color operator *(color &c1,float z)
return color(c1.R*z,c1.G*z,c1.B*z);
friend color operator *(color &c,vector &v)
return color(c.R*v.x,c.G*v.y,c.B*v.z);
friend color operator /(color &c1,float z)
return color(c1.R/z,c1.G/z,c1.B/z);
//光线定义:光色、传播矢量、起点
class light//光线的定义
vector L;//传播方向的单位矢量
//“相对光源”位置(起点位置)
//单色光滑球体的定义:球心、半径、材料的光反射属性
class sphere
//球心位置
float Ka;//环境光反射属性
float K//漫反射光(红光分量)
float Ks;//镜面反射
float light_n;//高光系数
sphere(float hx,float hy,float hz,float rl
,float ka,float kdr,float kdg,float kdb,float ks,float n)
heart.x=heart.y=heart.z=r=Ka=
Kdr=Kdr=Kdg=Kdb=Ks=light_n=n;
///////////////////////////////////////////////////////////////////////////
//求两点间的距离
float distance(vector c1,vector c2)
return sqrt((c1-c2)*(c1-c2));
//求两点间的单位矢量
vector view(vector c,vector v)
return (v-c)/sqrt((v-c)*(v-c));
//求球体a和光线v的交点
vector intersection(sphere a,light v)
float b=v.L*(v.loc-a.heart)*2;
float c=(v.loc-a.heart).x*(v.loc-a.heart).x+(v.loc-a.heart).y*(v.loc-a.heart).y+(v.loc-a.heart).z*(v.loc-a.heart).z-a.r*a.r;
float d=b*b-4*c;//判断是否有交点的量
return v.//无交点
return v.loc-v.L*b/2;//R=V-b/2*d即为光线与球的交点。
return v.//交点不在光线上
float t1=(sqrt(b*b-4*c)-b)/2;
float t2=(-b-sqrt(b*b-4*c))/2;
if(t1&0&&t2&0)
return v.//交点不在光线上
{//交点为R(t0)
return v.loc+v.L*t2;
return v.loc+v.L*t1;
if(t1&=0&&t2&=0)
int t0=t1&t2?t2:t1;
return v.loc+v.L*t0;
vector intersection(sphere a,vector loc,vector L)
float b=L*(loc-a.heart)*2;
float c=(loc-a.heart).x*(loc-a.heart).x+(loc-a.heart).y*(loc-a.heart).y+(loc-a.heart).z*(loc-a.heart).z-a.r*a.r;
float d=b*b-4*c;//判断是否有交点的量
return loc-L*b/2;//R=V-b/2*d即为光线与球的交点。
//交点不在光线上
float t1=(sqrt(b*b-4*c)-b)/2;
float t2=(-b-sqrt(b*b-4*c))/2;
if(t1&0&&t2&0)
//交点不在光线上
{//交点为R(t0)
return loc+L*t1;
return loc+L*t2;
if(t1&=0&&t2&=0)
int t0=t1&t2?t2:t1;
return loc+L*t0;
//求交点的单位法矢量
vector normals(sphere a,light v)
vector S=intersection(a,v);
return (S-a.heart)/a.r;
vector normals(sphere a,vector loc,vector L)
vector S=intersection(a,loc,L);
return (S-a.heart)/a.r;
vector normals(sphere a,vector cro)
return (cro-a.heart)/a.r;
//求反射光线(单位?)
vector reflection(sphere a,light v)
vector N=normals(a,v);
return N*(N*v.L)*2-v.L;
vector reflection(sphere a,vector loc,vector L)
vector N=normals(a,loc,L);
return N*(N*L)*2-L;
//阴影测试算法(球体a,光源点s,交点c),返回为光纤与某个球体的焦点的个数
float shadow(sphere a,vector s,vector c)
vector l=view(c,s);//c到s的单位方向矢量
float b=l*(s-a.heart)*2;
float e=(s-a.heart).x*(s-a.heart).x+(s-a.heart).y*(s-a.heart).y+(s-a.heart).z*(s-a.heart).z-a.r*a.r;
float d=b*b-4*e;//判断是否在阴影范围内的量
float t1=(sqrt(b*b-4*e)-b)/2;
float t2=(-b-sqrt(b*b-4*e))/2;
if(t1&0&&t2&0)
return 0;//交点不在光线上
if(t1&0||t2&0)
//在存在两个交点的情况下选取较为靠前且不同于视点的交点的函数
vector choose(vector c1,vector c2,vector v)
if(c1.x!=v.x&&c2.x!=v.x)//两个交点均不同于视点
if(c1.z&c2.z)
return c1;
return c2;
if(c1.x!=v.x)//如果c1点不同
return c1;
return c2;
//在已知一个交点的情况下判断其所在的球体
sphere choosesph(vector cro,sphere s1,sphere s2)
if((cro-s1.heart)*(cro-s1.heart)==(s1.r*s1.r))
return s1;
return s2;
//////////////////////////////////////////////////////////////////////////////
vector viewpoint(0,0,100);vector lightsource(-300,200,80);//视点、光源坐标位置
color backcolor(0,0,0);//定义背景色
color acolor(0.5,0.5,0.5);//定义环境光色
color pcolor(1,1,1);//定义光源的光色
//定义两个球体
sphere Sphere1(-10,10,-50,20,0.2,0.6,0.1,0.3,0.2,2);
sphere Sphere2(100,100,-100,40,0.7,0.7,0.3,0.5,0.1,4);
int depth=5;//追踪深度
///////////////////////////////////////////////////////////////////////////////
//光线追踪算法,
color TraceLight(vector start,vector L,int depth,color &c)
vector R,N,Rs,Ls;
vector cross1=intersection(Sphere1,start,L);
vector cross2=intersection(Sphere2,start,L);
if((cross1.x==cross2.x==start.x)&&(cross1.y==cross2.y==start.y)&&(cross1.z==cross2.z==start.z))//如果没有符合要求的交点
else//开始追踪
cross1=choose(cross1,cross2,start);//选择一个点
sphere Sphere3=choosesph(cross1,Sphere1,Sphere2);//选择该点所在的球体
//阴影判断//在阴影范围内则该点的当前光强为0;
//若不在阴影范围内则采用phone模型得到光源在该点产生的局部光颜色
int cronum=shadow(Sphere1,lightsource,cross1)+shadow(Sphere2,lightsource,cross1);//交点总数
if(cronum&1)
{c.R=0;c.G=0;c.B=0;}
vector kd(Sphere3.Kdr,Sphere3.Kdg,Sphere3.Kdb);
N=normals(Sphere3,cross1);
Ls=view(lightsource,cross1);
Rs=reflection(Sphere3,lightsource,Ls);
c=acolor*Sphere3.Ka+pcolor*kd*(N*Ls)+pcolor*(powf((Rs*N),Sphere3.light_n))*Sphere3.Ks;
//如果层数大于1则追踪求反射光线并追踪
if(depth&1)
R=reflection(Sphere3,start,L);
cr=TraceLight(cross1+vector(1,1,1),R,depth-1,cr);
}//追踪结束
void myInit()
glClearColor(0,0,0,0);//设置背景颜色为黑色;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-400,400,-300,300,400,-400);//坐标设置
//显示函数
void myDisplay()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
for(float i=-199;i&200;i=i+1)
for(float j=-199;j&200;j=j+1)
vector screen=vector(i,j,0);
vector vL=view(viewpoint,screen);
color cs=TraceLight(viewpoint,vL,depth,cs);
glColor3f(cs.R,cs.G,cs.B);
glVertex3f(screen.x,screen.y,screen.z);
glFlush();
int main(int argc,char* argv[])
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutCreateWindow("123");
glutDisplayFunc(myDisplay);
glutMainLoop();
}------解决方案--------------------楼主可以到我的blog去看看一篇管线追踪的文章
如果您想提高自己的技术水平,欢迎加入本站官方1号QQ群:&&,&&2号QQ群:,在群里结识技术精英和交流技术^_^
本站联系邮箱:}

我要回帖

更多关于 horizontalscrollview 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信