首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ActionBarDrawerToggle中实现up indicator?

如何在ActionBarDrawerToggle中实现up indicator?
EN

Stack Overflow用户
提问于 2014-11-28 15:25:43
回答 1查看 1K关注 0票数 0

我想在ActionBarDrawerToggle implements中添加一个动画作为向上指示器。

我试着研究了ActionBarDrawerToggle的源代码,我只是发现了设置图片的构造函数:mHomeAsUpIndicator = getThemeUpIndicator();和方法syncState改变了图片:

代码语言:javascript
复制
 public void syncState() {
    if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
        mSlider.setPosition(1);
    } else {
        mSlider.setPosition(0);
    }
    if (mDrawerIndicatorEnabled) {
        setActionBarUpIndicator((Drawable) mSlider,
                mDrawerLayout.isDrawerOpen(GravityCompat.START) ?
                        mCloseDrawerContentDescRes : mOpenDrawerContentDescRes);
    }
}

如何实现动画?

EN

回答 1

Stack Overflow用户

发布于 2014-11-28 18:49:18

你正在寻找像Android L这样的navigation drawer设计,这个设计是在API 21中引入的,要在API 14和更高版本中获得相同的功能,请使用以下代码。使用

FragmentActivity

代码语言:javascript
复制
 private DrawerArrowDrawable drawerArrow; 

 private ActionBarDrawerToggle mDrawerToggle;

代码语言:javascript
复制
drawerArrow = new DrawerArrowDrawable(this) {
            @Override 
            public boolean isLayoutRtl() { 
                return false; 
            } 
        }; 

代码语言:javascript
复制
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            drawerArrow, R.string.drawer_open,
            R.string.drawer_close)

使用(SDK_FOLDER\sources\android-21\android\support\v7\app )

DrawerArrowDrawable编写

代码语言:javascript
复制
public abstract class DrawerArrowDrawable extends Drawable {
    private static final float ARROW_HEAD_ANGLE = (float) Math.toRadians(45.0D);
    protected float mBarGap;
    protected float mBarSize;
    protected float mBarThickness;
    protected float mMiddleArrowSize;
    protected final Paint mPaint = new Paint();
    protected final Path mPath = new Path();
    protected float mProgress;
    protected int mSize;
    protected float mVerticalMirror = 1f;
    protected float mTopBottomArrowSize;
    protected Context context;


    public DrawerArrowDrawable(Context context) {
        this.context = context;
        this.mPaint.setAntiAlias(true);
        this.mPaint.setColor(context.getResources().getColor(R.color.Black));
        this.mSize = context.getResources().getDimensionPixelSize(R.dimen.ldrawer_drawableSize);
        this.mBarSize = context.getResources().getDimensionPixelSize(R.dimen.ldrawer_barSize);
        this.mTopBottomArrowSize = context.getResources().getDimensionPixelSize(R.dimen.ldrawer_topBottomBarArrowSize);
        this.mBarThickness = context.getResources().getDimensionPixelSize(R.dimen.ldrawer_thickness);
        this.mBarGap = context.getResources().getDimensionPixelSize(R.dimen.ldrawer_gapBetweenBars);
        this.mMiddleArrowSize = context.getResources().getDimensionPixelSize(R.dimen.ldrawer_middleBarArrowSize);
        this.mPaint.setStyle(Paint.Style.STROKE);
        this.mPaint.setStrokeJoin(Paint.Join.ROUND);
        this.mPaint.setStrokeCap(Paint.Cap.SQUARE);
        this.mPaint.setStrokeWidth(this.mBarThickness);
    } 


    protected float lerp(float paramFloat1, float paramFloat2, float paramFloat3) {
        return paramFloat1 + paramFloat3 * (paramFloat2 - paramFloat1);
    } 


    public void draw(Canvas canvas) {
        Rect localRect = getBounds();
        float f1 = lerp(this.mBarSize, this.mTopBottomArrowSize, this.mProgress);
        float f2 = lerp(this.mBarSize, this.mMiddleArrowSize, this.mProgress);
        float f3 = lerp(0.0F, this.mBarThickness / 2.0F, this.mProgress);
        float f4 = lerp(0.0F, ARROW_HEAD_ANGLE, this.mProgress);
        float f5 = 0.0F;
        float f6 = 180.0F;
        float f7 = lerp(f5, f6, this.mProgress);
        float f8 = lerp(this.mBarGap + this.mBarThickness, 0.0F, this.mProgress);
        this.mPath.rewind();
        float f9 = -f2 / 2.0F;
        this.mPath.moveTo(f9 + f3, 0.0F);
        this.mPath.rLineTo(f2 - f3, 0.0F);
        float f10 = (float) Math.round(f1 * Math.cos(f4));
        float f11 = (float) Math.round(f1 * Math.sin(f4));
        this.mPath.moveTo(f9, f8);
        this.mPath.rLineTo(f10, f11);
        this.mPath.moveTo(f9, -f8);
        this.mPath.rLineTo(f10, -f11);
        this.mPath.moveTo(0.0F, 0.0F);
        this.mPath.close();
        canvas.save();
        if (!isLayoutRtl()) 
            canvas.rotate(180.0F, localRect.centerX(), localRect.centerY());
        canvas.rotate(f7 * mVerticalMirror, localRect.centerX(), localRect.centerY());
        canvas.translate(localRect.centerX(), localRect.centerY());
        canvas.drawPath(this.mPath, this.mPaint);
        canvas.restore();
    } 


    public int getIntrinsicHeight() { 
        return this.mSize;
    } 


    public int getIntrinsicWidth() { 
        return this.mSize;
    } 


    public void setAlpha(int alpha) {
        this.mPaint.setAlpha(alpha);
    } 


    @Override 
    public int getOpacity() { 
        return PixelFormat.TRANSLUCENT;
    } 


    public abstract boolean isLayoutRtl(); 


    public void setColorFilter(ColorFilter colorFilter) {
        this.mPaint.setColorFilter(colorFilter);
    } 


    public void setVerticalMirror(boolean mVerticalMirror) {
        this.mVerticalMirror = mVerticalMirror ? 1 : -1;
    } 


    public void setProgress(float paramFloat) {
        this.mProgress = paramFloat;
        invalidateSelf();
    } 


    public void setColor(int resourceId) {
        this.mPaint.setColor(context.getResources().getColor(resourceId));
    } 
} 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27183811

复制
相关文章

相似问题

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