在网上找了一个旋转式菜单的源码,运行效果如下运行效果正常,其onMeasure,onLayout,以及控制旋转的方法如下
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mMaxChildWidth = 0;
mMaxChildHeight = 0;
// Measure once to find the maximum child size.
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.AT_MOST);
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
mMaxChildWidth = Math.max(mMaxChildWidth, child.getMeasuredWidth());
mMaxChildHeight = Math.max(mMaxChildHeight,
child.getMeasuredHeight());
System.out.println(mMaxChildWidth + "----------" + mMaxChildHeight
+ "----------" + count + "");
}
// Measure again for each child to be exactly the same size.
childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxChildWidth,
MeasureSpec.EXACTLY);
childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxChildHeight,
MeasureSpec.EXACTLY);
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
setMeasuredDimension(resolveSize(mMaxChildWidth, widthMeasureSpec),
resolveSize(mMaxChildHeight, heightMeasureSpec));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int layoutWidth = r - l;
int layoutHeight = b - t;
// Laying out the child views
final int childCount = getChildCount();
int left, top;
radius = (layoutWidth <= layoutHeight) ? layoutWidth / 3
: layoutHeight / 3;
childWidth = (int) (radius / 1.5);
childHeight = (int) (radius / 1.5);
float angleDelay = 360 / getChildCount();
for (int i = 0; i < childCount; i++) {
final CircleImageView child = (CircleImageView) CircleLayout.this
.getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
if (angle > 360) {
angle -= 360;
} else {
if (angle < 0) {
angle += 360;
}
}
child.setAngle(angle);
child.setPosition(i);
left = Math
.round((float) (((layoutWidth / 2) - childWidth / 2) + radius
* Math.cos(Math.toRadians(angle))));
top = Math
.round((float) (((layoutHeight / 2) - childHeight / 2) + radius
* Math.sin(Math.toRadians(angle))));
child.layout(left, top, left + childWidth, top + childHeight);
System.out.println(child.toString() + "onlayout");
angle += angleDelay;
}
}
/**
* Rotate the buttons.
*
* @param degrees
* The degrees, the menu items should get rotated.
*/
private void rotateButtons(float degrees) {
int left, top, childCount = getChildCount();
float angleDelay = 360 / childCount;
angle += degrees;
if (angle > 360) {
angle -= 360;
} else {
if (angle < 0) {
angle += 360;
}
}
for (int i = 0; i < childCount; i++) {
if (angle > 360) {
angle -= 360;
} else {
if (angle < 0) {
angle += 360;
}
}
final CircleImageView child = (CircleImageView) CircleLayout.this
.getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
left = Math
.round((float) (((circleWidth / 2) - childWidth / 2) + radius
* Math.cos(Math.toRadians(angle))));
top = Math
.round((float) (((circleHeight / 2) - childHeight / 2) + radius
* Math.sin(Math.toRadians(angle))));
child.setAngle(angle);
if (Math.abs(angle - firstChildPos) < (angleDelay / 2)
&& selected != child.getPosition()) {
selected = child.getPosition();
if (mOnItemSelectedListener != null && rotateToCenter) {
mOnItemSelectedListener.onItemSelected(child, selected,
child.getId(), child.getName());
}
}
child.layout(left, top, left + childWidth, top + childHeight);
System.out.println(child.toString() + "rorate");
angle += angleDelay;
}
}
xml布局文件如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:circle="http://schemas.android.com/apk/res/com.szugyi.circlemenu"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.szugyi.circlemenu.view.CircleLayout
android:id="@+id/main_circle_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/main_selected_textView"
android:layout_gravity="center_horizontal"
circle:firstChildPosition="South"
circle:rotateToCenter="true"
circle:isRotating="true" >
<com.szugyi.circlemenu.view.CircleImageView
android:id="@+id/main_facebook_image"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@drawable/icon_facebook"
circle:name="@string/facebook" />
<com.szugyi.circlemenu.view.CircleImageView
android:id="@+id/main_myspace_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_myspace"
circle:name="@string/myspace" />
<com.szugyi.circlemenu.view.CircleImageView
android:id="@+id/main_google_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_google"
circle:name="@string/google" />
<com.szugyi.circlemenu.view.CircleImageView
android:id="@+id/main_linkedin_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_linkedin"
circle:name="@string/linkedin" />
<com.szugyi.circlemenu.view.CircleImageView
android:id="@+id/main_twitter_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_twitter"
circle:name="@string/twitter" />
<com.szugyi.circlemenu.view.CircleImageView
android:id="@+id/main_wordpress_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_wordpress"
circle:name="@string/wordpress" />
</com.szugyi.circlemenu.view.CircleLayout>
<TextView
android:id="@+id/main_selected_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
我要把这个旋转菜单通过点击地图上的Marker打开,使用百度地图的InfoWindow类添加,java文件都没有改,自己写了一个xml文件circle_rotate_layout.xml,也与demo里的类似,点击Marker添加菜单的代码如下:
mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
ViewGroup view = new RelativeLayout(
getApplicationContext());
LayoutInflater lf1 = getLayoutInflater();
view = (ViewGroup) lf1.inflate(
R.layout.circle_rotate_layout, null);
CircleLayout cl = (CircleLayout) view
.findViewById(R.id.circle_layout);
cl.setOnItemSelectedListener(MainActivity.this);
cl.setOnItemClickListener(MainActivity.this);
// OnInfoWindowClickListener listener = null;
for (int i = 0; i < markerlist.size(); i++) {
if (marker == markerlist.get(i)) {
LatLng ll = marker.getPosition();
mInfoWindow = new InfoWindow(view, ll, 0);
mBaiduMap.showInfoWindow(mInfoWindow);
}
}
return false;
}
});
但是这样的效果却是![图片说明](https://img-ask.csdn.net/upload/201611/11/1478853712_785359.png)![图片说明](https://img-ask.csdn.net/upload/201611/11/1478853737_732981.png)
菜单一直很小,怎么都弄不大,而且onLayout方法确定位置绘制粗始菜单项之后,后面rotateButtons旋转菜单时,刚开始绘制的菜单项会一直存在,旋转的相当于是另一组菜单项,请问大神们该怎么解决这两个问题