问题
I'm new to Android programming and to stack overflow, and I need to slow down the animation speed of a SlidingDrawer in my app.
I've already subclassed SlidingDrawer like this:
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;
}
I found this link: How to change animation velocity on SlidingDrawer, and it says that I can find an alternative implementation, or copy the source and modify it.
I created SlidingDrawer.java in my own project and pasted the code from here, but there are errors. There are several lines that reference R.styleable.SlidingDrawer, e.g.
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SlidingDrawer, defStyle, 0);
which Eclipse can't resolve.
There are also four member variables (mTop, mBottom, mLeft, mRight) which Eclipse can't resolve.
How do I make Eclipse find these resources/variables? I will then be able to edit some variables to result in a slower animation, right?
回答1:
I believe R.styleable was removed from the sdk, you can write your own though, something like this should work
In the values folder create an xml file
<?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>
Then in the slider class you can reference the xml file
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MySlider, defStyle, 0);
int orientation = a.getInt(R.styleable.MySlider_SlidingDrawer_orientation, ORIENTATION_VERTICAL);
//...etc. ...
I am also extending the slidingdrawer class and this works for me.
来源:https://stackoverflow.com/questions/6884917/slidingdrawer-animation-velocity