Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何设置以编程方式创建的图像的父对齐';自定义布局中的s_Android_Android Relativelayout - Fatal编程技术网

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

Android 如何设置以编程方式创建的图像的父对齐';自定义布局中的s,android,android-relativelayout,Android,Android Relativelayout,大家好,我有一个扩展RelativeLayout的类,它接收并添加通过编程创建的带有上下文的ImageView,但添加的图像始终位于中心,而我需要它们在顶部对齐|对,我尝试添加规则,但返回null,我的代码如下: public class ArcMenu extends RelativeLayout { private static ArcLayout mArcLayout; private static ImageView mHintView; private static ViewGroup

大家好,我有一个扩展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;
}

}
公共类ArcMenu扩展了RelativeLayout{
专用静态ARC布局;
私有静态图像视图;
私有静态视图组控制布局;
私有静态布尔值shouldDisapear=false;
动画;
公共ArcMenu(上下文){
超级(上下文);
init(上下文);
}
公共ArcMenu(上下文上下文、属性集属性){
超级(上下文,attrs);
init(上下文);
applyAttrs(attrs);
}
public void shoulddispear(布尔值should){
shouldDisapear=应该;
postDelayed(新的Runnable(){
@凌驾
公开募捐{
mArcLayout.setVisibility(View.GONE);
controlLayout.setVisibility(View.GONE);
mHintView.setVisibility(View.GONE);
}
}, 400);
}
私有void init(上下文){
LayoutInflater li=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
li.充气(R.layout.arc_菜单,此菜单);
mArcLayout=(ArcLayout)findViewById(R.id.item_布局);
controlLayout=(视图组)findViewById(R.id.control\u布局);
controlLayout.setClickable(真);
setOnTouchListener(新的OnTouchListener(){
@凌驾
公共布尔onTouch(视图v,运动事件){
if(event.getAction()==MotionEvent.ACTION\u向下){
mHintView.startAnimation(createHintSwitchAnimation(mArcLayout.isExpanded());
如果(!shoulddispear){
mArcLayout.switchState(true);
}
}
返回false;
}
});
mHintView=(ImageView)findViewById(R.id.control\u提示);
}
公共无效设置角度(int-from,int-to){
mArcLayout.setArc(从,到);
}
公共void setChildSize(整数大小){
mArcLayout.setChildSize(大小);
}
公共空间设置控制hintbackground(可拉伸){
mHintView.setImageDrawable(可绘制);
}
公共void setControlHintBackGround(int资源){
mHintView.setImageResource(资源);
}
@SuppressLint(“新API”)
@抑制警告(“弃用”)
公共void setControlLayoutBackground(可绘制){
if(android.os.Build.VERSION.SDK_INT <?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>
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)item.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

item.setLayoutParams(params);