如果使用SlidingDrawer可以实现从右边和从底部拉出来的抽屉效果
使用如下:
<SlidingDrawer
android:id="@+id/slidingdrawer_rignt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:content="@+id/SettingContent_rignt"
android:handle="@+id/sliding_switch_rignt"
android:orientation="horizontal" >
<ImageButton
android:id="@id/sliding_switch_rignt"
android:layout_width="50dip"
android:layout_height="44dip"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@id/SettingContent_rignt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="@drawable/pic"/>
</SlidingDrawer>
orientation可以改变方向,但是不能实现从顶部和从左边滑出的效果,而且SlidingDrawer在使用的时候提示 过时
下面是使用自定义控件来实现从四个方向可以抽出的效果,拷贝即可以用:效果图如下:
关键代码如下:
package com.xy.slidingdrawer;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SoundEffectConstants;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
public class MultiDirectionSlidingDrawer extends ViewGroup {
public static final int ORIENTATION_RTL = 0;
public static final int ORIENTATION_BTT = 1;
public static final int ORIENTATION_LTR = 2;
public static final int ORIENTATION_TTB = 3;
private static final int TAP_THRESHOLD = 6;
private static final float MAXIMUM_TAP_VELOCITY = 100.0f;
private static final float MAXIMUM_MINOR_VELOCITY = 150.0f;
private static final float MAXIMUM_MAJOR_VELOCITY = 200.0f;
private static final float MAXIMUM_ACCELERATION = 2000.0f;
private static final int VELOCITY_UNITS = 1000;
private static final int MSG_ANIMATE = 1000;
private static final int ANIMATION_FRAME_DURATION = 1000 / 60;
private static final int EXPANDED_FULL_OPEN = -10001;
private static final int COLLAPSED_FULL_CLOSED = -10002;
private final int mHandleId;
private final int mContentId;
private View mHandle;
private View mContent;
private final Rect mFrame = new Rect();
private final Rect mInvalidate = new Rect();
private boolean mTracking;
private boolean mLocked;
private VelocityTracker mVelocityTracker;
private boolean mInvert;
private boolean mVertical;
private boolean mExpanded;
private int mBottomOffset;
private int mTopOffset;
private int mHandleHeight;
private int mHandleWidth;
private OnDrawerOpenListener mOnDrawerOpenListener;
private OnDrawerCloseListener mOnDrawerCloseListener;
private OnDrawerScrollListener mOnDrawerScrollListener;
private final Handler mHandler = new SlidingHandler();
private float mAnimatedAcceleration;
private float mAnimatedVelocity;
private float mAnimationPosition;
private long mAnimationLastTime;
private long mCurrentAnimationTime;
private int mTouchDelta;
private boolean mAnimating;
private boolean mAllowSingleTap;
private boolean mAnimateOnClick;
private final int mTapThreshold;
private final int mMaximumTapVelocity;
private int mMaximumMinorVelocity;
private int mMaximumMajorVelocity;
private int mMaximumAcceleration;
private final int mVelocityUnits;
/**
* Callback invoked when the drawer is opened.
*/
public static interface OnDrawerOpenListener {
/**
* Invoked when the drawer becomes fully open.
*/
public void onDrawerOpened();
}
/**
* Callback invoked when the drawer is closed.
*/
public static interface OnDrawerCloseListener {
/**
* Invoked when the drawer becomes fully closed.
*/
public void onDrawerClosed();
}
/**
* Callback invoked when the drawer is scrolled.
*/
public static interface OnDrawerScrollListener {
/**
* Invoked when the user starts dragging/flinging the drawer's handle.
*/
public void onScrollStarted();
/**
* Invoked when the user stops dragging/flinging the drawer's handle.
*/
public void onScrollEnded();
}
/**
* Creates a new SlidingDrawer from a specified set of attributes defined in
* XML.
*
* @param context
* The application's environment.
* @param attrs
* The attributes defined in XML.
*/
public MultiDirectionSlidingDrawer( Context context, AttributeSet attrs )
{
this( context, attrs, 0 );
}
/**
* Creates a new SlidingDrawer from a specified set of attributes defined in
* XML.
*
* @param context
* The application's environment.
* @param attrs
* The attributes defined in XML.
* @param defStyle
* The style to apply to this widget.
*/
public MultiDirectionSlidingDrawer( Context context, AttributeSet attrs, int defStyle )
{
super( context, attrs, defStyle );
TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.MultiDirectionSlidingDrawer, defStyle, 0 );
int orientation = a.getInt( R.styleable.MultiDirectionSlidingDrawer_direction, ORIENTATION_BTT );
mVertical = ( orientation == ORIENTATION_BTT || orientation == ORIENTATION_TTB );
mBottomOffset = (int)a.getDimension( R.styleable.MultiDirectionSlidingDrawer_bottomOffset, 0.0f );
mTopOffset = (int)a.getDimension( R.styleable.MultiDirectionSlidingDrawer_topOffset, 0.0f );
mAllowSingleTap = a.getBoolean( R.styleable.MultiDirectionSlidingDrawer_allowSingleTap, true );
mAnimateOnClick = a.getBoolean( R.styleable.MultiDirectionSlidingDrawer_animateOnClick, true );
mInvert = ( orientation == ORIENTATION_TTB || orientation == ORIENTATION_LTR );
int handleId = a.getResourceId( R.styleable.MultiDirectionSlidingDrawer_handle, 0 );
if ( handleId == 0 ) { throw new IllegalArgumentException( "