简体   繁体   English

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

[英]How to Set Parent Alignment of Programatically Created Image's in Custom Layout

Hello guys i have a class extending RelativeLayout , which receives and adds Programatically created ImageView's with the context, but the added images are always on Center while i need them to be Aligned on parents top|right , i have tried to add rule but it returns null , here is my code : 大家好,我有一个扩展RelativeLayout的类,该类接收并添加以编程方式创建的带有上下文的ImageView,但是添加的图像始终位于Center上,而我需要将它们对齐在上层的上层| right上,我试图添加规则,但返回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;
}

}

and here is the xml : 这是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>

alittle help please? 请帮忙吗?

I can't see where you use Rule in your class but you can do this in your addItem() method: 我看不到在类中使用Rule的位置,但是可以在addItem()方法中执行此操作:

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

item.setLayoutParams(params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM