Skip to content

Commit 9e67b57

Browse files
author
d.tarasov
committed
Added ability configure ArcMenu fromDegrees, toDegrees and childSize attributes via XML
1 parent 6abe47d commit 9e67b57

File tree

6 files changed

+90
-30
lines changed

6 files changed

+90
-30
lines changed

library/res/values/attrs.xml

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<resources>
3+
<attr name="fromDegrees" format="float|reference" />
4+
<attr name="toDegrees" format="float|reference" />
5+
<attr name="childSize" format="dimension|reference" />
6+
37
<declare-styleable name="ArcLayout">
4-
<attr name="fromDegrees" format="float" />
5-
<attr name="toDegrees" format="float" />
6-
<attr name="childSize" format="dimension" />
8+
<attr name="fromDegrees"/>
9+
<attr name="toDegrees"/>
10+
<attr name="childSize"/>
711
</declare-styleable>
12+
13+
<declare-styleable name="ArcMenu">
14+
<attr name="fromDegrees"/>
15+
<attr name="toDegrees"/>
16+
<attr name="childSize"/>
17+
</declare-styleable>
818

919
<declare-styleable name="RayLayout">
1020
<attr name="leftHolderWidth" format="dimension" />

library/src/com/capricorn/ArcLayout.java

+4
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ public void setChildSize(int size) {
273273
requestLayout();
274274
}
275275

276+
public int getChildSize() {
277+
return mChildSize;
278+
}
279+
276280
/**
277281
* switch between expansion and shrinkage
278282
*

library/src/com/capricorn/ArcMenu.java

+18
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.capricorn;
1818

1919
import android.content.Context;
20+
import android.content.res.TypedArray;
2021
import android.util.AttributeSet;
2122
import android.view.LayoutInflater;
2223
import android.view.MotionEvent;
@@ -52,6 +53,7 @@ public ArcMenu(Context context) {
5253
public ArcMenu(Context context, AttributeSet attrs) {
5354
super(context, attrs);
5455
init(context);
56+
applyAttrs(attrs);
5557
}
5658

5759
private void init(Context context) {
@@ -78,6 +80,22 @@ public boolean onTouch(View v, MotionEvent event) {
7880
mHintView = (ImageView) findViewById(R.id.control_hint);
7981
}
8082

83+
private void applyAttrs(AttributeSet attrs) {
84+
if (attrs != null) {
85+
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ArcLayout, 0, 0);
86+
87+
float fromDegrees = a.getFloat(R.styleable.ArcLayout_fromDegrees, ArcLayout.DEFAULT_FROM_DEGREES);
88+
float toDegrees = a.getFloat(R.styleable.ArcLayout_toDegrees, ArcLayout.DEFAULT_TO_DEGREES);
89+
mArcLayout.setArc(fromDegrees, toDegrees);
90+
91+
int defaultChildSize = mArcLayout.getChildSize();
92+
int newChildSize = a.getDimensionPixelSize(R.styleable.ArcLayout_childSize, defaultChildSize);
93+
mArcLayout.setChildSize(newChildSize);
94+
95+
a.recycle();
96+
}
97+
}
98+
8199
public void addItem(View item, OnClickListener listener) {
82100
mArcLayout.addView(item);
83101
item.setOnClickListener(getItemClickListener(listener));

sample/res/layout/main.xml

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<ScrollView
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:arc="http://schemas.android.com/apk/res-auto"
35
android:layout_width="fill_parent"
4-
android:layout_height="fill_parent"
5-
android:orientation="vertical" >
6+
android:layout_height="fill_parent">
67

7-
<com.capricorn.RayMenu
8-
android:id="@+id/ray_menu"
8+
<LinearLayout
99
android:layout_width="fill_parent"
10-
android:layout_height="60dp"
11-
android:paddingLeft="5dp"
12-
android:paddingRight="5dp" />
10+
android:layout_height="wrap_content"
11+
android:orientation="vertical">
1312

14-
<com.capricorn.ArcMenu
15-
android:id="@+id/arc_menu"
16-
android:layout_width="wrap_content"
17-
android:layout_height="wrap_content" />
13+
<com.capricorn.RayMenu
14+
android:id="@+id/ray_menu"
15+
android:layout_width="fill_parent"
16+
android:layout_height="60dp"
17+
android:paddingLeft="5dp"
18+
android:paddingRight="5dp"/>
1819

19-
</LinearLayout>
20+
<com.capricorn.ArcMenu
21+
android:id="@+id/arc_menu"
22+
android:layout_width="wrap_content"
23+
android:layout_height="wrap_content"/>
24+
25+
<com.capricorn.ArcMenu
26+
android:id="@+id/arc_menu_2"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
arc:fromDegrees="@dimen/menuFromDegrees"
30+
arc:toDegrees="@dimen/menuToDegrees"
31+
arc:childSize="@dimen/menuChildSize"/>
32+
33+
</LinearLayout>
34+
</ScrollView>

sample/res/values/dimens.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<item name="menuFromDegrees" format="float" type="dimen">180.0</item>
4+
<item name="menuToDegrees" format="float" type="dimen">360.0</item>
5+
<dimen name="menuChildSize">30dp</dimen>
6+
</resources>

sample/src/com/capricorn/example/MainActivity.java

+21-14
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,13 @@ public void onCreate(Bundle savedInstanceState) {
4242
setContentView(R.layout.main);
4343

4444
ArcMenu arcMenu = (ArcMenu) findViewById(R.id.arc_menu);
45+
ArcMenu arcMenu2 = (ArcMenu) findViewById(R.id.arc_menu_2);
4546

46-
final int itemCount = ITEM_DRAWABLES.length;
47-
for (int i = 0; i < itemCount; i++) {
48-
ImageView item = new ImageView(this);
49-
item.setImageResource(ITEM_DRAWABLES[i]);
50-
51-
final int position = i;
52-
arcMenu.addItem(item, new OnClickListener() {
53-
54-
@Override
55-
public void onClick(View v) {
56-
Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
57-
}
58-
});// Add a menu item
59-
}
47+
initArcMenu(arcMenu, ITEM_DRAWABLES);
48+
initArcMenu(arcMenu2, ITEM_DRAWABLES);
6049

6150
RayMenu rayMenu = (RayMenu) findViewById(R.id.ray_menu);
51+
final int itemCount = ITEM_DRAWABLES.length;
6252
for (int i = 0; i < itemCount; i++) {
6353
ImageView item = new ImageView(this);
6454
item.setImageResource(ITEM_DRAWABLES[i]);
@@ -73,4 +63,21 @@ public void onClick(View v) {
7363
});// Add a menu item
7464
}
7565
}
66+
67+
private void initArcMenu(ArcMenu menu, int[] itemDrawables) {
68+
final int itemCount = itemDrawables.length;
69+
for (int i = 0; i < itemCount; i++) {
70+
ImageView item = new ImageView(this);
71+
item.setImageResource(itemDrawables[i]);
72+
73+
final int position = i;
74+
menu.addItem(item, new OnClickListener() {
75+
76+
@Override
77+
public void onClick(View v) {
78+
Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
79+
}
80+
});
81+
}
82+
}
7683
}

0 commit comments

Comments
 (0)