您的位置:首页 > 其它

自定义HorizontalScrollView之侧滑

2015-12-05 01:14 190 查看
xml

<com.example.uimyviews.MyHorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scrollbars="none"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#333"
android:orientation="horizontal" >

<include  layout="@layout/left"></include>

<include layout="@layout/right"></include>

</LinearLayout>

</com.example.uimyviews.MyHorizontalScrollView>


MyHorizontalScrollView

package com.example.uimyviews;

import com.nineoldandroids.view.ViewHelper;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;

public class MyHorizontalScrollView extends HorizontalScrollView {

int screeWidth = -1;// 屏幕宽度
int menuWidth = -1;// 左边的宽度(菜单)
boolean isTouchBtn;// 判断是移动到左边还是右边
int halfMenuWidth;// 左边的一般宽
ViewGroup menu;
ViewGroup content;

public MyHorizontalScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
screeWidth = context.getResources().getDisplayMetrics().widthPixels;
}

// onMeasure():计算和设置布局的宽和高
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// viewGroup才有getChildAt(),view没有这个方法
ViewGroup child = (ViewGroup) getChildAt(0);
menu = (ViewGroup) child.getChildAt(0);
content = (ViewGroup) child.getChildAt(1);
menuWidth = screeWidth - 100;
menu.getLayoutParams().width = menuWidth;
content.getLayoutParams().width = screeWidth;// 设置布局的width为屏幕宽度
halfMenuWidth = (screeWidth - 100) / 2;

Button btn_toggle = (Button) content.getChildAt(0);
btn_toggle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (!isTouchBtn) {
moveToLeft();
} else {
moveToRight();
}
}
});
}

// onLayout:进行布局排版
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
// 隐藏左边的布局,移动到左边布局的宽度处
this.scrollTo(menuWidth, 0);// 核心方法
}

@Override
public boolean onTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_UP:
if (getScrollX() >= 0 && getScrollX() < halfMenuWidth) {
moveToLeft();
} else {
moveToRight();
}
break;
default:
break;
}
return super.onTouchEvent(e);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);

float scale = l * 1.0f / menuWidth;// 缩放的范围(1到0)
float leftScale = 0.7f + 0.3f * (1.0f - scale);// (0.7,1)
float rightScale = 0.8f + 0.2f * scale;// (0.8,1)

// 使用ViewHelper需要导入niceoldandroids.jar
// 缩放
ViewHelper.setScaleX(menu, leftScale);
ViewHelper.setScaleY(menu, leftScale);
ViewHelper.setScaleX(content, rightScale);
ViewHelper.setScaleY(content, rightScale);
// 透明度
float alpha = l * 1.0f / menuWidth;// 缩放的范围(1到0)
float leftAlpha = 0.3f + 0.7f * (1.0f - alpha);// (0.7,1)
float rightAlpha = 0.8f + 0.2f * alpha;// (0.8,1)
ViewHelper.setAlpha(menu, leftAlpha);
ViewHelper.setAlpha(content, rightAlpha);
// 向右边移动多少位置(让页面不被挤出)
ViewHelper.setTranslationX(menu, getScrollX()*leftScale);//这个地方有瑕疵,因为缩放了menu的X,没找到正确的匹配方式。
//如果没有缩放x,直接ViewHelper.setTranslationX(menu, getScrollX())完美适配;
}

private void moveToLeft() {
scrollTo(0, 0);// 移动到左边
isTouchBtn = true;
}

private void moveToRight() {
scrollTo(menuWidth, 0);// 移动到右边
isTouchBtn = false;
}

}


注意:使用ViewHelper要导入oldniceandroids.jar

效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: