我有一个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;
}
};
}
答案 0 :(得分:0)
尝试将android:visibility="invisible"
放入布局xml中的那些按钮。
答案 1 :(得分:0)
在对象上使用setVisibility(int visibility)方法隐藏它,参数可见性可见,不可见或GONE之一:
此视图不可见,并且不需要任何空间进行布局。与setVisibility(int)和android:visibility一起使用。 常数值:8(0x00000008)
此视图不可见,但仍会占用布局空间。与setVisibility(int)和android:visibility一起使用。 常数值:4(0x00000004)
此视图可见。与setVisibility(int)和android:visibility一起使用。 常数值:0(0x00000000)