我怎样才能在自己的ActionBarDrawerToggle中实现指标?

时间:2014-11-28 07:25:43

标签: android animation android-actionbar drawerlayout

我想在ActionBarDrawerToggle实现中添加动画作为向上指示符。 Before Click After Click

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

 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);
    }
}

如何实现动画?

1 个答案:

答案 0 :(得分:2)

您正在寻找navigation drawer设计,例如Android L 此设计在API 21中引入,并在API 14及以上版本中使用相同的功能使用此.. ..

FragmentActivity

中的

 private DrawerArrowDrawable drawerArrow; 

 private ActionBarDrawerToggle mDrawerToggle;

drawerArrow = new DrawerArrowDrawable(this) {
            @Override 
            public boolean isLayoutRtl() { 
                return false; 
            } 
        }; 

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            drawerArrow, R.string.drawer_open,
            R.string.drawer_close)

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

写入DrawerArrowDrawable

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));
    } 
}