自定义HorizontalScrollView之侧滑

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

效果:

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值