关于SlidingDrawer控件
1.重要属性
android:allowSingleTap:指示是否可以通过handle打开或关闭
android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。
android:content:隐藏的内容
android:handle:handle(手柄)
2.重要方法
animateClose():关闭时实现动画。
close():即时关闭
getContent():获取内容
isMoving():指示SlidingDrawer是否在移动。
isOpened():指示SlidingDrawer是否已全部打开
lock():屏蔽触摸事件。
setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener):SlidingDrawer关闭时调用
unlock():解除屏蔽触摸事件。
toggle():切换打开和关闭的抽屉SlidingDrawer。
实例:
自定义SlidingDrawer控件,为得是在SlidingDrawer关闭的状态下,显示content内容
package com.borqs.music.lyric;
import android.content.Context;
import android.graphics.Canvas;
import android.provider.ContactsContract.CommonDataKinds.Event;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SlidingDrawer;
public class LyricSlidingDrawer extends SlidingDrawer {
public LyricSlidingDrawer(Context paramContext, AttributeSet paramAttributeSet) {
this(paramContext, paramAttributeSet, 0);
}
public LyricSlidingDrawer(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
super(paramContext, paramAttributeSet, paramInt);
}
@Override
protected void dispatchDraw(Canvas canvas) {
long time = getDrawingTime();
View handle = super.getHandle();
View content = super.getContent();
drawChild(canvas, handle, time);
if (!isOpened() || isMoving()) {
canvas.save();
float f2 = handle.getBottom() - content.getTop();
canvas.translate(0, f2);
drawChild(canvas, content, time);
canvas.restore();
} else {
super.dispatchDraw(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == Event.OFFLINE) {
int i = super.getHandle().getBottom();
int j = (int)event.getY();
if (j > i) {
int k = i+20;
if (j <= k) {
animateToggle();
}
}
}
return super.onTouchEvent(event);
}
}
SlidingDrawer完全打开时隐藏专辑图片,完全关闭时显示专辑图片:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
public class NowPlay extends Activity {
private static final String TAG = "NowPlay";
private LyricSlidingDrawer mSlidingDrawer;
private ImageView mHandle;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.now_play);
mSlidingDrawer = (LyricSlidingDrawer) findViewById(R.id.lyrics_sliding_drawer);
mHandle = (ImageView) findViewById(R.id.lyrics_handle_image);
//监听打开抽屉事件
mSlidingDrawer.setOnDrawerOpenListener(new C_SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
mHandle.setImageResource(R.drawable.lyrics_handle_expand);
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
animation.setAnimationListener(new fadeAnimationListener());
mAlbum.startAnimation(animation);
}
});
//监听关闭抽屉事件
mSlidingDrawer.setOnDrawerCloseListener(new C_SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() {
mHandle.setImageResource(R.drawable.lyrics_handle_shrink);
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setStartOffset(500);
animation.setAnimationListener(new fadeAnimationListener());
mAlbum.startAnimation(animation);
}
});
}
private class fadeAnimationListener implements AnimationListener{
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
if (mSlidingDrawer.isOpened()) {
mAlbum.setVisibility(View.GONE);
findViewById(R.id.rs_button_area).setVisibility(View.GONE);
} else {
mAlbum.setVisibility(View.VISIBLE);
}
}
}
所使用的Layout, 注意SlidingDrawer的相对位置!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/parent">
<TextView android:id="@+id/trackname"
android:textSize="18sp"
android:singleLine="true"
android:ellipsize="end"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:layout_alignParentTop="true" />
<ImageView android:id="@+id/album" android:layout_below="@id/trackname"
android:layout_centerHorizontal="true"
android:background="@drawable/album_border_large"
android:layout_width="180dip"
android:layout_height="180dip"
android:layout_marginTop="8dip"/>
<com.borqs.music.lyric.LyricSlidingDrawer android:id="@+id/lyric_slidingdrawer"
android:layout_below="@id/trackname"
android:layout_above="@id/player_common_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:handle="@+id/handle"
android:content="@+id/lyric_view"
android:bottomOffset="-75dip"
android:topOffset="0dip"
android:animateOnClick="true">
<ImageButton android:id="@id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/lyr_handle_collapse_xml"/>
<com.borqs.music.lyric.LyricsView android:id="@id/lyric_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="2dip"
android:paddingBottom="2dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"/>
</com.borqs.music.lyric.LyricSlidingDrawer>
</RelativeLayout>