我正在尝试使用没有操作栏的抽屉布局来实现导航抽屉。一切都很顺利,直到我发现抽屉图像不像 YouTube 或 Google Plus 应用程序那样动画/滑动。
我对抽屉图像的意思
然后我尝试实现我的自定义 DrawerListener。这是我的活动课。
lstMenuDrawer.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
selectMenu(pos);
}
});
drawerToggle = new GRHDrawerToggle(MainActivity.this, imvDrawer.getDrawable());
drawerLayout.setDrawerListener(drawerToggle);
这是我的听众课
public class GRHDrawerToggle implements DrawerListener {
private Activity mActivity;
private SlideDrawable mSlider;
/** Fraction of its total width by which to offset the toggle drawable. */
private static final float TOGGLE_DRAWABLE_OFFSET = 1 / 3f;
public GRHDrawerToggle(Activity activity, Drawable drawerImage){
mActivity = activity;
mSlider = new SlideDrawable(drawerImage);
mSlider.setOffset(TOGGLE_DRAWABLE_OFFSET);
}
@Override
public void onDrawerClosed(View arg0) {
// TODO Auto-generated method stub
mSlider.setPosition(0);
}
@Override
public void onDrawerOpened(View arg0) {
// TODO Auto-generated method stub
mSlider.setPosition(1);
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
// TODO Auto-generated method stub
float glyphOffset = mSlider.getPosition();
if (slideOffset > 0.5f) {
glyphOffset = Math.max(glyphOffset, Math.max(0.f, slideOffset - 0.5f) * 2);
} else {
glyphOffset = Math.min(glyphOffset, slideOffset * 2);
}
mSlider.setPosition(glyphOffset);
}
@Override
public void onDrawerStateChanged(int arg0) {
// TODO Auto-generated method stub
}
private class SlideDrawable extends InsetDrawable implements Drawable.Callback {
private final boolean mHasMirroring = Build.VERSION.SDK_INT > 18;
private final Rect mTmpRect = new Rect();
private float mPosition;
private float mOffset;
private SlideDrawable(Drawable wrapped) {
super(wrapped, 0);
}
/**
* Sets the current position along the offset.
*
* @param position a value between 0 and 1
*/
public void setPosition(float position) {
mPosition = position;
invalidateSelf();
}
public float getPosition() {
return mPosition;
}
/**
* Specifies the maximum offset when the position is at 1.
*
* @param offset maximum offset as a fraction of the drawable width,
* positive to shift left or negative to shift right.
* @see #setPosition(float)
*/
public void setOffset(float offset) {
mOffset = offset;
invalidateSelf();
}
@Override
public void draw(Canvas canvas) {
copyBounds(mTmpRect);
canvas.save();
// Layout direction must be obtained from the activity.
final boolean isLayoutRTL = ViewCompat.getLayoutDirection(
mActivity.getWindow().getDecorView()) == ViewCompat.LAYOUT_DIRECTION_RTL;
final int flipRtl = isLayoutRTL ? -1 : 1;
final int width = mTmpRect.width();
canvas.translate(-mOffset * width * mPosition * flipRtl, 0);
// Force auto-mirroring if it's not supported by the platform.
if (isLayoutRTL && !mHasMirroring) {
canvas.translate(width, 0);
canvas.scale(-1, 1);
}
super.draw(canvas);
canvas.restore();
}
}
}
但是我的抽屉按钮中仍然没有滑动动画。然后我意识到我的监听器类中的方法 draw(Canvas canvas) 从未被触发。有人可以告诉我我的错误在哪里吗?
非常感谢。
胶圈