這是慕課網鴻洋的視屏demo的實現,以前QQ版本的側滑,現在的版本相對簡單了。涉及到的內容:自定義ViewGroup、自定義屬性
自定義View屬性
- 自定義View屬性的xml文件
- 在布局文件中使用自定義的屬性
- 在View的構造方法中獲得我們的自定義屬性
1.創建屬性內容
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--創建屬性-->
<attr name="rightPadding" format="dimension"></attr>
<!--為SlidingMenu使用屬性-->
<declare-styleable name="SlidingMenu">
<attr name="rightPadding"></attr>
</declare-styleable>
</resources>
2.在布局文件中使用自定義的屬性
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:zwf = "http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--定義一個命名空間-->
<com.zwf.qqslidingmenu.CustomView.SlidingMenu
android:id="@+id/id_sliding_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img_frame_background"
zwf:rightPadding="100dp">
<!--使用自定義的屬性-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include layout="@layout/left_menu"></include>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/qq">
<Button
android:id="@+id/id_open_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="open"
android:textAllCaps="false"/>
</LinearLayout>
</LinearLayout>
</com.zwf.qqslidingmenu.CustomView.SlidingMenu>
</RelativeLayout>
這里需要注意的是需要引入命名空間:
xmlns:zwf = "http://schemas.android.com/apk/res-auto"
3.在構造函數中獲取到我們定義的屬性
自定義View的時候我們需要重寫構造函數,一般View的構造函數有三個。
一個參數:SlidingMenu(Context context)
這個構造函數是在我們手動在代碼中new一個SlidingMenu的時候使用
兩個參數:SlidingMenu(Context context, AttributeSet attrs)
這個構造函數是當我們在xml布局文件中使用SlidingMenu的時候調用
三個參數:SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr)
在這個構造函數中我們主要用來獲取我們自定義的屬性
當我們使用了自定義屬性的時候,還是會調用兩個參數的構造函數!然后在兩個參數的構造函數中我們手動調用三個參數的構造函數。
下面看一下這幾個構造函數的實現:
public SlidingMenu(Context context) {
this(context, null, 0);
}
public SlidingMenu(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
/**
* 一般都是在這個方法中做一些屬性初始化的操作。
* 可以把獲取屏幕尺寸、dp與px轉化的代碼單獨提出來做一個工具使用
* @param context
* @param attrs
* @param defStyleAttr
*/
public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//通過WindowManager獲取到屏幕的尺寸
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
mScreenWidth = outMetrics.widthPixels;
//把dp轉化為px
mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context
.getResources()
.getDisplayMetrics());
//獲取自定義的屬性
/* TypedArray這個類用完了一定要釋放(recycle())一下
第一個參數
第二個參數:剛定義的屬性文件
*/
TypedArray attrArr = context.getTheme().obtainStyledAttributes(attrs, R.styleable
.SlidingMenu,defStyleAttr,0);
int n = attrArr.getIndexCount();
for (int i=0; i<n; i++){
int attr = attrArr.getIndex(i);
switch (attr){
case R.styleable.SlidingMenu_rightPadding:
//獲取到自定義屬性,如果沒有值默認給一個50
mMenuRightPadding = attrArr.getDimensionPixelSize(attr,(int) TypedValue
.applyDimension
(TypedValue.COMPLEX_UNIT_DIP, 50, context
.getResources()
.getDisplayMetrics()));
break;
}
}
attrArr.recycle();
}
關于獲取自定義屬性的部分也在上面了,注釋也很詳細。
4.然后就是onMeasure方法的實現,主要是獲取到那些視圖內容控件,并設置尺寸
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (!isOnce){
mWapper = (LinearLayout) getChildAt(0);
mMenu = (ViewGroup) mWapper.getChildAt(0);
mContent = (ViewGroup) mWapper.getChildAt(1);
mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
mContent.getLayoutParams().width = mScreenWidth;
isOnce = true;
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
然后其他就是一些滑動邏輯上的處理了。這里就不記錄了。主要是為自定義View做一個筆記。
demo下載