在Android中围绕Circle动态排列ImageButtons

时间:2015-07-27 20:10:49

标签: android layout geometry

我的问题是要更专注于布局文件或文件,我知道如何计算和定位我需要围绕圆圈的每个项目,但我不清楚,是如何编码xml文件,我的意思是,我定义了多少个ImageButtons?它是相对布局吗?我是否使用了一个列表并填充了ImageButtons?...我很困惑:/

提前谢谢你:)

2 个答案:

答案 0 :(得分:0)

我很确定相对布局不会成为可行的方法,因为你希望它们根据圆圈的中心定位而不是相对于护送。话虽这么说,你需要把它们全部放在一起而不是一个简单的程序,因为图像按钮是方形的,你得到的重叠越接近45度角,除非你把它们隔开足够远的距离或者你也让它们成为圆形。

答案 1 :(得分:0)

我忘了回答这个问题,很久以前我找到了解决方案,我这样做了:

/**
 * Method that describe the circle equation to organize the buttons
 *
 * @param n Number of item to arrange around circle
 */
public void setButtons(int n) {

    /**
     * x = initial position
     * y = initial position
     * R = radius of circle
     * i = item/element/category to position
     * pi = Math.pi
     * nC = number of categories
     *
     *          X                   Y
     * (x+R*Cos(i*2*pi/n) , y-R*Sin(i*2*pi/n))
     *
     */

    int actionBarHeight = Utils.convertDp(8, this);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels + actionBarHeight;
    int width = displaymetrics.widthPixels;
    final int x = (width / 2) - Utils.convertDp(38, this);
    final int y = height / 2 - Utils.convertDp(70, this);
    final double pi = Math.PI;
    final int R = Utils.convertDp(13 * n, this); // 13 is a constants that 
    //assure a good distance when there n > 8
    final int nC = n;

    Log.i("DATOS ECUACION = ", "Height: " + height +
            " Width: " + width +
            " x: " + x +
            " y: " + y);

    final Handler mHandler = new Handler();

    for (int i = 0; i < nC; i++) {

        final ImageButton mIb = new ImageButton(DooActivity.this);
        final TextView mTv = new TextView(DooActivity.this);

        try {
            File f = new File(mList.get(i).getLocalURL_ImageCircle());
            Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            mIb.setVisibility(View.INVISIBLE);
            mIb.setImageBitmap(b);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        float xI = ((float) (x + R * Math.cos(i * 2 * pi / nC)));
        float yI = ((float) (y - R * Math.sin(i * 2 * pi / nC)));
        mIb.setX(xI);
        mIb.setY(yI);

        mTv.setX(xI + Utils.convertDp(15, this));
        mTv.setY(yI + Utils.convertDp(60, this));
        mTv.setTextSize(11);

        mIb.setAdjustViewBounds(true);
        mIb.setMaxHeight(Utils.convertDp(80, this));
        mIb.setMaxWidth(Utils.convertDp(80, this));
        mIb.setBackgroundColor(Color.TRANSPARENT);

        mTv.setMaxHeight(Utils.convertDp(80, this));
        mTv.setMaxWidth(Utils.convertDp(80, this));

        LinearLayout.LayoutParams vp = new  
        LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT);
        mIb.setLayoutParams(vp);
        mIb.setId(mList.get(i).getCategoryId().intValue());
        mIb.setTag(mList.get(i));
        mIb.setOnClickListener(this);

        mListImageButton.add(mIb);

        mTv.setLayoutParams(vp);

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                ScaleAnimation scale = new ScaleAnimation(0, 1, 0, 1,
                        ScaleAnimation.RELATIVE_TO_PARENT, .5f,
                        ScaleAnimation.RELATIVE_TO_PARENT, .5f);
                scale.setDuration(140);
                mIb.startAnimation(scale);
                mIb.setVisibility(View.VISIBLE);

                mTv.startAnimation(scale);
                mTv.setVisibility(View.VISIBLE);
                mFl.addView(mIb);
                mFl.addView(mTv);
            }
        }, 1000);

        Log.i("DATOS FOR = ", "x" + i + ": " + xI +
                " y" + i + ": " + yI +
                " CAT ID: " + mList.get(i).getCategoryId());
    }
}

我的XML文件中有一个带有FrameLayout的RelativeLayout,希望你觉得它很有用。