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);

    }
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容