先上效果图:
public class CVScrollLayout extends ViewGroup {
private static final String TAG = "ScrollLayout";
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mCurScreen;
private int mDefaultScreen = 0;
private static final int TOUCH_STATE_REST = 0;
private static final int TOUCH_STATE_SCROLLING = 1;
private static final int SNAP_VELOCITY = 600;
private int mTouchState = TOUCH_STATE_REST;
private int mTouchSlop;
private float mLastMotionX;
private float mLastMotionY;
public CVScrollLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public CVScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
mScroller = new Scroller(context);
mCurScreen = mDefaultScreen;
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
if (changed) {
int childLeft = 0;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
childView.layout(childLeft, 0, childLeft + childWidth,
childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only canmCurScreen run at EXACTLY mode!");
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only can run at EXACTLY mode!");
}
// The children are given the same width and height as the scrollLayout
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
scrollTo(mCurScreen * width, 0);
}
/**
* According to the position of current layout scroll to the destination
* page.
*/
public void snapToDestination() {
final int screenWidth = getWidth();
final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
snapToScreen(destScreen);
}
public void snapToScreen(int whichScreen) {
// get the valid layout page
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
if (getScrollX() != (whichScreen * getWidth())) {
final int delta = whichScreen * getWidth() - getScrollX();
mScroller.startScroll(getScrollX(), 0, delta, 0,
Math.abs(delta) * 2);
mCurScreen = whichScreen;
onScreenChangeListener.onScreenChange(mCurScreen);
invalidate(); // Redraw the layout
}
}
public void setToScreen(int whichScreen) {
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
mCurScreen = whichScreen;
scrollTo(whichScreen * getWidth(), 0);
}
public int getCurScreen() {
return mCurScreen;
}
@Override
public void computeScroll() {
// TODO Auto-generated method stub
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
mLastMotionY = y;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int) (mLastMotionX - x);
int deltaY = (int)(mLastMotionY - y);
mLastMotionX = x;
mLastMotionY = y;
if(Math.abs(deltaX) > Math.abs(deltaY)){
scrollBy(deltaX, 0);
}
break;
case MotionEvent.ACTION_UP:
// if (mTouchState == TOUCH_STATE_SCROLLING) {
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int velocityX = (int) velocityTracker.getXVelocity();
if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
// onScreenChangeListener.onScreenChange(mCurScreen - 1);
snapToScreen(mCurScreen - 1);
} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - 1) {
// Fling enough to move right
// onScreenChangeListener.onScreenChange(mCurScreen + 1);
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
// }
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
mTouchState = TOUCH_STATE_REST;
break;
}
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE)
&& (mTouchState != TOUCH_STATE_REST)) {
return true;
}
final float x = ev.getX();
final float y = ev.getY();
switch (action) {
case MotionEvent.ACTION_MOVE:
int deltaX = (int)(mLastMotionX - x);
int deltaY = (int)(mLastMotionY - y);
final int xDiff = (int) Math.abs(mLastMotionX - x);
if (xDiff > mTouchSlop && Math.abs(deltaX) > Math.abs(deltaY)) {
mTouchState = TOUCH_STATE_SCROLLING;
}else {
mTouchState = TOUCH_STATE_REST;
}
break;
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mLastMotionY = y;
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
: TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mTouchState = TOUCH_STATE_REST;
break;
}
return mTouchState != TOUCH_STATE_REST;
}
// 监听页面变动
public interface OnScreenChangeListener {
void onScreenChange(int currentIndex);
}
private OnScreenChangeListener onScreenChangeListener;
public void setOnScreenChangeListener(
OnScreenChangeListener onScreenChangeListener) {
this.onScreenChangeListener = onScreenChangeListener;
}
}
public class CVPageControlView extends LinearLayout {
private Context context;
private int count;
public CVPageControlView(Context context) {
super(context);
this.context = context;
}
public CVPageControlView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void bindScrollLayout(CVScrollLayout scrollLayout) {
this.count = scrollLayout.getChildCount();
// 初始化第一屏
generatePageControl(0);
scrollLayout.setOnScreenChangeListener(new OnScreenChangeListener() {
@Override
public void onScreenChange(int currentIndex) {
generatePageControl(currentIndex);
}
});
}
public void generatePageControl(int currentIndex) {
this.removeAllViews();
for (int i = 0; i < this.count; i++) {
ImageView imageView = new ImageView(context);
if (i == currentIndex) {
imageView.setImageResource(R.drawable.page_indicator_focused);
} else {
imageView.setImageResource(R.drawable.page_indicator);
}
this.addView(imageView);
}
}
}
布局文件:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.zoeice.scrolllayout.CVScrollLayout
android:id="@+id/scrollControl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe00000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00e000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000e0" />
</com.zoeice.scrolllayout.CVScrollLayout>
<com.zoeice.scrolllayout.CVPageControlView
android:id="@+id/pageControl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dip" >
</com.zoeice.scrolllayout.CVPageControlView>
</RelativeLayout>
调用方法:
CVScrollLayout sl = (CVScrollLayout) findViewById(R.id.scrollControl);
CVPageControlView pageControlView = (CVPageControlView) findViewById(R.id.pageControl);
pageControlView.bindScrollLayout(sl);//绑定控件
sl.setToScreen(0);//默认第一页
pageControlView.generatePageControl(0);//默认第一个点
分享到:
相关推荐
最近调查了一个仿launch的横向滑动屏幕的DEMO,在网上看到一个类,并加以修改分享如下,最后附有完整打包文件和截图: 本例中实现了动态添加的功能,每一屏显示9个TEXTview,点击上方add按钮会添加一个textview,当...
和Android的launch有点相同,界面挺炫的
android程序,滑动屏幕切换页面,anim文件夹里有进、出的移动、缩放、旋转等多种切换动画效果文件。
Android应用源码之BackgroundViewPager实现桌面launch移动.rar
您是否在网上找了好多资料都无法解决这个问题?launch failed binary not found,那小弟不才,愿以独家秘诀,(图文)手把手教你解决这个问题,绝不坑爹,欢迎下载!
LaunchBar Commander【苹果Mac界面仿真软件】 v1.109.01 LaunchBar Commander【苹果Mac界面仿真软件】的简介: LaunchBar Commander v1.109.01是一款可以模仿苹果mac界面的仿真软件,用户安装本软件后即可正常启动...
Free Launch Bar 是一款免费代替标准Quick Launch bar的软件,现在Free Launch Bar拥有Quick Launch bar的全部功能,然而Free Launch Bar完全兼容快速启动栏,因为它使用同样的文件夹和快捷方式。 但是有一个巨大的...
LaunchDemo
前几年的Android应用源码Demo,主要面向的是学生毕业设计学习。
压缩包涵盖 sbt-launch-0.11.0,sbt-launch-0.11.2,sbt-launch-0.11.3,sbt-launch-0.13.9版本
十张图片,十步教会你使用launch4j,简单易学,一看就懂
桌面上有太多的快捷图标,看上去杂乱无章,想找一个软件得费很大的劲,launch就是用来管理这些快捷方式的,可以对快捷方式进行分类,launch纯绿色软件,不用在系统中留下任何垃圾,占用资源少,启动迅速。
真机运行,iOS 自动生成各种尺寸的App Icon 和 Launch Image,生成后保存在相册中
宏基LaunchManager_Dritek
Launch4j_3.9 老朽痴拙汉化版, 多平台Java打包, JAR生成exe
launch4j是Java应用程序的Windows本地可执行文件 (.exe) 封装器。Launch4j是一个开源的安装程序,将您的jar文件打包成操作系统的本地可执行文件,支持Windows, MacOS,Linux和Solaris。对于JRE,您可以嵌入在安装文件...
True Launch Bar的缩写,True Launch Bar是一款功能强悍的快速启动栏工具栏软件,完全可以取代Windows传统的“快速启动”工具栏。它和普通的“快速启动”工具栏采用相同的工作原理,启动迅速;不同之处在于,True ...
ROS launch文件编写说明
非常好用的桌面工具,是true launch bar的简化版因为是免费的
Free Launch Bar INSTALLATION NOTES If you are using 32-bit Windows run freelaunchbar32.exe to install Free Launch Bar. freelaunchbar64.msi is for 64-bit Windows http://www.freelaunchbar.com