Skip to content

Commit

Permalink
1、修复在ScrollView等可滑动的ViewGroup中无法触发手势旋转的问题
Browse files Browse the repository at this point in the history
2、新增LayerLineColor、LayerLineWidth、VertexLineColor、VertexLineWidth属性,可单独设置雷达图顶点连线和各层连线的颜色与宽度,弃用RadarLineEnable、RadarLineWidth、RadarLineColor
3、RadarData新增setLineWidth用来设置数据区多边形外框线宽度
  • Loading branch information
rorbin committed Oct 12, 2017
1 parent 88e7db9 commit 79de48e
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,37 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
mRadarView = (RadarView) findViewById(R.id.radarView);

mRadarView.setEmptyHint("无数据");

List<Integer> layerColor = new ArrayList<>();
Collections.addAll(layerColor, 0x3300bcd4, 0x3303a9f4, 0x335677fc, 0x333f51b5, 0x33673ab7);
mRadarView.setLayerColor(layerColor);

List<String> vertexText = new ArrayList<>();
Collections.addAll(vertexText, "力量", "敏捷", "速度", "智力", "精神", "耐力", "体力", "魔力", "意志", "幸运");
mRadarView.setVertexText(vertexText);

List<Integer> res = new ArrayList<>();
Collections.addAll(res, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher);
Collections.addAll(res, R.mipmap.power, R.mipmap.agile, R.mipmap.speed,
R.mipmap.intelligence, R.mipmap.spirit, R.mipmap.endurance,
R.mipmap.strength, R.mipmap.magic, R.mipmap.will, R.mipmap.lucky);
mRadarView.setVertexIconResid(res);

List<Float> values = new ArrayList<>();
Collections.addAll(values, 3f, 6f, 2f, 7f, 5f, 1f, 4f, 3f, 8f, 5f);
Collections.addAll(values, 3f, 6f, 2f, 7f, 5f, 1f, 9f, 3f, 8f, 5f);
RadarData data = new RadarData(values);
data.setValueTextEnable(true);
data.setVauleTextColor(Color.WHITE);
mRadarView.addData(data);

List<Float> values2 = new ArrayList<>();
Collections.addAll(values2, 7f, 1f, 4f, 2f, 8f, 3f, 9f, 6f, 5f, 3f);
Collections.addAll(values2, 7f, 1f, 4f, 2f, 8f, 3f, 4f, 6f, 5f, 3f);
RadarData data2 = new RadarData(values2);
mRadarView.addData(data);
data2.setValueTextEnable(true);
data2.setVauleTextColor(Color.WHITE);
data2.setValueTextSize(dp2px(10));
data2.setLineWidth(dp2px(1));
mRadarView.addData(data2);
}

Expand Down Expand Up @@ -80,4 +89,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return super.onOptionsItemSelected(item);
}

private float dp2px(float dpValue) {
final float scale = getResources().getDisplayMetrics().density;
return dpValue * scale + 0.5f;
}
}
10 changes: 7 additions & 3 deletions RadarViewDemo/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
<rorbin.q.radarview.RadarView
android:id="@+id/radarView"
android:layout_width="match_parent"
app:vertex_icon_size="30dp"
app:vertex_text_offset="30dp"
android:layout_height="match_parent"
app:vertex_icon_margin="10dp"
app:vertex_icon_position="top"
android:layout_height="match_parent"/>
app:vertex_icon_size="30dp"
app:vertex_line_color="#99FFFFFF"
app:layer_line_color="#99FFFFFF"
app:vertex_text_color="#FFFFFF"
app:vertex_text_offset="30dp" />
</RelativeLayout>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion radarviewlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ publish {
userOrg = 'qstumn'
groupId = 'q.rorbin'
artifactId = 'RadarView'
publishVersion = '1.0.2'
publishVersion = '1.0.3'
desc = 'this is a can rotate radar chart for android'
website = 'https://github.com/qstumn/RadarView'
}
Expand Down
22 changes: 18 additions & 4 deletions radarviewlib/src/main/java/rorbin/q/radarview/RadarData.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ public class RadarData {
private List<Float> mValue;
private int mColor;
private List<String> mValueText;
private float mLineWidth;
private int mVauleTextColor;
private int mValueTextSize;
private float mValueTextSize;
private boolean mValueTextEnable;

public RadarData(List<Float> value) {
Expand All @@ -27,13 +28,18 @@ public RadarData(List<Float> value, String label) {
this(label, value, new RandomColor().randomColor());
}

public RadarData(List<Float> value, int color) {
this("data", value, color);
}

public RadarData(String label, List<Float> value, int color) {
this.mLabel = label;
this.mValue = value;
this.mColor = color;
initValueText();
mVauleTextColor = 0xFF9E9E9E;
mValueTextSize = 10;
mValueTextSize = 30;
mLineWidth = 1;
mValueTextEnable = false;
}

Expand Down Expand Up @@ -70,11 +76,11 @@ public void setVauleTextColor(int mVauleTextColor) {
this.mVauleTextColor = mVauleTextColor;
}

public int getValueTextSize() {
public float getValueTextSize() {
return mValueTextSize;
}

public void setValueTextSize(int mValueTextSize) {
public void setValueTextSize(float mValueTextSize) {
this.mValueTextSize = mValueTextSize;
}

Expand All @@ -94,6 +100,14 @@ public void setValueText(List<String> mValueText) {
this.mValueText = mValueText;
}

public float getLineWidth() {
return mLineWidth;
}

public void setLineWidth(float width) {
this.mLineWidth = width;
}

private void initValueText() {
mValueText = new ArrayList<>();
for (int i = 0; i < mValue.size(); i++) {
Expand Down
154 changes: 111 additions & 43 deletions radarviewlib/src/main/java/rorbin/q/radarview/RadarView.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ public class RadarView extends View {

private float mRadius;
private PointF mPointCenter;
private int mRadarLineColor;
private float mRadarLineWidth;
private boolean mRadarLineEnable;
private int mLayerLineColor;
private float mLayerLineWidth;
private int mVertexLineColor;
private float mVertexLineWidth;

private int mLayer;
private List<Integer> mLayerColor;
private float mMaxValue;
Expand Down Expand Up @@ -118,14 +120,15 @@ private void initAttrs(AttributeSet attrs) {
mRotationEnable = typedArray.getBoolean(R.styleable.RadarView_rotation_enable, true);
mWebMode = typedArray.getInt(R.styleable.RadarView_web_mode, WEB_MODE_POLYGON);
mMaxValue = typedArray.getFloat(R.styleable.RadarView_max_value, 0);
mRadarLineColor = typedArray.getColor(R.styleable.RadarView_radar_line_color, 0xFF9E9E9E);
mRadarLineEnable = typedArray.getBoolean(R.styleable.RadarView_radar_line_enable, true);
mRadarLineWidth = typedArray.getDimension(R.styleable.RadarView_radar_line_width, dp2px(1));
mVertexTextColor = typedArray.getColor(R.styleable.RadarView_vertex_text_color, mRadarLineColor);
mLayerLineColor = typedArray.getColor(R.styleable.RadarView_layer_line_color, 0xFF9E9E9E);
mLayerLineWidth = typedArray.getDimension(R.styleable.RadarView_layer_line_width, dp2px(1));
mVertexLineColor = typedArray.getColor(R.styleable.RadarView_vertex_line_color, 0xFF9E9E9E);
mVertexLineWidth = typedArray.getDimension(R.styleable.RadarView_vertex_line_width, dp2px(1));
mVertexTextColor = typedArray.getColor(R.styleable.RadarView_vertex_text_color, mVertexLineColor);
mVertexTextSize = typedArray.getDimension(R.styleable.RadarView_vertex_text_size, dp2px(12));
mVertexTextOffset = typedArray.getDimension(R.styleable.RadarView_vertex_text_offset, 0);
mCenterTextColor = typedArray.getColor(R.styleable.RadarView_center_text_color, mRadarLineColor);
mCenterTextSize = typedArray.getDimension(R.styleable.RadarView_center_text_size, dp2px(18));
mCenterTextColor = typedArray.getColor(R.styleable.RadarView_center_text_color, mVertexLineColor);
mCenterTextSize = typedArray.getDimension(R.styleable.RadarView_center_text_size, dp2px(30));
mCenterText = typedArray.getString(R.styleable.RadarView_center_text);
mVertexIconSize = typedArray.getDimension(R.styleable.RadarView_vertex_icon_size, dp2px(20));
mVertexIconPosition = typedArray.getInt(R.styleable.RadarView_vertex_icon_position, VERTEX_ICON_POSITION_TOP);
Expand Down Expand Up @@ -163,9 +166,13 @@ private void init() {
mVertexTextPaint = new TextPaint();
mValueTextPaint = new TextPaint();
mCenterTextPaint = new TextPaint();

mRadarLinePaint.setAntiAlias(true);
mLayerPaint.setAntiAlias(true);
mVertexTextPaint.setAntiAlias(true);
mCenterTextPaint.setAntiAlias(true);
mValueTextPaint.setAntiAlias(true);
mValuePaint.setAntiAlias(true);
mValueTextPaint.setFakeBoldText(true);

mVertexIconRect = new RectF();
Expand Down Expand Up @@ -198,33 +205,6 @@ public void setWebMode(int webMode) {
invalidate();
}

public int getRadarLineColor() {
return mRadarLineColor;
}

public void setRadarLineColor(int radarLineColor) {
this.mRadarLineColor = radarLineColor;
invalidate();
}

public float getRadarLineWidth() {
return mRadarLineWidth;
}

public void setRadarLineWidth(float radarLineWidth) {
this.mRadarLineWidth = radarLineWidth;
invalidate();
}

public boolean isRadarLineEnable() {
return mRadarLineEnable;
}

public void setRadarLineEnable(boolean radarLineEnable) {
this.mRadarLineEnable = radarLineEnable;
invalidate();
}

public int getLayer() {
return mLayer;
}
Expand Down Expand Up @@ -406,6 +386,42 @@ public void setRotationEnable(boolean enable) {
this.mRotationEnable = enable;
}

public int getLayerLineColor() {
return mLayerLineColor;
}

public void setLayerLineColor(int color) {
this.mLayerLineColor = color;
invalidate();
}

public float getLayerLineWidth() {
return mLayerLineWidth;
}

public void setLayerLineWidth(float width) {
this.mLayerLineWidth = width;
invalidate();
}

public int getVertexLineColor() {
return mVertexLineColor;
}

public void setVertexLineColor(int color) {
this.mVertexLineColor = color;
invalidate();
}

public float getVertexLineWidth() {
return mVertexLineWidth;
}

public void setVertexLineWidth(float width) {
this.mVertexLineWidth = width;
invalidate();
}

public void animeValue(int duration) {
for (RadarData radarData : mRadarData) {
animeValue(duration, radarData);
Expand Down Expand Up @@ -498,12 +514,9 @@ protected void onDraw(Canvas canvas) {
}

private void initPaint() {
mRadarLinePaint.setStrokeWidth(mRadarLineWidth);
mRadarLinePaint.setColor(mRadarLineColor);
mRadarLinePaint.setStyle(Paint.Style.STROKE);
mVertexTextPaint.setColor(mVertexTextColor);
mVertexTextPaint.setTextSize(mVertexTextSize);
mValuePaint.setStrokeWidth(dp2px(1));
mLayerPaint.setStyle(Paint.Style.FILL);
mCenterTextPaint.setTextSize(mCenterTextSize);
mCenterTextPaint.setColor(mCenterTextColor);
Expand Down Expand Up @@ -539,7 +552,9 @@ private void drawWeb(Canvas canvas) {
mLayerPaint.setColor(layerColor);
canvas.drawPath(mRadarPath, mLayerPaint);
}
if (mRadarLineEnable) {
if (mLayerLineWidth > 0) {
mRadarLinePaint.setColor(mLayerLineColor);
mRadarLinePaint.setStrokeWidth(mLayerLineWidth);
canvas.drawPath(mRadarPath, mRadarLinePaint);
}
}
Expand All @@ -553,7 +568,9 @@ private void drawCircle(Canvas canvas) {
mLayerPaint.setColor(layerColor);
canvas.drawCircle(mPointCenter.x, mPointCenter.y, radius, mLayerPaint);
}
if (mRadarLineEnable) {
if (mLayerLineWidth > 0) {
mRadarLinePaint.setColor(mLayerLineColor);
mRadarLinePaint.setStrokeWidth(mLayerLineWidth);
canvas.drawCircle(mPointCenter.x, mPointCenter.y, radius, mRadarLinePaint);
}
}
Expand Down Expand Up @@ -633,19 +650,21 @@ protected void drawVertexImpl(Canvas canvas, String text, Bitmap icon, Paint pai
}

private void drawVertexLine(Canvas canvas, double angleSin, double angleCos) {
if (!mRadarLineEnable) {
if (mVertexLineWidth <= 0) {
return;
}
float x = (float) (mPointCenter.x + angleSin * mRadius);
float y = (float) (mPointCenter.y - angleCos * mRadius);
mRadarLinePaint.setColor(mVertexLineColor);
mRadarLinePaint.setStrokeWidth(mVertexLineWidth);
canvas.drawLine(mPointCenter.x, mPointCenter.y, x, y, mRadarLinePaint);
}

private void drawData(Canvas canvas) {
for (int i = 0; i < mRadarData.size(); i++) {
RadarData radarData = mRadarData.get(i);
mValuePaint.setColor(radarData.getColor());
mValueTextPaint.setTextSize(dp2px(radarData.getValueTextSize()));
mValueTextPaint.setTextSize(radarData.getValueTextSize());
mValueTextPaint.setColor(radarData.getVauleTextColor());
List<Float> values = radarData.getValue();
mRadarPath.reset();
Expand Down Expand Up @@ -681,6 +700,7 @@ private void drawData(Canvas canvas) {
mRadarPath.close();
mValuePaint.setAlpha(255);
mValuePaint.setStyle(Paint.Style.STROKE);
mValuePaint.setStrokeWidth(radarData.getLineWidth());
canvas.drawPath(mRadarPath, mValuePaint);
mValuePaint.setStyle(Paint.Style.FILL);
mValuePaint.setAlpha(150);
Expand Down Expand Up @@ -727,6 +747,14 @@ private void calcRadius() {

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(mRotationEnable);
break;
case MotionEvent.ACTION_UP:
getParent().requestDisallowInterceptTouchEvent(false);
break;
}
if (!mRotationEnable) return super.onTouchEvent(event);
return mDetector.onTouchEvent(event);
}
Expand Down Expand Up @@ -803,4 +831,44 @@ private float dp2px(float dpValue) {
final float scale = mContext.getResources().getDisplayMetrics().density;
return dpValue * scale + 0.5f;
}


@Deprecated
/**
use {@link RadarView#getLayerLineColor()} or {@link RadarView#getVertexLineColor()}
*/
public int getRadarLineColor() {
return -1;
}

@Deprecated
/**
use {@link RadarView#setLayerLineColor()} or {@link RadarView#setVertexLineColor()}
*/
public void setRadarLineColor(int radarLineColor) {
}

@Deprecated
/**
use {@link RadarView#getLayerLineWidth()} or {@link RadarView#getVertexLineWidth()}
*/
public float getRadarLineWidth() {
return -1;
}

@Deprecated
/**
use {@link RadarView#setLayerLineWidth()} or {@link RadarView#setVertexLineWidth()}
*/
public void setRadarLineWidth(float radarLineWidth) {
}

@Deprecated
public boolean isRadarLineEnable() {
return false;
}

@Deprecated
public void setRadarLineEnable(boolean radarLineEnable) {
}
}
Loading

0 comments on commit 79de48e

Please sign in to comment.