如何在自定义布局中设置以编程方式创建的图像的父对齐方式

时间:2014-10-03 15:28:35

标签: android relativelayout

大家好,我有一个扩展RelativeLayout的类,它接收和添加以编程方式创建的ImageView与上下文,但添加的图像总是在中心,而我需要它们在父母顶部对齐,我有试图添加规则,但它返回null,这是我的代码:

public class ArcMenu extends RelativeLayout {
private static ArcLayout mArcLayout;
private static ImageView mHintView;
private static ViewGroup controlLayout;
private static boolean shouldDisapear = false;
Animation animation;


public ArcMenu(Context context) {
    super(context);
    init(context);
}

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

public void shouldDisapear(boolean should){
    shouldDisapear = should;

    postDelayed(new Runnable() {

@Override
public void run() {
    mArcLayout.setVisibility(View.GONE);
    controlLayout.setVisibility(View.GONE);
    mHintView.setVisibility(View.GONE);

}
}, 400);


}

private void init(Context context) {
    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    li.inflate(R.layout.arc_menu, this);

    mArcLayout = (ArcLayout) findViewById(R.id.item_layout);
    controlLayout = (ViewGroup) findViewById(R.id.control_layout);
    controlLayout.setClickable(true);
    controlLayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {


                mHintView.startAnimation(createHintSwitchAnimation(mArcLayout.isExpanded()));

                if(!shouldDisapear){
                  mArcLayout.switchState(true);
              }
            }

            return false;
        }
    });

    mHintView = (ImageView) findViewById(R.id.control_hint);
}

public void setAngle(int from, int to){
    mArcLayout.setArc(from,to);
}

public void setChildSize(int size){
    mArcLayout.setChildSize(size);
}


public void setControlHintBackGround(Drawable drawable){

    mHintView.setImageDrawable(drawable);


}

public void setControlHintBackGround(int resource){
    mHintView.setImageResource(resource);
}

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public void setControlLayoutBackground(Drawable drawable){
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {  
    controlLayout.setBackgroundDrawable(drawable);
 }else{
    controlLayout.setBackground(drawable);
 }
}

public void setControlLayoutBackground(int resource){
    controlLayout.setBackgroundResource(resource);
}


public void Expand(){

    if(mArcLayout.getVisibility() == View.GONE){
        mArcLayout.setVisibility(View.VISIBLE);
        controlLayout.setVisibility(View.VISIBLE);
        mHintView.setVisibility(View.VISIBLE);
    }
    postDelayed(new Runnable() {

        @Override
        public void run() {
            mHintView.startAnimation(createHintSwitchAnimation(mArcLayout.isExpanded()));

        }
    }, 50);


}

public boolean isExpanded(){
    return mArcLayout.isExpanded();
}

private void applyAttrs(AttributeSet attrs) {
    if (attrs != null) {
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ArcLayout, 0, 0);

        float fromDegrees = a.getFloat(R.styleable.ArcLayout_fromDegrees, ArcLayout.DEFAULT_FROM_DEGREES);
        float toDegrees = a.getFloat(R.styleable.ArcLayout_toDegrees, ArcLayout.DEFAULT_TO_DEGREES);
        mArcLayout.setArc(90,180);

        int defaultChildSize = mArcLayout.getChildSize();
        int newChildSize = a.getDimensionPixelSize(R.styleable.ArcLayout_childSize, defaultChildSize);
        mArcLayout.setChildSize(newChildSize);

        a.recycle();
    }
}

public void addItem(View item, OnClickListener listener) {

    mArcLayout.addView(item);
    item.setOnClickListener(getItemClickListener(listener));
}

private OnClickListener getItemClickListener(final OnClickListener listener) {
    return new OnClickListener() {

        @Override
        public void onClick(final View viewClicked) {
            Animation animation = bindItemAnimation(viewClicked, true, 400);
            animation.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            itemDidDisappear();

                            if(shouldDisapear){

                                mArcLayout.setVisibility(View.GONE);
                                controlLayout.setVisibility(View.GONE);
                                mHintView.setVisibility(View.GONE);
                            }

                        }
                    }, 0);
                }
            });

            final int itemCount = mArcLayout.getChildCount();
            for (int i = 0; i < itemCount; i++) {
                View item = mArcLayout.getChildAt(i);
                if (viewClicked != item) {
                    bindItemAnimation(item, false, 300);
                }
            }

            mArcLayout.invalidate();
            mHintView.startAnimation(createHintSwitchAnimation(true));

            if (listener != null) {
                listener.onClick(viewClicked);
            }
        }
    };
}

private Animation bindItemAnimation(final View child, final boolean isClicked, final long duration) {
    Animation animation = createItemDisapperAnimation(duration, isClicked);
    child.setAnimation(animation);

    return animation;
}

private void itemDidDisappear() {
    final int itemCount = mArcLayout.getChildCount();
    for (int i = 0; i < itemCount; i++) {
        View item = mArcLayout.getChildAt(i);
        item.clearAnimation();
    }
    if(!shouldDisapear){
    mArcLayout.switchState(false);
    }
}

private static Animation createItemDisapperAnimation(final long duration, final boolean isClicked) {
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.addAnimation(new ScaleAnimation(1.0f, isClicked ? 2.0f : 0.0f, 1.0f, isClicked ? 2.0f : 0.0f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
    animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));

    animationSet.setDuration(duration);
    animationSet.setInterpolator(new DecelerateInterpolator());
    animationSet.setFillAfter(true);

    return animationSet;
}

private static Animation createHintSwitchAnimation(final boolean expanded) {
    Animation animation = new RotateAnimation(expanded ? 45 : 0, expanded ? 0 : 45, Animation.RELATIVE_TO_SELF,
            0.5f, Animation.RELATIVE_TO_SELF, 0.5f);


       mArcLayout.switchState(true);   


    animation.setStartOffset(0);
    animation.setDuration(500);
    animation.setInterpolator(new DecelerateInterpolator());
    animation.setFillAfter(true);
    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            if(shouldDisapear && expanded){
                mArcLayout.setVisibility(View.GONE);
                controlLayout.setVisibility(View.GONE);
                mHintView.setVisibility(View.GONE);
            }


        }
    });



    return animation;
}

}

,这是xml:

 <?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" 
>

<com.capricorn.ArcLayout
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    custom:childSize="44dp"

    custom:fromDegrees="270.0"
    custom:toDegrees="360.0" />

<FrameLayout
    android:id="@+id/control_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
      >

    <ImageView
        android:id="@+id/control_hint"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:duplicateParentState="true"
      />
</FrameLayout>

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

我无法在您的班级中查看您在哪里使用规则,但您可以使用addItem()方法执行此操作:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)item.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

item.setLayoutParams(params);