Skip to content

Commit 56e4e34

Browse files
committed
add SimpleCirView
1 parent 7e576fa commit 56e4e34

File tree

5 files changed

+132
-22
lines changed

5 files changed

+132
-22
lines changed

app/src/main/java/com/hackplan/androidarcmenu/demo/MainActivity.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.hackplan.androidarcmenu.demo;
22

3+
import android.graphics.Color;
34
import android.os.Bundle;
45
import android.support.v7.app.AppCompatActivity;
56
import android.support.v7.widget.LinearLayoutManager;
@@ -10,6 +11,7 @@
1011

1112
import com.hackplan.androidarcmenu.ArcButton;
1213
import com.hackplan.androidarcmenu.ArcMenu;
14+
import com.hackplan.androidarcmenu.SimpleCirView;
1315

1416
public class MainActivity extends AppCompatActivity implements ArcMenu.OnClickMenuListener,
1517
View.OnLongClickListener{
@@ -43,8 +45,12 @@ protected void onCreate(Bundle savedInstanceState) {
4345
.addBtn(R.drawable.w, 6)
4446
.addBtn(R.drawable.w, 6)
4547
.addBtn(R.drawable.w, 6)
46-
.addBtn(R.drawable.w, 6)
47-
.addBtns(new ArcButton.Builder(menuBtn, 2))
48+
.addBtns(new ArcButton.Builder(menuBtn, 2),
49+
new ArcButton.Builder(new SimpleCirView(this)
50+
.setText("2")
51+
.setCirColor(Color.parseColor("#03A9F4"))
52+
.setTextColor(Color.WHITE),
53+
3))
4854
.setListener(MainActivity.this)
4955
.showOnTouch(btn2)
5056
.hideOnTouchUp(true)

app/src/main/java/com/hackplan/androidarcmenu/demo/MyAdapter.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.content.Context;
5+
import android.graphics.Color;
56
import android.support.v4.view.ViewPager;
67
import android.support.v7.widget.RecyclerView;
78
import android.view.LayoutInflater;
@@ -11,7 +12,9 @@
1112
import android.widget.TextView;
1213
import android.widget.Toast;
1314

15+
import com.hackplan.androidarcmenu.ArcButton;
1416
import com.hackplan.androidarcmenu.ArcMenu;
17+
import com.hackplan.androidarcmenu.SimpleCirView;
1518

1619
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
1720
private String[] mDataset;
@@ -32,6 +35,10 @@ public MyAdapter(String[] myDataset, Activity activity, ArcMenu.OnClickMenuListe
3235
.setId(ARC_MENU_ID_3)
3336
.addBtn(R.drawable.a, 0)
3437
.addBtn(R.drawable.r, 1)
38+
.addBtns(new ArcButton.Builder(new SimpleCirView(activity)
39+
.setText("66")
40+
.setCirColor(Color.parseColor("#2196F3")),
41+
3))
3542
.setListener(listener)
3643
.hideOnTouchUp(false);
3744
builder.build();
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.hackplan.androidarcmenu;
22

33
import android.content.Context;
4+
import android.support.annotation.ColorInt;
45
import android.support.annotation.DrawableRes;
6+
import android.text.TextUtils;
57
import android.util.AttributeSet;
68
import android.view.View;
79
import android.widget.ImageView;
@@ -28,44 +30,35 @@ public ArcButton(Context context, AttributeSet attrs, int defStyleAttr) {
2830
public static class Builder {
2931
@DrawableRes private int resId = -1;
3032
private int id;
31-
private int backgroundColor = -1;
32-
private String text;
3333
private View customView;
3434

35-
public Builder(@DrawableRes int resId, int id) {
35+
Builder(@DrawableRes int resId, int id) {
3636
this.resId = resId;
3737
this.id = id;
3838
}
3939

40-
/**
41-
* NOT COMPLETED
42-
* @param text
43-
* @param backgroundColor
44-
* @param id
45-
*/
46-
private Builder(String text, int backgroundColor, int id) {
47-
this.text = text;
48-
this.backgroundColor = backgroundColor;
49-
this.id = id;
50-
}
51-
5240
public Builder(View customView, int id) {
5341
this.customView = customView;
5442
this.id = id;
5543
}
5644

5745
View getButton(Context context) {
46+
View btnView = null;
5847
if (customView != null) {
59-
customView.setTag(id);
60-
return customView;
48+
btnView = customView;
6149

6250
} else if (resId != -1) {
6351
ImageView arcButton = new ImageView(context);
6452
arcButton.setImageResource(resId);
65-
arcButton.setTag(id);
66-
return arcButton;
53+
btnView = arcButton;
54+
}
55+
56+
if (btnView == null) {
57+
throw new RuntimeException("ArcButton does not init correctly");
58+
} else {
59+
btnView.setTag(id);
60+
return btnView;
6761
}
68-
throw new RuntimeException("ArcButton does not init correctly");
6962
}
7063
}
7164
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.hackplan.androidarcmenu;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Color;
6+
import android.graphics.Paint;
7+
import android.support.annotation.ColorInt;
8+
import android.util.AttributeSet;
9+
import android.util.TypedValue;
10+
import android.view.View;
11+
12+
/**
13+
* Created by Dacer on 19/11/2016.
14+
*/
15+
16+
public class SimpleCirView extends View {
17+
18+
private Paint bgPaint, textPaint;
19+
private int radiusInPx;
20+
private String textStr;
21+
22+
private final static int DEFAULT_BG_COLOR = Color.parseColor("#03A9F4");
23+
private final static int DEFAULT_TEXT_COLOR = Color.WHITE;
24+
private final static int DEFAULT_TEXT_SIZE_IN_SP = 22;
25+
26+
public SimpleCirView(Context context) {
27+
this(context, null);
28+
}
29+
30+
public SimpleCirView(Context context, AttributeSet attrs) {
31+
this(context, attrs, 0);
32+
}
33+
34+
public SimpleCirView(Context context, AttributeSet attrs, int defStyleAttr) {
35+
super(context, attrs, defStyleAttr);
36+
init(context);
37+
}
38+
39+
public SimpleCirView setText(String text) {
40+
textStr = text;
41+
postInvalidate();
42+
return this;
43+
}
44+
45+
public SimpleCirView setTextColor(@ColorInt int color) {
46+
textPaint.setColor(color);
47+
postInvalidate();
48+
return this;
49+
}
50+
51+
public SimpleCirView setTextSizeInSp(int sizeInSp) {
52+
textPaint.setTextSize(spToPx(sizeInSp));
53+
postInvalidate();
54+
return this;
55+
}
56+
57+
public SimpleCirView setTextSizeInPx(int sizeInPx) {
58+
textPaint.setTextSize(sizeInPx);
59+
postInvalidate();
60+
return this;
61+
}
62+
63+
public SimpleCirView setBackgroundRadiusInPx(int radiusInPx) {
64+
this.radiusInPx = radiusInPx;
65+
postInvalidate();
66+
return this;
67+
}
68+
69+
public SimpleCirView setCirColor(@ColorInt int color) {
70+
bgPaint.setColor(color);
71+
postInvalidate();
72+
return this;
73+
}
74+
75+
private void init(Context context) {
76+
radiusInPx = context.getResources().getDimensionPixelSize(R.dimen.default_simple_cir_view_radius);
77+
bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
78+
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
79+
80+
bgPaint.setColor(DEFAULT_BG_COLOR);
81+
textPaint.setColor(DEFAULT_TEXT_COLOR);
82+
textPaint.setTextSize(spToPx(DEFAULT_TEXT_SIZE_IN_SP));
83+
textPaint.setTextAlign(Paint.Align.CENTER);
84+
85+
}
86+
87+
@Override
88+
protected void onDraw(Canvas canvas) {
89+
canvas.drawCircle(getWidth()/2, getHeight()/2, radiusInPx, bgPaint);
90+
int xPos = (canvas.getWidth() / 2);
91+
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
92+
canvas.drawText(textStr, xPos, yPos, textPaint);
93+
}
94+
95+
@Override
96+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
97+
setMeasuredDimension(radiusInPx * 2, radiusInPx * 2);
98+
}
99+
100+
private int spToPx(float sp) {
101+
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getContext().getResources().getDisplayMetrics());
102+
}
103+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<resources>
22
<dimen name="default_radius">80dp</dimen>
3+
<dimen name="default_simple_cir_view_radius">22dp</dimen>
34
</resources>

0 commit comments

Comments
 (0)