动画动作条的图标 [英] Animating ActionBar's icon

查看:147
本文介绍了动画动作条的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个动作条图标(主一个在左边,而不是行动项目),我想动画。

I have an ActionBar icon (the main one on the left, not an action item) that I would like to animate.

我设置在我的活动我的动作条的图标是这样的:

I am setting my ActionBar's icon in my Activity like this:

getSupportActionBar()的setIcon(图标)

其中,图标可绘制通过转换的自定义XML视图为位图库产生的。这个XML视图是一个 RelativeLayout的与顶部的背景图像和的TextView

where icon is a Drawable produced by a library that converts a custom XML view into a bitmap. This XML view is a RelativeLayout with a background image and a TextView on top.

今天,当我不得不更新的TextView 我只是重新生成图标并调用的setIcon 一次。相反,我想获得的握住我的的TextView 并应用了一些动画效果就可以了,像淡出,然后淡入更新后(也许永远不必叫的setIcon ,只是重新使用同一个)。

Today, when I have to update the TextView I simply re-generate the icon and call setIcon again. Instead, I would like to get a hold of my TextView and apply some animation effect on it, like fade-out and then fade-in after updating it (maybe never having to call setIcon, just re-use the same one).

不知道如何去了解这一点。有人可以推荐的方法?

Not sure how to go about this. Can someone recommend an approach?

修改:尝试这个办法:

MyActivity

Drawable myDrawable = new MyDrawable();
supportActionBar.setIcon(myDrawable);

public class MyDrawable extends Drawable {
    private Paint paint;
    private RectF rect;

    public MyDrawable() {
        this.paint = new Paint();
        this.rect = new RectF();
    }

    @Override
    public void draw(Canvas canvas) {
        paint.setARGB(255, 0, 255, 0);
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.FILL);

        rect.right = 20f;
        rect.bottom = 20f;

        canvas.drawRoundRect(rect, 0.5f, 0.5f, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

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

什么也不显示。我验证了的onDraw 被调用。可疑的东西对我来说是画布既有高度和放大器;宽度设定为1

Nothing shows up. I verified that onDraw gets called. Something suspicious to me is that canvas has both height & width set to 1.

推荐答案

一个适当的方法为它会忘记XML布局,并创建一个自定义的可绘制

A proper approach for it would be to forget the XML layout and create a custom Drawable.

这个自定义绘制的一个实例将被设置为图标的动作条,并调用 invalidateSelf()必要时重绘(由于动画,例如)。

An instance of this custom drawable will be set to the icon on the ActionBar and call invalidateSelf() whenever necessary to redraw (due to animation, for example).

提拉可容纳参考其他可绘制(如BitmapDrawable有从 / RES / 文件夹或颜色或渐变绘制的背景遮阳的东西),并调用(例如) bgDraw.draw(画布)的onDraw 回调过程中。

The drawable can hold reference to other drawables (e.g. BitmapDrawable to have something from the /res/ folder or a Color or Gradient drawable for a background shade) and call (for example) bgDraw.draw(canvas) during the onDraw callback.

这也可以被的onDraw 回调过程中赋予它的画布上直接绘制的东西。与画布,你可以直接在上面画圈,线,面,道路和文字。

It can also draw stuff directly on the canvas that is given to it during onDraw callback. With the canvas you can draw circle, lines, areas, path and text directly on it.

编辑:

很简单的动画例子(没有检查code,可能错别字):

very simple animation example (didn't check the code, likely typos):

private long animationTime;
public void doAnimation(){
    animationTime = System.currentTimeMilis() + 3000; // 3 seconds
    invalidateSelf();
}

public void onDraw(Canvas canvas){
    // do your drawing.
    // You can use difference between
    // currentTimeMilis and animationTime for status/position
   ...

    // at the end
    if(animationTime > System.currentTimeMilis())
         invalidateSelf();
}

这篇关于动画动作条的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆