本文介绍了在Android中动态排列ImageButtons围绕圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题旨在更集中于一个或多个布局文件,我知道如何计算和定位围绕圆所需的每个项目,但是我不清楚的是如何对xml文件进行编码,我的意思是,我定义了几个ImageButton?它是相对布局吗?我应该使用列表代替并使用ImageButtons填充吗?...我很困惑:/

My question is intended to be more focused on the layout file or files, I know how to calculate and position every item that I need around the circle, but what I don´t have clear, is how to code the xml files, I mean, how many ImageButtons do I define? Is it a relative Layout? Do I use a list instead and populated with ImageButtons?... I´m confused :/

预先感谢您:)

推荐答案

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

I forgot to answer this question, I found the solution long time ago, I did it this way:

/**
 * 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,希望对您有用.

And I had a RelativeLayout with a FrameLayout in my XML file, hope you find it useful.

这篇关于在Android中动态排列ImageButtons围绕圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 11:48