2011-07-30 197 views
3

我是新來的Android編程和堆棧溢出,我需要在我的應用程序中減慢SlidingDrawer的動畫速度。SlidingDrawer動畫速度

我已經子類SlidingDrawer這樣的:

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.SlidingDrawer; 

public class WrappingSlidingDrawer extends SlidingDrawer { 

public WrappingSlidingDrawer(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 

    int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL); 
    mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0); 
    mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL); 
} 

public WrappingSlidingDrawer(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL); 
    mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0); 
    mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL); 
} 


@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

    int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); 
    int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); 

    int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); 
    int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); 

    if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) { 
     throw new RuntimeException("SlidingDrawer cannot have UNSPECIFIED dimensions"); 
    } 

    final View handle = getHandle(); 
    final View content = getContent(); 
    measureChild(handle, widthMeasureSpec, heightMeasureSpec); 

    if (mVertical) { 
     int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset; 
     content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode)); 
     heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight(); 
     widthSpecSize = content.getMeasuredWidth(); 
     if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth(); 
    } 
    else { 
     int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset; 
     getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec); 
     widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth(); 
     heightSpecSize = content.getMeasuredHeight(); 
     if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight(); 
    } 

    setMeasuredDimension(widthSpecSize, heightSpecSize); 
} 

private boolean mVertical; 
private int mTopOffset; 

}

我發現這個鏈接:How to change animation velocity on SlidingDrawer,和它說,我能找到一個替代實現,或者複製源和修改。

我在我自己的項目中創建了SlidingDrawer.java並粘貼了代碼here,但有錯誤。有幾行引用R.styleable.SlidingDrawer,例如

TypedArray a = context.obtainStyledAttributes(attrs, 
      R.styleable.SlidingDrawer, defStyle, 0); 

Eclipse無法解析。

還有四個Eclipse無法解析的成員變量(mTop,mBottom,mLeft,mRight)。

如何讓Eclipse找到這些資源/變量?然後,我將能夠編輯一些變量以產生較慢的動畫,對嗎?

+0

嘿,你可以請你提供最終的自定義類,以及如何將它添加到XML中,因爲它給我錯誤「InflateException」。 – abhishek

回答

4

我相信R.styleable從SDK中刪除,你可以寫你自己的,雖然,這樣的事情應該工作

在值文件夾中的滑塊創建一個XML文件

<?xml version="1.0" encoding="UTF-8"?> 
<resources> 
<declare-styleable name="MySlider"> 
     <attr name = "SlidingDrawer_orientation" format="integer"/> 
     <attr name = "SlidingDrawer_bottomOffset" format = "dimension"/> 
     <attr name = "SlidingDrawer_topOffset" format = "dimension"/> 
     <attr name = "SlidingDrawer_allowSingleTap" format = "boolean"/> 
     <attr name = "SlidingDrawer_animateOnClick" format = "boolean"/> 
     <attr name = "SlidingDrawer_handle" format = "reference"/> 
     <attr name = "SlidingDrawer_content" format = "reference"/> 
    </declare-styleable> 
</resources> 

然後類可以引用xml文件

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MySlider, defStyle, 0); 

int orientation = a.getInt(R.styleable.MySlider_SlidingDrawer_orientation, ORIENTATION_VERTICAL); 
//...etc. ... 

我也在擴展slidingdrawer類,這對我很有用。

+0

非常感謝!這解決了我的問題,我現在可以繼續擴展課程。 –

+0

你是如何解決成員變量問題的? – 2013-01-24 23:13:56

+0

你的意思是'mTop','mBottom'等只使用它們的getter(),'getTop()','getBottom()'等 – triggs