Android 自定义控件基础-QQ侧滑菜单

  QQ侧滑菜单的实现方式有很多种,而我只学了一种。。。那就是通过继承HorizontalScrollView来实现的。
  HorizontalScrollView是ViewGroup,而ViewGroup的重写不像普通的View控件那样,而是要考虑到其他的。

  1.onMeasure--决定内部的View(子View)的宽和高,以及自己的宽和高
  2.onLayout--决定子View放置的位置

1.布局文件

  布局文件主要包含两部分,一是Menu的布局,也就是qq侧滑的侧滑菜单,二是content布局,也就是内容布局。

  Menu的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:background="@drawable/img_frame_background"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        <ImageView
            android:src="@drawable/img_1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第一个Item"
            android:textSize="16sp"
            android:textStyle="bold"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        <ImageView
            android:src="@drawable/img_2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第二个Item"
            android:textSize="16sp"
            android:textStyle="bold"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        <ImageView
            android:src="@drawable/img_3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第三个Item"
            android:textSize="16sp"
            android:textStyle="bold"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        >
        <ImageView
            android:src="@drawable/img_4"
            android:layout_width="50dp"
            android:layout_height="50dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="第四个Item"
            android:textSize="16sp"
            android:textStyle="bold"
            />
    </LinearLayout>

</LinearLayout>
  Content布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.android_slidmenu.SlidMenu
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="none"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            >

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

            <LinearLayout
                android:background="@drawable"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></LinearLayout>
        </LinearLayout>
    </com.example.android_slidmenu.SlidMenu>
</RelativeLayout>

  注意一下,这里如果使用HorizontalScrollView做为ViewGroup,发现子空间根本无法充满HorizontalScrollView,如果设置fillViewPost的话,虽然充满了布局,但是无法滑动。这时候的解决方式,将图片资源移动至低分辨的drawable文件夹下面,就行了。

2.实现自定义的HorizontalScrollView

  

private LinearLayout mLayout = null;


    private ViewGroup mMenu = null;
    private ViewGroup mContent = null;
    private int mMenuWidth = 0;


    private int mScreenWidth = 0;

    private int mMenuMarginRight = 0;

    private boolean mOnce = false;

    public SlidMenu(Context context, AttributeSet attrs) {
        super(context, attrs);

        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(displayMetrics);
        mScreenWidth = displayMetrics.widthPixels;

        mMenuMarginRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, context.getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if(!mOnce)
        {
            mLayout = (LinearLayout) getChildAt(0);
            mMenu = (ViewGroup) mLayout.getChildAt(0);
            mContent = (ViewGroup) mLayout.getChildAt(1);

            mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuMarginRight;
            mContent.getLayoutParams().width = mScreenWidth;
            mOnce = true;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(changed)
        {
            this.scrollTo(mMenuWidth, 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_UP)
        {

            if(getScrollX() >= mMenuWidth / 2)
            {
                Log.i("main", "我进来没1");
                this.smoothScrollTo(mMenuWidth, 0);
            }
            else
            {
                Log.i("main", "我进来没2");
                this.smoothScrollTo(0,0);
            }
            return true;
        }
        return super.onTouchEvent(ev);
    }

3.使用自定义的属性

  之前,我们在xml中定义控件时,设置了很多的属性,而这些的属性都是系统给我们的,并不是我们自定义的属性,如果我们想要自定义属性,要经历一下几个步骤:

  A.在values文件夹中新建一个名叫attr的xml文件,并且在xml文件中定义我们想要的属性
  B.在布局文件中声明我们定义的属性的命名空间。
  C.在布局文件中文件引用我们的属性
  D.在自定义的View的构造方法中获得该属性的值(通常自定义控件时,叫我们实现这个View的构造方法,我们会发现通常会有三个构造方法,而这三个构造方法的调用时间是不同的:一个参数的构造方法通常是该控件被new创建一个对象而调用,两个参数的构造方法是在未使用自定义的属性时调用,三个参数的构造方法是在使用自定义的属性时调用)。而这里想用使用自定义的属性,因此必须使用三个参数的构造方法,通常我们使用的方式:用一个参数的构造方法来调用两个参数的构造方法,两个参数的构造方法来调用三个参数的构造方法。

  例如:这里定义一个属性,用来控制Menu对右边的Padding

(1).在attr文件中这样定义

 <attr name="CustomViewRightPadding" format="dimension">
    <declare-styleable name="SlidMenu">
      <attr name="CustomViewRightPadding"></attr>
   </declare-styleable>
</attr>

(2).声明我们的命名空间

  注意:eclipse和Android studio的声明方式是不一样的,eclipse中:xmlns:pby="http://schemas.android.com/apk/包名",Android studio中:xmlns:pby="http://schemas.android.com/apk/res-auto"。其中pby是我们的空间名,相当于是系统属性前的android一样。
  在三个参数的构造方法中使用,使用方式如下面代码:

public SlidMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlidMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(displayMetrics);
        mScreenWidth = displayMetrics.widthPixels;
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SlidMenu, defStyleAttr, 0);
        int n = array.getIndexCount();
        for (int i = 0; i < n; i++) {
            int index = array.getIndex(i);
            switch (index) {
                case R.styleable.SlidMenu_CustomViewRightPadding: {
                    mMenuMarginRight = array.getDimensionPixelSize
                            (R.styleable.SlidMenu_CustomViewRightPadding,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics()));
                    break;
                }
            }
        }
    }

  从上面看到,我们是从通过context.obtainStyledAttributes这个方法来获得属性。通常还可以使用context.getTheme().obtainStyledAttributes来获得属性。

(4).定义成抽屉式菜单

    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float scale = l * 1.0f/ mMenuWidth;
        mMenu.setTranslationX( mMenuWidth * scale);
    }

  在以前的代码中重写上面的方法就行了。注意:这里scale是一个渐变量,来控制mMenu的位置

(5).增加抽屉式菜单的动画

  在这里增加菜单的缩放和透明度的动画,同时还增加Content的缩放动画

@Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        float scale = l * 1.0f/ mMenuWidth;
        mMenu.setTranslationX( mMenuWidth * scale);
        /**
         * Menu的透明度 -- 0.7~1    1 - 0.3 * scale
         * Menu的缩放 -- 0.8~1     1 - 0.2 * scale
         * Content的缩放 -- 1~0.8   0.8 + 0.2 * scale
         */
        mMenu.setAlpha(1 - 0.3f * scale);
        mMenu.setScaleX(1 - 0.2f * scale);
        mMenu.setScaleY(1 - 0.2f * scale);
        mContent.setPivotX(0);
        mContent.setPivotY(mContent.getHeight() / 2);
        mContent.setScaleX(0.8f + 0.2f * scale);
        mContent.setScaleY(0.8f + 0.2f * scale);

    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容