一、效果图
二、修改方法
- 在SlidingMenu添加一个设置透明度方法
public void setOffsetFadeDegree(float f) {
mViewBehind.setOffsetFadeDegree(f);
}
- CustomViewBehind 添加变量和方法
private float mOffsetFadeDegree;
public void setOffsetFadeDegree(float degree) {
if (degree > 1.0f || degree < 0.0f)
throw new IllegalStateException("The offsetFadeDegree must be between 0.0f and 1.0f");
mOffsetFadeDegree = degree;
}
/**
* 绘制剩余View的淡出淡入
* @param content 主界面
* @param canvas 画布
* @param openPercent 打开了的百分比
*/
public void drawOffsetFade(View content, Canvas canvas, float openPercent) {
if (mWidthOffset <= 0 || mOffsetFadeDegree <= 0)
return; //不开启就返回,不设置 淡入淡出效果
final int alpha = (int) (mOffsetFadeDegree * 255 * Math.abs(openPercent));
mFadePaint.setColor(Color.argb(alpha, 0, 0, 0));
canvas.drawRect(content.getLeft(), 0,content.getRight(), getHeight(), mFadePaint);
}
- 最后在CustomViewAbove 的dispatchDraw 绘制view 里面调用
drawOffsetFade
/**
* 绘制ui
* @param canvas
*/
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// Draw the margin drawable if needed.
mViewBehind.drawShadow(mContent, canvas);
mViewBehind.drawFade(mContent, canvas, getPercentOpen());
//绘制剩余View的淡入淡出
mViewBehind.drawOffsetFade(mContent, canvas, getPercentOpen());
mViewBehind.drawSelector(mContent, canvas, getPercentOpen()); //
}
三、使用方法
在初始化menu的时候,调用下面这个方法,设置透明度就行了,一般设置为0.4就行了
mSlidingMenu.setOffsetFadeDegree(0.4f);