圆形菜单问题

时间:2014-01-28 16:37:19

标签: java android menu

我有一点问题,我正在制作一个应用程序,我认为有一个圆形菜单是一个好主意,我的问题是只有退出图标显示其他人不,也许我搞砸了代码中的一些东西,你能帮助我吗?

TestMenuActivity.java

 package com.example.circlemenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class TestMenuActivity extends Activity {
         CircleView cView;

         @Override
         public void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);

                   //int numberOfElements = 11;
                   int numberOfElements = 6;
                   View[] elems = new View[numberOfElements];

                   //EditText tv = new EditText(this);
                   //tv.setText("Some text");

                   ImageButton tv = new ImageButton(this);
                   tv.setBackgroundResource(R.drawable.images1);
                   tv.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[0] = tv;

                   //for (int i = 1; i < numberOfElements - 1; i++) {
                     //       Button newButton = new Button(this);
                       //     newButton.setText("Button   " + i);
                         //   newButton.setLayoutParams(new RelativeLayout.LayoutParams(
                           //                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                             //                  RelativeLayout.LayoutParams.WRAP_CONTENT));
                            //elems[i] = newButton;
                   //}

                   //Spinner sp = new Spinner(this);
                   //sp.setLayoutParams(new RelativeLayout.LayoutParams(
                     //                 RelativeLayout.LayoutParams.WRAP_CONTENT,
                       //               RelativeLayout.LayoutParams.WRAP_CONTENT));
                   //elems[numberOfElements - 1] = sp;

                   ImageButton tv1 = new ImageButton(this);
                   tv.setBackgroundResource(R.drawable.imagen2);
                   tv.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[1] = tv1;

                   ImageButton tv2 = new ImageButton(this);
                   tv.setBackgroundResource(R.drawable.image3);
                   tv.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[2] = tv2;

                   ImageButton tv3 = new ImageButton(this);
                   tv.setBackgroundResource(R.drawable.imagen4);
                   tv.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[3] = tv3;

                   ImageButton tv4 = new ImageButton(this);
                   tv.setBackgroundResource(R.drawable.imagen5);
                   tv.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[4] = tv4;

                   ImageButton tv5 = new ImageButton(this);
                   tv.setBackgroundResource(R.drawable.image6);
                   tv.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[numberOfElements - 1] = tv5;

                   cView = new CircleView(this, 115, elems);
                   setContentView(cView);
         }
}

CircleView.java

package com.example.circlemenu;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

public class CircleView extends RelativeLayout {
         static final int centerId = 111;
         private final int radius;

         private RelativeLayout.LayoutParams createNewRelativeLayoutParams() {
                   RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT);
                   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);
                   this.addView(center);

                   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));
                            }
                   }
         }

}

2 个答案:

答案 0 :(得分:0)

每次要设置图像时都使用tv变量,而不是使用要在元素列表中分配的图像(tv1tv2等)< / p>

答案 1 :(得分:0)

试试这个:

class CircleView extends ViewGroup {

    public CircleView(Context context) {
        super(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int cnt = getChildCount();
        int x0 = r / 2;
        int y0 = b / 2;
        int radius = x0 * 2 / 3; 
        for (int i = 0; i < cnt; i++) {
            double angle = i * 2 * Math.PI / cnt;
            int x = (int) (x0 + radius * Math.sin(angle));
            int y = (int) (y0 - radius * Math.cos(angle));
            View child = getChildAt(i);
            int w2 = child.getMeasuredWidth() / 2;
            int h2 = child.getMeasuredHeight() / 2;
            child.layout(x - w2, y - h2, x + w2, y + h2);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
    }
}

使用onCreate中的以下内容对其进行测试:

    CircleView menu = new CircleView(this);
    setContentView(menu);
    for (int i = 0; i < 7; i++) {
        Button b = new Button(this);
        b.setText("#" + i);
        menu.addView(b);
    }

了解CircleView如何扩展ViewGroup简短