带有滚动视差的ScrollView
来源:互联网 发布:程序员 欣欣 编辑:程序博客网 时间:2024/05/16 02:04
最近看了滚动视差的例子,写了个小demo,不能调节参数,但能够随着ScrollView的滑动而滑动。在这里要强调一下滚动视差的意思,就是背景层的滑动速度没有ScrollView的滑动速度快。通过这个小例子,我对ScrollView的理解更深了。不多说,上代码。
首先是自己定义的ScrollView:
package com.example.parallxscrollview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;
public class MyScrollView extends ScrollView{
private ScrollChangeListener scrollChangeListener;
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context) {
super(context);
}
public void setScrollChangeListener(ScrollChangeListener scrollChangeListener) {
this.scrollChangeListener = scrollChangeListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (null != scrollChangeListener) {
scrollChangeListener.scollChange(getScrollY());
}
}
}
在这里要把ScrollView滑动的距离即getScrollY()传给背景。
接着就是大头了
package com.example.parallxscrollview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View.MeasureSpec;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class MyFrameLayout extends FrameLayout implements ScrollChangeListener{
private ImageView skinImageView;
private MyScrollView myScrollView;
private int contentHeight;
private int minusContentHeight;
private float diff = 0.2f;
public MyFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MyFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public MyFrameLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void setSkin(ImageView skinImageView) {
this.skinImageView = skinImageView;
}
public void setScrollView(MyScrollView myScrollView) {
this.myScrollView = myScrollView;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//取得肉容的宽度
contentHeight = myScrollView.getChildAt(0).getMeasuredHeight();
minusContentHeight = (int) (myScrollView.getMeasuredHeight() + (contentHeight - myScrollView.getMeasuredHeight()) *diff);
measureChild(skinImageView, MeasureSpec.makeMeasureSpec(
skinImageView.getMeasuredWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(minusContentHeight, MeasureSpec.EXACTLY));
}
@Override
public void scollChange(int scrollY) {
// TODO Auto-generated method stub
skinImageView.layout(getLeft(), (int)(-scrollY * diff), getRight(), (int)(-scrollY * diff) + minusContentHeight);
}
}
interface ScrollChangeListener {
public void scollChange(int scrollY);
}
在这里重点是measureChild()函数,按照我的理解,它是再一次测量child的宽、高,再layout。
接着是Activity:
package com.example.parallxscrollview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView skinImageView;
private MyFrameLayout myFrameLayout;
private MyScrollView myScrollView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
skinImageView = (ImageView) findViewById(R.id.skin);
myFrameLayout = (MyFrameLayout) findViewById(R.id.myFrameLayout);
myScrollView = (MyScrollView) findViewById(R.id.myScrollView);
myFrameLayout.setSkin(skinImageView);
myScrollView.setScrollChangeListener(myFrameLayout);
myFrameLayout.setScrollView(myScrollView);
}
@Override
protected void onResume() {
super.onResume();
}
}
再来布局:
<com.example.parallxscrollview.MyFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myFrameLayout">
<ImageView
android:id="@+id/skin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_sky" />
<com.example.parallxscrollview.MyScrollView
android:id="@+id/myScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"/>
</LinearLayout>
</com.example.parallxscrollview.MyScrollView>
</com.example.parallxscrollview.MyFrameLayout>
再来上传我的图片:
- 带有滚动视差的ScrollView
- 带有滚动监听的ScrollView
- 14个超棒的带有故事趣味性视差滚动网站
- 有视差的滚动视图-Parallax ScrollView In Swift
- iOS - scrollView与headerView的视差滚动实现思路 scrollView 可接收通知啊
- 视差滚动的背后
- 图片滑块 Sequence.js 实现带有视差滚动特效的图片滑块
- ParallaxScrollView有视差的ScrollView
- 视差滚动的那些事儿
- UIScrollView的视差滚动实现
- 简单的上下视差滚动
- 视差滚动
- 视差滚动
- jquery实现的视差滚动教程(视差大背景效果)
- 15个最好的视差滚动教程
- 11个超炫的视差滚动网站欣赏
- CSS3滚动视差效果的制作技巧
- 页面滚动视差效果的实现
- linux安装文件,查找安装文件位置
- gradle异常(1)-EOF初识
- 《我的twitter技术面试失败》中墙之间水坑装水问题的更烂解法(Java解)
- jqurymobile跳转页面的几种方式
- 【Java笔记】类的构造器究竟做了什么?
- 带有滚动视差的ScrollView
- OJ题——指针、引用法对三个数按从小到大排序
- wordpress去掉回复标签属性 那几行文字
- 去掉 功能里的 wordpress.org
- WordPress加载页面慢解决方案
- android 广播接收者--BroadcastReceiver
- cocos2d-x 2.2.3 sprite使用scale以后获取宽高
- Popush迭代2总结
- 国家对五险一金的交纳说明