android布局设计动态

时间:2014-02-19 09:30:02

标签: android android-layout dynamic

我正在尝试在android中设计ui部分...有一个布局可以保存图像...并且转到另一个视图,它在相同的布局上动态创建元素...我的动态布局效果很好但是静态的不显示...简而言之,我希望在静态布局上使用动态图像。我的代码是:

  public class CircleView extends RelativeLayout {

    static final int centerId = 100;
    static final int center = 110;
    private final int radius;
    ImageView image=new ImageView(getContext());

    private RelativeLayout.LayoutParams createNewRelativeLayoutParams() {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        //image.setLayoutParams(lp);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
        //image.setImageResource(R.drawable.info);
        //image.setLayoutParams(lp);

        lp.addRule(RelativeLayout.ABOVE, centerId);
        lp.addRule(RIGHT_OF, centerId);

        return lp;
    }


    private View prepareElementForCircle(View elem, int distX, int distY) {
        RelativeLayout.LayoutParams lp = createNewRelativeLayoutParams();


        elem.measure(0, 0);
        int deltaX = elem.getMeasuredWidth() / 2;
        int deltaY = elem.getMeasuredHeight() / 2;
        lp.setMargins(distX - deltaX, 0, 0, radius - distY - deltaY);
        elem.setLayoutParams(lp);
        return elem;
    }


    public CircleView(Context context, int radius, View[] elements) {
        super(context);
        this.radius = radius;

        /*RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        this.setLayoutParams(lpView);*/

        View center = new View(context);
        center.setId(centerId);
        RelativeLayout.LayoutParams lpcenter = new RelativeLayout.LayoutParams(
                0, 0);
        lpcenter.addRule(CENTER_HORIZONTAL);
        lpcenter.addRule(CENTER_VERTICAL);
        center.setLayoutParams(lpcenter);

        //center.setBackgroundResource(R.drawable.info);
        this.addView(center);
        //ImageView tt=new ImageView(this);

        this.addView(prepareElementForCircle(elements[0], 0, 0));
        if (elements.length % 2 == 0) {
            this.addView(prepareElementForCircle(elements[elements.length / 2],
                    0, 2 * radius));
        }
        if (elements.length > 2) {
            for (int i = 1; i <= (elements.length - 1) / 2; i++) {
                int y = i * 4 * radius / elements.length;
                int x = (int) Math.sqrt(Math.pow(radius, 2)
                        - Math.pow((radius - y), 2));
                this.addView(prepareElementForCircle(elements[i], x, y));
                this.addView(prepareElementForCircle(elements[elements.length
                        - i], -x, y));
            }
        }
    }
}

0 个答案:

没有答案