在Android的圆形菜单的按钮上给出Arc Effect?

时间:2014-03-12 11:46:35

标签: android button menu

HIII 我必须在R& S之后创建圆形菜单。 D我得到了以下输出,它显示在图像1中,但我希望像图像2一样生效我会把它放在这里我的代码所以你可以检查它

  

我当前输出图像1在此按钮和水平文本显示我想将其转换为弧形效果按钮和文本如下图像2

enter image description here

  

我想让这个按钮像这个图像

enter image description here

MainActivity.java
  

公共类MainActivity扩展了MenuActivity {

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

          @Override       public void createButtons()         {
                  for(int i = 0 ; i < 3 ; i++)            {
          RotativeButton button = new RotativeButton(this);

          if(i==0)
          {   
              button.setBackgroundResource(R.drawable.green);
              //button.setBackgroundColor(Color.GREEN);
              button.setText("Insurance Green");
              button.setPosition(i);
              button.setLabel("OPTION "+(i+1));
              button.setLayoutParams(new  LinearLayout.LayoutParams(100, 100));
          }
          if(i==1)
          {

              button.setBackgroundResource(R.drawable.red);
              //button.setBackgroundColor(Color.RED);
              button.setText("Insurance Red");
              button.setPosition(i);
              button.setLabel("OPTION "+(i+1));
              button.setLayoutParams(new  LinearLayout.LayoutParams(100, 100));

          }
          if(i==2)
          {   
              button.setBackgroundResource(R.drawable.yellow);
              //button.setBackgroundColor(Color.YELLOW);
              button.setText("Insurance Yellow");
              button.setPosition(i);
              button.setLabel("OPTION "+(i+1));
              button.setLayoutParams(new  LinearLayout.LayoutParams(100, 100));   

          }
          Intent intent = new Intent(this, DetailActivity.class);
          intent.putExtra("id", String.valueOf(i+1));
          button.setIntent(intent);
          mButtonList.add(button);
                      }
              }

   }
     

MenuActivity.java

public abstract class  MenuActivity extends Activity {

    protected List<RotativeButton> mButtonList = new ArrayList<RotativeButton>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.common_view);
       // RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainView);
        LinearLayout mainLayout=(LinearLayout)findViewById(R.id.mainView);
        getLayoutInflater().inflate(R.layout.menu, mainLayout, true);
        final RotativeMenuView customView = (RotativeMenuView) findViewById(R.id.rotativeMenuView);
        final Button okBtn = (Button) customView.findViewById(R.id.okBtn);
        customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() 
        {
            public void onGlobalLayout() 
            {
                if(okBtn.getVisibility()==View.VISIBLE)
                {
                    customView.addButtons(mButtonList);
                    customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

            }

        });
        createButtons();
    }

    protected abstract void createButtons();

}

RotativeButton.java

public class RotativeButton extends Button {

    private int position;
    private Intent intent;
    private String label;

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

    public int getPosition() {
        return position;
    }
    public void setPosition(int position) {
        this.position = position;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Intent getIntent() {
        return intent;
    }

    public void setIntent(Intent intent) {
        this.intent = intent;
    }

}
  

RotativeMenuView.java

public class RotativeMenuView extends RelativeLayout
{

    private List<RotativeButton> _buttonList;
    private HashMap<Integer, Point > _pointForPosition = new HashMap<Integer, Point>();
    private HashMap<Integer, RectF > _rectForPosition = new HashMap<Integer, RectF>();
    private TextView titleTv;

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

    public RotativeMenuView(Context context, AttributeSet attrs) 
    { 
        super(context, attrs); 
    } 

    public RotativeMenuView(Context context, AttributeSet attrs, int defStyle) 
    { 
        super(context, attrs, defStyle); 
    } 


    public void addButtons(List<RotativeButton> buttonList) {

        final Button okBtn = (Button) findViewById(R.id.okBtn);
        titleTv = (TextView) findViewById(R.id.labelTv);

        okBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                RotativeButton activeButton = getActiveButton();
                Intent i = activeButton.getIntent();
                getContext().startActivity(i);
            }
        });

        _buttonList = buttonList;
        int numberOfButtons = buttonList.size();
        final double angle = 360 / numberOfButtons;

        int okWidth = okBtn.getMeasuredWidth();
        int okHeight = okBtn.getMeasuredHeight();
        Log.e("OK_WIDTH", String.valueOf(okWidth));
        Log.e("OK_HEIGHT", String.valueOf(okHeight));

        int okLeft = okBtn.getLeft();
        int okTop = okBtn.getTop();

        Log.e("okLeft", String.valueOf(okLeft));
        Log.e("okTop", String.valueOf(okTop));

        int distance = okWidth;

        Log.e("distance", String.valueOf(distance));

        final int[] point = new int[2];
        okBtn.getLocationInWindow(point);

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

            RotativeButton rotativeButton = buttonList.get(i);
            int position = rotativeButton.getPosition();
            int buttonHeight = rotativeButton.getBackground()
                    .getIntrinsicHeight();
            int buttonWidth = rotativeButton.getBackground()
                    .getIntrinsicWidth();
            int diffHeight = (okHeight / 2) - (buttonHeight / 2);
            int diffWidth = (okWidth / 2) - (buttonWidth / 2);

            float newAngle = (float) angle * position;

            Point nextPoint = nextPosition(okTop + diffHeight, okLeft
                    + diffWidth, newAngle, distance);
            int newLeft = nextPoint.x;
            int newTop = nextPoint.y;

            final RectF okRect = new RectF(point[0] - newLeft
                    - okBtn.getWidth() + diffWidth, point[1] - newTop
                    - okBtn.getHeight() + diffHeight, (point[0] - newLeft)
                    + okBtn.getWidth() + diffWidth, (point[1] - newTop)
                    + okBtn.getHeight() + diffHeight);

            rotativeButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    final RotativeButton clickedButton = (RotativeButton) v;

                    int position = clickedButton.getPosition();
                    if (position == 0) {
                        return;
                    }
                    int total = _buttonList.size();
                    final int clickedPosition = total - position;
                    double angle = 360 / total;
                    titleTv.setVisibility(INVISIBLE);

                    for (int i = 0; i < total; i++) {
                        final RotativeButton currentButton = _buttonList.get(i);
                        position = currentButton.getPosition();
                        RectF rectF = _rectForPosition.get(position);
                        float startAngle = (float) angle * position;

                        Path aPath = new Path();
                        aPath.addArc(rectF, startAngle, (float) angle
                                * clickedPosition);

                        Animation anim = new PathAnimation(aPath);
                        anim.setDuration(500);
                        anim.setAnimationListener(new AnimationListener() {

                            public void onAnimationStart(Animation animation) {                             
                                currentButton.setClickable(false);
                                okBtn.setClickable(false);

                            }

                            public void onAnimationRepeat(Animation animation) {
                                // TODO Auto-generated method stub

                            }

                            public void onAnimationEnd(Animation animation) {
                                currentButton.setClickable(true);
                                okBtn.setClickable(true);
                                updatePosition(currentButton, clickedPosition);
                                currentButton.clearAnimation();
                            }
                        });
                        anim.setFillBefore(true);
                        currentButton.startAnimation(anim);

                    }
                }
            });

            LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.topMargin = newTop;
            params.leftMargin = newLeft;
            addView(rotativeButton, params);

            if (position == 0) {
                applyLabel(rotativeButton.getLabel());
            }

            _pointForPosition.put(position, nextPoint);
            _rectForPosition.put(position, okRect);
        }

    }

    public  void updatePosition(final RotativeButton currentButton, int diffPosition){
        int total = _buttonList.size();
        int currentPosition = currentButton.getPosition();
        int newPosition = currentPosition + diffPosition;
        if(newPosition >= total ){
            newPosition -= total;
        }
        currentButton.setPosition(newPosition);

        Point pointForPosition = _pointForPosition.get(newPosition);
        int newLeft = pointForPosition.x;
        int newTop =  pointForPosition.y;

        Log.d("CustomView", "Button: "+ currentButton.getPosition()+ "("+currentButton.getLabel()+") , New Left:" +newLeft+" top: "+ newTop);


        currentButton.layout(newLeft, newTop, 
                newLeft + currentButton.getMeasuredWidth(), newTop + currentButton.getMeasuredHeight());

        Log.d("CustomView", "Moving from:" +currentPosition+" to: "+currentButton.getPosition() + "code: " +currentButton.getLabel());

        if (newPosition == 0) {
            applyLabel(currentButton.getLabel());
        }
    }

    protected void applyLabel( String label) {
        titleTv.setText(label.toUpperCase());
        titleTv.setVisibility(VISIBLE);
    }

    private Point nextPosition(int okTop, int okLeft, double angle, int distance) {

        int x = okLeft + (int) (distance*Math.cos(Math.toRadians(angle)));
        int y = okTop + (int) (distance*Math.sin(Math.toRadians(angle)));
        Log.e("X", String.valueOf(x));
        Log.e("Y", String.valueOf(y));
        Point p = new Point();
        p.set(x, y);
        return p;
    }

    private RotativeButton getActiveButton()
    {
        for (RotativeButton aButton : _buttonList)
        {
            int position = aButton.getPosition();
            if(position == 0)
            {
                return aButton;
            }

        }
        return null;
    }
}
  

PathAnimation.java

public class PathAnimation extends Animation {
    private PathMeasure measure;
    private float[] pos = new float[2];

    public PathAnimation(Path path) {
        measure = new PathMeasure(path, false);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t){
        measure.getPosTan(measure.getLength() * interpolatedTime, pos,null);
        t.getMatrix().setTranslate(pos[0], pos[1]);
    }
}

0 个答案:

没有答案