<span style="font-size:18px;">不能仅仅会用框架,了解原理才能更快的成长</span>
<span style="font-size:14px;"> 咋们就是要任性一点,自己去做一些效果</span>
<span style="font-size:14px;">
</span>
可以总结为 一:测量 二:摆放 三:事件
<span style="font-size:14px;">public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//FEATURE_NO_TITLE
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}</span>
<span style="font-size:14px;">
</span>
<span style="font-size:14px;">xml :</span><pre name="code" class="java"><span style="font-size:14px;"> </span><pre name="code" class="html"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<www.guge.com.MySlidingMenu
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
layout="@layout/menu"/>
<include
layout="@layout/main"/>
</www.guge.com.MySlidingMenu>
</RelativeLayout>
activity_main
<span style="font-size:14px;">
</span>
<span style="font-size:14px;">
</span>
public class MySlidingMenu extends ViewGroup {
private int meWidth;
private ImageButton bt;
private Scroller scroller;
// 报错的错误为inflating class www.guge.com.my.slidingMenu
// 这些构造方法至少是两到三个否则报错
public MySlidingMenu(Context context) {
super(context);
}
public MySlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
scroller = new Scroller(context);
}
public MySlidingMenu(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 获取子控件 再进行测量
View menu = getChildAt(0);
View main = getChildAt(1);
meWidth = menu.getLayoutParams().width;
menu.measure(meWidth, heightMeasureSpec);
main.measure(widthMeasureSpec, heightMeasureSpec);
}
// 控制子控件的摆放
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
View menu = getChildAt(0);
View main = getChildAt(1);
// layout
menu.layout(-meWidth, t, 0, b);
main.layout(l, t, r, b);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
bt = (ImageButton) findViewById(R.id.ib_toggle);
}
int startX = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startX = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
int moveX = (int) event.getX();
int disX = moveX - startX;
int scrollX = (int) getScaleX();
// 从左滑不能超过(menuWidth) ,,从右滑不能超过0
if (scrollX - disX < -meWidth || scrollX - disX > 0) {
} else {
scrollBy(-disX, 0);
}
startX = moveX;
break;
case MotionEvent.ACTION_UP:
int x = (int) getScaleX();
if (Math.abs(x) > meWidth / 2) {
}
break;
default:
break;
}
return true;
}
// 这里可以设置滑动的效果啊,比如QQ的滑动特效
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
// TODO Auto-generated method stub
super.onScrollChanged(l, t, oldl, oldt);
}
}