public class MainUI extends FrameLayout{
private Context context;
private LinearLayout layout;
private LinearLayout backLayout;
private LinearLayout leftMenu;
private LinearLayout rightMenu;
private Scroller mScroller;
private int mScreenWidth;
public MainUI(Context context) {
super(context);
initView(context);
}
public MainUI(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(Context context){
this.context=context;
this.mScroller=new Scroller(context,new DecelerateInterpolator());
layout=new LinearLayout(context);
layout.setBackgroundColor(Color.BLUE);
leftMenu=new LinearLayout(context);
// leftMenu= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.left_menu_item,null);
leftMenu.setBackgroundColor(Color.YELLOW);
rightMenu=new LinearLayout(context);
rightMenu.setBackgroundColor(Color.GREEN);
backLayout=new LinearLayout(context);
backLayout.setBackgroundColor(0x88000000);
addView(leftMenu);
addView(layout);
addView(rightMenu);
addView(backLayout);
layout.setScaleX(1);
layout.setScaleY(1);
backLayout. setAlpha(0);
backLayout.setScaleX(1);
backLayout.setScaleY(1);
WindowManager manager= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics=new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
mScreenWidth=metrics.widthPixels;
}
@Override
public void scrollTo(int x, int y) {
super.scrollTo(x, y);
float curX= Math.abs(getScrollX());
float scale=curX /leftMenu.getMeasuredWidth();
backLayout.setAlpha(scale);
float xxx=(float)(leftMenu.getMeasuredWidth()-Math.sqrt(Math.abs(x)*2))/leftMenu.getMeasuredWidth();
layout.setScaleX(xxx);
layout.setScaleY(xxx);
backLayout.setScaleX(xxx);
backLayout.setScaleY(xxx);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
this.layout.measure(widthMeasureSpec, heightMeasureSpec);
this.backLayout.measure(widthMeasureSpec, heightMeasureSpec);
/**实际宽度*/
int realWidth=MeasureSpec.getSize(widthMeasureSpec);
//AT_MOST 自适应,EXACTLY精准
int menuWidth=MeasureSpec.makeMeasureSpec((int)(realWidth * 0.8f),MeasureSpec.EXACTLY);
leftMenu.measure(menuWidth, heightMeasureSpec);
rightMenu.measure(menuWidth, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
layout.layout(left, top, right, bottom);
backLayout.layout(left, top, right, bottom);
leftMenu.layout(left - leftMenu.getMeasuredWidth(), top, right, bottom);
rightMenu.layout(left + layout.getMeasuredWidth(),top,left + layout.getMeasuredWidth()+ rightMenu.getMeasuredWidth(),bottom);
}
/**判断是什么事件*/
private boolean isTestCompete;
private boolean isLeftRight;
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if(!isTestCompete){
getEventType(ev);
return true;
}
if(isLeftRight){
switch (ev.getAction()){
case MotionEvent.ACTION_MOVE:
int curScrollX=getScrollX();
int dis_x= (int) (ev.getX()-point.x);
int expectX=-dis_x + curScrollX;
int finalX;
if(expectX<0){//向左
finalX=Math.max(expectX,-leftMenu.getMeasuredWidth());
}else{//向右
finalX=Math.min(expectX, rightMenu.getMeasuredWidth());
}
scrollTo(finalX,0);
point.x= (int) ev.getX();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
int mScraleX=getScrollX();
if(Math.abs(mScraleX) > leftMenu.getMeasuredWidth()/ 3){
if(mScraleX<0){
mScroller.startScroll(mScraleX,0, -leftMenu.getMeasuredWidth()-mScraleX,0);
}else{
mScroller.startScroll(mScraleX,0,leftMenu.getMeasuredWidth()-mScraleX,0);
}
}else{
mScroller.startScroll(mScraleX,0, -mScraleX,0);
}
invalidate();
isLeftRight=false;
isTestCompete=false;
break;
default:
break;
}
}else{
switch (ev.getAction()){
case MotionEvent.ACTION_UP:
isLeftRight=false;
isTestCompete=false;
break;
default:
break;
}
}
return super.dispatchTouchEvent(ev);
}
@Override
public void computeScroll() {
super.computeScroll();
if(!mScroller.computeScrollOffset()){
return;
}
scrollTo(mScroller.getCurrX(),0);
}
private Point point=new Point();
private static final int TEST_DIS=20;
private void getEventType(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
point.x= (int) ev.getX();
point.y= (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
int dX= (int) Math.abs(ev.getX()-point.x);
int dY= (int) Math.abs(ev.getY()-point.y);
if(dX >=TEST_DIS && dX>dY){//左右滑动
isLeftRight=true;
isTestCompete=true;
point.x= (int) ev.getX();
point.y= (int) ev.getY();
}else if(dY>TEST_DIS && dY>dX){//上下滑动
isLeftRight=false;
isTestCompete=true;
point.x= (int) ev.getX();
point.y= (int) ev.getY();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
break;
}
}
}
在 initView 方法中 leftMenu= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.left_menu_item,null);
这个View里面,就只有一个LinearLayout,单单设置了一个背景图片
你在scrollTo()方法中调用layout的setScaleX(),这应该会导致变卡。