安卓侧边栏实现
上一篇写的是侧滑布局,虽然有侧滑效果,但是没有阴影,效果不是很好
这一次我们来为布局加上阴影
package xiaolin.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
/**
* 标准侧边栏
* Created by XiaoLin on 2016/1/14.
*/
public class ASlidingLayout extends ATranslationLayout {
public static final String TAG = "ASlidingLayer";
public ASlidingLayout(Context context) {
super(context);
}
public ASlidingLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ASlidingLayout(Context context, AttributeSet attrs, int theme) {
super(context, attrs, theme);
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
| Paint.FILTER_BITMAP_FLAG));// 画布抗锯齿
int x = getScrollX();
if (x == 0) {
super.dispatchDraw(canvas);
return;
}
int alpha = 150 * -x / menuWidth;
canvas.save();
canvas.translate(x, 0);
super.dispatchDraw(canvas);
canvas.drawColor((Color.BLACK & 0xffffff) + alpha * 0x1000000);
canvas.restore();
canvas.save();
canvas.clipRect(x, 0, 0, this.getHeight());
super.dispatchDraw(canvas);
canvas.restore();
}
}
ATranslationLayout请擢这里
用法还是和侧滑布局一样,在里面添加一个或两个View就行了
<?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:background="#cacaca">
<xiaolin.widget.ASlidingLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/bg" />
</xiaolin.widget.ASlidingLayout>
</LinearLayout>