首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SlidingDrawer动画速度

SlidingDrawer动画速度
EN

Stack Overflow用户
提问于 2011-07-31 01:49:07
回答 1查看 4.1K关注 0票数 3

我刚接触Android编程和堆栈溢出,我需要在我的应用程序中放慢SlidingDrawer的动画速度。

我已经像这样将SlidingDrawer子类化了:

代码语言:javascript
复制
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,例如

代码语言:javascript
复制
TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.SlidingDrawer, defStyle, 0);

这是Eclipse无法解决的问题。

还有四个成员变量(mTop、mBottom、mLeft、mRight)是Eclipse不能解析的。

如何让Eclipse找到这些资源/变量?然后我将能够编辑一些变量来产生一个较慢的动画,对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-01 05:21:54

我相信R.styleable已经从sdk中删除了,不过你可以自己写,这样的东西应该可以工作。

在values文件夹中创建一个xml文件

代码语言:javascript
复制
<?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>

然后,在slider类中可以引用xml文件

代码语言:javascript
复制
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MySlider, defStyle, 0);

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

我还扩展了slidingdrawer类,这对我很有效。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6884917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档