Skip to content

Commit

Permalink
完善功能,v1.0版完成
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay-huangjie committed Feb 25, 2019
1 parent a64457e commit 5eacb5d
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 114 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DefaultActivity" />
<activity android:name=".CustomLabelActivity" />
<activity android:name=".CustomHalfWidthActivity" />
</application>

</manifest>
24 changes: 24 additions & 0 deletions app/src/main/java/jie/com/funnel/BaseFunnelActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jie.com.funnel;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import jie.com.funnellib.FunnelView;

/**
* Created by hj on 2019/2/25.
* 说明:
*/
public abstract class BaseFunnelActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_default);
FunnelView funnelVie = findViewById(R.id.funnelView);
initEvent(funnelVie);
}

abstract void initEvent(FunnelView funnelView);
}
42 changes: 42 additions & 0 deletions app/src/main/java/jie/com/funnel/CustomHalfWidthActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jie.com.funnel;

import android.util.TypedValue;

import jie.com.funnellib.FunnelView;
import jie.com.funnellib.HalfWidthCallback;

/**
* Created by hj on 2019/2/25.
* 说明:自定义宽度策略
*/
public class CustomHalfWidthActivity extends BaseFunnelActivity {

@Override
void initEvent(FunnelView funnelView) {
/**
* 自定义宽度策略,也就是漏斗每一层宽度增加多少,这都是可以自定义的,这样有利于适配的灵活性,也可以自定义出
* 很多的效果出来
* 注意事项:绘制是从下往上绘制的,halfWidth返回的当前的下面那个漏斗的宽度,需要注意一下
*
*/
funnelView.setChartData(FunnelData.getTenCountData(), new HalfWidthCallback() {
@Override
public float getHalfStrategy(float halfWidth, int count, int i) {
/**
* 这里定义的策略是前4个宽度不变,后面的逐渐增加10dp,所以呈现了一个真正的漏斗形状
*/
if (i <= 3) {
halfWidth = dp2px(5);
return halfWidth;
} else {
halfWidth += dp2px(10);
return halfWidth;
}
}
});
}

private float dp2px(int dip) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, this.getResources().getDisplayMetrics());
}
}
45 changes: 45 additions & 0 deletions app/src/main/java/jie/com/funnel/CustomLabelActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package jie.com.funnel;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import jie.com.funnellib.CustomLabelCallback;
import jie.com.funnellib.FunnelView;
import jie.com.funnellib.Util;

/**
* Created by hj on 2019/2/25.
* 说明:此Activity演示自定义描述文字
*/
public class CustomLabelActivity extends BaseFunnelActivity {

@Override
void initEvent(FunnelView funnelView) {
final List<FunnelData> data = FunnelData.getFourCountData();

funnelView.addCustomLabelCallback(new CustomLabelCallback() {
@Override
public void drawText(Canvas canvas, Paint mPaintLabel, float labelX, float labelY, int index) {
FunnelData funnelData = data.get(index);
//先画前面的文字
mPaintLabel.setColor(funnelData.color);
mPaintLabel.setFakeBoldText(false);
canvas.drawText(funnelData.getLabel()+":", labelX, labelY, mPaintLabel);
//计算前面文字的长度
float labelWidth = Util.getTextWidth(mPaintLabel,funnelData.getLabel()+":");
mPaintLabel.setColor(Color.parseColor("#333333"));
mPaintLabel.setFakeBoldText(true);
//画后面的文字
canvas.drawText(funnelData.num+"个", labelX + labelWidth, labelY, mPaintLabel);
}
});
funnelView.setChartData(data);
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/jie/com/funnel/DefaultActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jie.com.funnel;


import jie.com.funnellib.FunnelView;

/**
* Created by hj on 2019/2/25.
* 说明:使用默认方式创建一个漏斗
*/
public class DefaultActivity extends BaseFunnelActivity {

@Override
void initEvent(FunnelView funnelView) {
funnelView.setChartData(FunnelData.getTenCountData());
}
}
63 changes: 58 additions & 5 deletions app/src/main/java/jie/com/funnel/FunnelData.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package jie.com.funnel;

import android.graphics.Color;

import java.util.ArrayList;
import java.util.List;

import jie.com.funnellib.IFunnelData;

/**
* Created by hj on 2019/2/22.
* 说明:
*/
public class FunnelData implements IFunnelData {
public String value;
public String label;
public int color;

@Override
public String getValue() {
return value;
}
public String num;

@Override
public int getColor() {
Expand All @@ -25,4 +26,56 @@ public int getColor() {
public String getLabel() {
return label;
}


//-----------------------------测试数据--------------------------------------------------------------
private static int[] colors = new int[]{
Color.parseColor("#FF5656"), Color.parseColor("#FF854B"),
Color.parseColor("#FFB240"), Color.parseColor("#FFEC3D"),
Color.parseColor("#4DD2F9"), Color.parseColor("#52B5FF"),
Color.parseColor("#5882FF"), Color.parseColor("#5959FF"),
Color.parseColor("#8359FF"), Color.parseColor("#AC59FF")
};

private static String[] labels = new String[]{
"Android", "ios",
"php", "c",
"c++", "python",
"golang", "java",
"javascript", ".net"
};


private static int[] fourColors = new int[]{
Color.parseColor("#FF5D5D"), Color.parseColor("#FFB240"),
Color.parseColor("#52B5FF"), Color.parseColor("#5882FF"),
};

private static String[] fourLabel = new String[]{
"数学", "语文", "物理", "化学"
};

public static List<FunnelData> getFourCountData() {
List<FunnelData> data = new ArrayList<>();
for (int i = 0; i < fourLabel.length; i++) {
FunnelData funnelData = new FunnelData();
funnelData.label = fourLabel[i];
funnelData.color = fourColors[i];
funnelData.num = String.valueOf(i);
data.add(funnelData);
}
return data;
}

public static List<FunnelData> getTenCountData() {
List<FunnelData> data = new ArrayList<>();
for (int i = labels.length - 1; i >= 0; i--) {
FunnelData funnelData = new FunnelData();
funnelData.label = labels[i];
funnelData.color = colors[i];
funnelData.num = String.valueOf(i);
data.add(funnelData);
}
return data;
}
}
50 changes: 28 additions & 22 deletions app/src/main/java/jie/com/funnel/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package jie.com.funnel;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,34 +21,34 @@
* Created by hj on 2019/2/22.
* 说明:
*/
public class MainActivity extends AppCompatActivity {
public class MainActivity extends ListActivity {

private int[] colors = new int[]{
Color.parseColor("#FF5656"), Color.parseColor("#FF854B"),
Color.parseColor("#FFB240"), Color.parseColor("#FFEC3D"),
Color.parseColor("#4DD2F9"), Color.parseColor("#52B5FF"),
Color.parseColor("#5882FF"), Color.parseColor("#5959FF"),
Color.parseColor("#8359FF"), Color.parseColor("#AC59FF")
};

private int[] fourColors = new int[]{
Color.parseColor("#FF5D5D"), Color.parseColor("#FFB240"),
Color.parseColor("#52B5FF"), Color.parseColor("#5882FF"),
private final String[] data = new String[]{
"使用默认方式(Use default mode)",
"自定义描述文字(Custom Description Text)",
"使用自定义宽度策略(Use custom width policy)",
};

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FunnelView funnelView = findViewById(R.id.funnel);
List<IFunnelData> data = new ArrayList<>();
for (int i = 0; i < 4; i++) {
FunnelData entity = new FunnelData();
entity.value = "A";
entity.color = fourColors[i];
entity.label = i+"";
data.add(entity);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
data);
setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
switch (position) {
case 0:
startActivity(new Intent(this, DefaultActivity.class));
break;
case 1:
startActivity(new Intent(this, CustomLabelActivity.class));
break;
case 2:
startActivity(new Intent(this,CustomHalfWidthActivity.class));
break;
}
funnelView.setChartData(data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<jie.com.funnellib.FunnelView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/funnel"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:background="#e4ebe8"
/>

<jie.com.funnellib.FunnelView
android:id="@+id/funnelView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp" />
</LinearLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Created by hj on 2019/2/22.
* 说明:开放自定义描述绘制画笔
*/
public interface CustomLabel {
public interface CustomLabelCallback {
/**
* 循环绘制线后面的文字
* @param canvas 画布
Expand Down
Loading

0 comments on commit 5eacb5d

Please sign in to comment.