-1

我有一个MenuView ViewGroup封装了我界面的所有按钮。在应用程序启动时,所有按钮都通过 定位button.layout()。然后我隐藏了所有按钮,只有一个例外:菜单按钮。当我按下菜单按钮时,会显示所有其他按钮(通过缩放动画)。

现在的问题是:通过 定位按钮时button.layout(),它们是可见的。所以几乎 1 秒所有按钮都显示在应用程序启动时。

我怎样才能避免这种情况?

我的按钮代码:

public class CircleButton extends Button {

    static final int StateDefault = 0;
    static final int StateFocused = 1;
    static final int StatePressed = 2;


    private Bitmap mBitmapDefault;

    private String mCaption;
    protected int radius;
    protected int color = 0xff000000;
    private Typeface font1;
    private boolean visible = true;
    private int savedLeft, savedTop;
    // indicates whether the button is visible when the menu is rendered
    public boolean visibleInMenu = true;
    private Paint paint;
    private static String TAG = "CircleButton";

    public int getRadius(){
        return radius;
    }


    public CircleButton setRadius(int radius){
        this.radius = radius;
        this.setWidth(2*radius);
        this.setHeight(2*radius);
        return this;
    }

    public CircleButton setColor(int color){
        this.color = color;
        return this;
    }

    public CircleButton setCaption(String caption){
        mCaption = caption;
        return this;
    }
    public CircleButton(Context context) {
        super(context);
        init(context);
    }

    public CircleButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CircleButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        setClickable(true);
        setBackgroundColor(0x00000000);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);
        font1 = Typeface.createFromAsset(context.getAssets(), "fonts/Bebas/BEBAS___.ttf");
        mCaption = "Caption";
        this.setRadius(45);
        this.setTextSize(18);

        setOnClickListener(onClickListener);
        setOnTouchListener(onTouchListener);
    }

    public void setVisibility(int visibility) {
        if(visibility == GONE){
            this.visible = false;
        }else{
            this.visible = true;
        }
        super.setVisibility(visibility);
    }
    /**
     * draws the normal button (without rollover)
     * @param canvas
     */
    protected void drawDefault(Canvas canvas){
        //canvas.drawARGB(255, 255, 0, 0);
        Paint paintText = new Paint();
        paintText.setAntiAlias(true);
        paintText.setTextSize(this.getTextSize());
        paintText.setColor(0xffffffff); // white
        paintText.setTypeface(this.font1);

        Rect bounds = new Rect();
        //Paint textPaint = this.getPaint();
        paintText.getTextBounds(mCaption,0,mCaption.length(),bounds);

        float left = (float) (2*radius-bounds.width())/2;
        float top = (float) radius+bounds.height()/2;
        //Log.v("Button","bounds:"+bounds.width()+"x"+bounds.height());
        //Log.v("Button","this.getWIdth:"+2*radius);
        // create the Drawing Tool (Brush)
        Paint paint = new Paint();
        paint.setAntiAlias(true); // for a nicer paint

        paint.setColor(color);
        paint.setStrokeWidth(1);
        paint.setStyle(Style.FILL);

        Path path = new Path();
        path.addCircle(radius, radius, radius, Path.Direction.CW);
        canvas.drawPath(path, paint);

        canvas.save(); 
        canvas.rotate(-45,this.getRadius(),this.getRadius());
        canvas.drawText(mCaption, left, top, paintText);
        canvas.restore();
    }



    protected Bitmap getDefaultBitmap(){
        if(mBitmapDefault == null){
            mBitmapDefault = Bitmap.createBitmap(2*radius, 2*radius, Config.ARGB_8888);
            Canvas canvas = new Canvas(mBitmapDefault);
            this.drawDefault(canvas);
            return mBitmapDefault;
        }
        return mBitmapDefault;
    }




    @Override
    protected void onDraw(Canvas canvas) {
        //Log.v("Button","onDraw(): "+this.getWidth()+"x"+this.getHeight());
        super.onDraw(canvas);
        canvas.drawBitmap(this.getDefaultBitmap(), 0, 0, paint);
        //super.onDraw(canvas);
        //canvas.drawBitmap(this.getDefaultBitmap(), new Rect(0, 0, this.radius*8, this.radius*8), new Rect(0,0, this.radius*2, this.radius*2), null);
    }


    public void recycle() {
        if(mBitmapDefault != null){
            mBitmapDefault.recycle();
            mBitmapDefault = null;
        }
    }

    public void hide(){
        this.hide(true);
    }
    public void hide(boolean withAnimation){
        if(this.visible == true){
            savedLeft = getLeft();
            savedTop = getTop();

            ScaleAnimation anim = new ScaleAnimation(1, 0, 1, 0, this.getRadius(), this.getRadius());
            anim.setDuration(300);
            anim.setFillAfter(true);
            anim.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {}
                @Override
                public void onAnimationRepeat(Animation animation) {}

                @Override
                public void onAnimationEnd(Animation animation) {
                    visible = false;
                    layout(0, 0, 0, 0);
                }
            });
            this.startAnimation(anim);

        }

    }

    public void show(){
        if(this.visible == false){
            this.setVisibility(VISIBLE);
            OvershootInterpolator inter = new OvershootInterpolator();
            ScaleAnimation anim = new ScaleAnimation(0, 1, 0, 1, this.getRadius(), this.getRadius());
            anim.setDuration(300);
            anim.setInterpolator(inter);
            anim.setFillAfter(true);
            anim.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) { }

                @Override
                public void onAnimationRepeat(Animation animation) { }

                @Override
                public void onAnimationEnd(Animation animation) {
                    visible = true;

                }
            });
            this.startAnimation(anim);

            this.layout(this.savedLeft, this.savedTop, this.savedLeft+2*this.radius, this.savedTop+2*this.radius);
        }
        this.visible = true;
    }


    public CircleButton setOnClickCallback(OnClickListener l) {
        super.setOnClickListener(l);
        return this;
    }
    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {

        }
    };

    private OnTouchListener onTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Log.v("Button","onTouch()");
            if(event.getAction() == MotionEvent.ACTION_DOWN){
                OvershootInterpolator inter = new OvershootInterpolator();
                ScaleAnimation anim = new ScaleAnimation(1, (float) 1.5, 1,(float) 1.5, CircleButton.this.getRadius(), CircleButton.this.getRadius());
                anim.setInterpolator(inter);
                anim.setDuration(200);
                anim.setFillAfter(true);
                CircleButton.this.startAnimation(anim);
            }else if(event.getAction() == MotionEvent.ACTION_UP){
                ScaleAnimation anim = new ScaleAnimation((float) 1.5, 1, (float) 1.5, 1, CircleButton.this.getRadius(), CircleButton.this.getRadius());
                anim.setDuration(300);
                anim.setFillAfter(true);
                CircleButton.this.startAnimation(anim);
            }
            return false;
        }
    };
}
4

2 回答 2

0

在对象上使用 setVisibility(int visibility)方法以使用 VISIBLE、INVISIBLE 或 GONE 之一的参数可见性隐藏它:

消失了

此视图是不可见的,并且它不占用任何空间用于布局目的。与 setVisibility(int) 和 android:visibility 一起使用。常数值:8 (0x00000008)

无形的

这个视图是不可见的,但它仍然占用空间用于布局。与 setVisibility(int) 和 android:visibility 一起使用。常数值:4 (0x00000004)

可见的

这个视图是可见的。与 setVisibility(int) 和 android:visibility 一起使用。常数值:0 (0x00000000)

于 2012-05-28T17:26:49.490 回答
0

尝试将android:visibility="invisible"这些按钮放在布局 xml 中。

于 2012-05-28T16:15:14.577 回答