Skip to content

Commit 4fc8e60

Browse files
committed
完成基本版本
1 parent 4ed704b commit 4fc8e60

20 files changed

+361
-181
lines changed

app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.bin.david.smartchart">
55

6+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
7+
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
8+
69
<application
710
android:allowBackup="true"
811
android:icon="@mipmap/icon"

app/src/main/java/com/bin/david/smartchart/LineChartActivity.java

+106-87
Large diffs are not rendered by default.

app/src/main/java/com/bin/david/smartchart/custom/MarkPoint.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,23 @@ public class MarkPoint implements IPoint {
2020
private Bitmap markBitmap;
2121
private Bitmap avatorBitmap;
2222
private Rect bitmapRect;
23-
private Rect avatorbitmapRect;
23+
private Rect avatorBitmapRect;
2424
private Rect rect;
2525
private int height;
2626
private int width;
27-
private PorterDuffXfermode xfermode;
27+
private PorterDuffXfermode xFermode;
2828

2929
public MarkPoint(Context context,int height){
3030
markBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.mark);
3131
BitmapFactory.Options options = new BitmapFactory.Options();
3232
options.inSampleSize = 4;
3333
avatorBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.avator,options);
3434
bitmapRect = new Rect(0,0,markBitmap.getWidth(),markBitmap.getHeight());
35-
avatorbitmapRect = new Rect(0,0,avatorBitmap.getWidth(),avatorBitmap.getHeight());
35+
avatorBitmapRect = new Rect(0,0,avatorBitmap.getWidth(),avatorBitmap.getHeight());
3636
this.height = height;
3737
rect = new Rect();
3838
this.width = markBitmap.getWidth()*this.height/markBitmap.getHeight();
39-
xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);
39+
xFermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
4040

4141
}
4242

@@ -46,12 +46,13 @@ public void drawPoint(Canvas canvas, float x, float y, boolean isShowDefaultColo
4646
rect.bottom = (int) y;
4747
rect.right = (int) (x + width/2);
4848
rect.left = (int)(x - width/2);
49+
canvas.drawBitmap(markBitmap,bitmapRect,rect,paint);
4950
int layerId = canvas.saveLayer(rect.left,rect.top,rect.right,rect.bottom , null, Canvas.ALL_SAVE_FLAG);
50-
51-
canvas.drawBitmap(markBitmap,bitmapRect,rect,paint);
52-
paint.setXfermode(xfermode);
53-
rect.bottom = rect.top+rect.width();
54-
canvas.drawBitmap(avatorBitmap,avatorbitmapRect,rect,paint);
51+
rect.bottom = rect.top+rect.width();
52+
paint.setStyle(Paint.Style.FILL);
53+
canvas.drawCircle(rect.centerX(),rect.centerY(),rect.width()/2,paint);
54+
paint.setXfermode(xFermode);
55+
canvas.drawBitmap(avatorBitmap, avatorBitmapRect,rect,paint);
5556
paint.setXfermode(null);
5657
canvas.restoreToCount(layerId);//将自己创建的画布Layer绘制到画布默认的Layer
5758
}

app/src/main/res/layout/activity_line.xml

+32-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
android:layout_height="match_parent"
66
android:orientation="vertical"
77
android:gravity="center"
8-
tools:context="com.bin.david.smartchart.MainActivity">
8+
tools:context="com.bin.david.smartchart.LineChartActivity">
99

1010
<com.daivd.chart.core.LineChart
1111

@@ -14,18 +14,37 @@
1414
android:background="#f4f4f4"
1515
android:layout_height="400dp"/>
1616

17-
<Button
18-
android:id="@+id/btn"
19-
android:layout_width="wrap_content"
20-
android:layout_marginTop="12dp"
21-
android:paddingLeft="23dp"
22-
android:paddingRight="23dp"
23-
android:background="@drawable/dialog_btn_bg"
24-
android:text="chart Setting"
25-
android:onClick="onClick"
26-
android:clickable="true"
27-
android:textColor="#fff"
28-
android:layout_height="45dp" />
17+
<LinearLayout
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content"
20+
android:gravity="center"
21+
android:orientation="horizontal">
22+
<Button
23+
android:id="@+id/btn"
24+
android:layout_width="wrap_content"
25+
android:paddingLeft="10dp"
26+
android:paddingRight="10dp"
27+
android:background="@drawable/dialog_btn_bg"
28+
android:layout_marginRight="30dp"
29+
android:text="chart Setting"
30+
android:onClick="onClick"
31+
android:clickable="true"
32+
android:textColor="#fff"
33+
android:layout_height="45dp" />
34+
35+
<Button
36+
android:id="@+id/btn2"
37+
android:layout_width="wrap_content"
38+
android:paddingLeft="10dp"
39+
android:paddingRight="10dp"
40+
android:background="@drawable/dialog_btn_bg"
41+
android:text="Save Pic"
42+
android:onClick="onClick"
43+
android:clickable="true"
44+
android:textColor="#fff"
45+
android:layout_height="45dp" />
46+
47+
</LinearLayout>
2948

3049

3150

-496 Bytes
Loading

chart/build.gradle

-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,4 @@ android {
2323
}
2424

2525
dependencies {
26-
testCompile 'junit:junit:4.12'
27-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
28-
exclude group: 'com.android.support', module: 'support-annotations'
29-
})
30-
compile 'com.android.support:appcompat-v7:25.3.1'
3126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.daivd.chart.component;
2+
3+
import android.content.ContentValues;
4+
import android.graphics.Bitmap;
5+
import android.graphics.Canvas;
6+
import android.graphics.Color;
7+
import android.graphics.drawable.Drawable;
8+
import android.os.Environment;
9+
import android.provider.MediaStore;
10+
import android.view.View;
11+
12+
import com.daivd.chart.data.PicOption;
13+
14+
import java.io.File;
15+
import java.io.FileOutputStream;
16+
import java.io.IOException;
17+
18+
/**
19+
* Created by huang on 2017/10/30.
20+
* 图片生成
21+
*/
22+
23+
public class PicGeneration<V extends View> {
24+
25+
public boolean save(V v,PicOption option){
26+
getChartBitmap(v);
27+
return saveToGallery(v,option);
28+
}
29+
30+
private Bitmap getChartBitmap(V v) {
31+
// 创建一个bitmap 根据我们自定义view的大小
32+
Bitmap returnedBitmap = Bitmap.createBitmap(v.getWidth(),
33+
v.getHeight(), Bitmap.Config.RGB_565);
34+
// 绑定canvas
35+
Canvas canvas = new Canvas(returnedBitmap);
36+
// 获取视图的背景
37+
Drawable bgDrawable = v.getBackground();
38+
if (bgDrawable != null)
39+
// 如果有就绘制
40+
bgDrawable.draw(canvas);
41+
else
42+
// 没有就绘制白色
43+
canvas.drawColor(Color.WHITE);
44+
// 绘制
45+
v.draw(canvas);
46+
return returnedBitmap;
47+
}
48+
49+
private boolean saveToGallery(V v, PicOption option) {
50+
String mFilePath;
51+
// 控制图片质量
52+
if (option.getQuality() < 0 || option.getQuality() > 100)
53+
option.setQuality(50);
54+
long currentTime = System.currentTimeMillis();
55+
56+
File extBaseDir = Environment.getExternalStorageDirectory();
57+
File file = new File(extBaseDir.getAbsolutePath() + "/DCIM/" + option.getSubFolderPath());
58+
if (!file.exists()) {
59+
if (!file.mkdirs()) {
60+
return false;
61+
}
62+
}
63+
64+
String mimeType = "";
65+
String fileName = option.getFileName();
66+
switch (option.getFormat()) {
67+
case PNG:
68+
mimeType = "image/png";
69+
if (!fileName.endsWith(".png"))
70+
fileName += ".png";
71+
break;
72+
case WEBP:
73+
mimeType = "image/webp";
74+
if (!fileName.endsWith(".webp"))
75+
fileName += ".webp";
76+
break;
77+
case JPEG:
78+
default:
79+
mimeType = "image/jpeg";
80+
if (!(fileName.endsWith(".jpg") || fileName.endsWith(".jpeg")))
81+
fileName += ".jpg";
82+
break;
83+
}
84+
mFilePath = file.getAbsolutePath() + "/" + fileName;
85+
FileOutputStream out = null;
86+
try {
87+
out = new FileOutputStream(mFilePath);
88+
Bitmap b = getChartBitmap(v);
89+
b.compress(option.getFormat(), option.getQuality(), out);
90+
91+
out.flush();
92+
out.close();
93+
94+
} catch (IOException e) {
95+
e.printStackTrace();
96+
97+
return false;
98+
}
99+
100+
long size = new File(mFilePath).length();
101+
102+
ContentValues values = new ContentValues(8);
103+
104+
// store the details
105+
values.put(MediaStore.Images.Media.TITLE, fileName);
106+
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
107+
values.put(MediaStore.Images.Media.DATE_ADDED, currentTime);
108+
values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
109+
values.put(MediaStore.Images.Media.DESCRIPTION, option.getFileDescription());
110+
values.put(MediaStore.Images.Media.ORIENTATION, 0);
111+
values.put(MediaStore.Images.Media.DATA, mFilePath);
112+
values.put(MediaStore.Images.Media.SIZE, size);
113+
return v.getContext().getContentResolver().
114+
insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values) != null;
115+
}
116+
}

chart/src/main/java/com/daivd/chart/core/Bar3DChart.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.content.Context;
44
import android.os.Build;
5-
import android.support.annotation.RequiresApi;
65
import android.util.AttributeSet;
76

87
import com.daivd.chart.core.base.BaseBarLineChart;
@@ -27,10 +26,7 @@ public Bar3DChart(Context context, AttributeSet attrs, int defStyleAttr) {
2726
super(context, attrs, defStyleAttr);
2827
}
2928

30-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
31-
public Bar3DChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
32-
super(context, attrs, defStyleAttr, defStyleRes);
33-
}
29+
3430

3531

3632

chart/src/main/java/com/daivd/chart/core/BarChart.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.daivd.chart.core;
22

33
import android.content.Context;
4-
import android.os.Build;
5-
import android.support.annotation.RequiresApi;
64
import android.util.AttributeSet;
75

86
import com.daivd.chart.core.base.BaseBarLineChart;
@@ -28,10 +26,7 @@ public BarChart(Context context, AttributeSet attrs, int defStyleAttr) {
2826
super(context, attrs, defStyleAttr);
2927
}
3028

31-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
32-
public BarChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
33-
super(context, attrs, defStyleAttr, defStyleRes);
34-
}
29+
3530

3631

3732
@Override

chart/src/main/java/com/daivd/chart/core/BarLineChart.java

-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.daivd.chart.core;
22

33
import android.content.Context;
4-
import android.os.Build;
5-
import android.support.annotation.RequiresApi;
64
import android.util.AttributeSet;
75

86
import com.daivd.chart.core.base.BaseBarLineChart;
@@ -28,10 +26,6 @@ public BarLineChart(Context context, AttributeSet attrs, int defStyleAttr) {
2826
super(context, attrs, defStyleAttr);
2927
}
3028

31-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
32-
public BarLineChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
33-
super(context, attrs, defStyleAttr, defStyleRes);
34-
}
3529

3630

3731

chart/src/main/java/com/daivd/chart/core/LineChart.java

-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package com.daivd.chart.core;
22

33
import android.content.Context;
4-
import android.os.Build;
5-
import android.support.annotation.RequiresApi;
64
import android.util.AttributeSet;
75
import android.view.Gravity;
86

97
import com.daivd.chart.core.base.BaseBarLineChart;
10-
import com.daivd.chart.data.BarData;
118
import com.daivd.chart.data.LineData;
129
import com.daivd.chart.exception.ChartException;
1310
import com.daivd.chart.provider.barLine.LineProvider;
@@ -36,10 +33,6 @@ public LineChart(Context context, AttributeSet attrs, int defStyleAttr) {
3633
super(context, attrs, defStyleAttr);
3734
}
3835

39-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
40-
public LineChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
41-
super(context, attrs, defStyleAttr, defStyleRes);
42-
}
4336

4437

4538
@Override

chart/src/main/java/com/daivd/chart/core/Pie3DChart.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.content.Context;
44
import android.os.Build;
5-
import android.support.annotation.RequiresApi;
65
import android.util.AttributeSet;
76

87
import com.daivd.chart.core.base.BaseRotateChart;
@@ -30,10 +29,7 @@ public Pie3DChart(Context context, AttributeSet attrs, int defStyleAttr) {
3029
super(context, attrs, defStyleAttr);
3130
}
3231

33-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
34-
public Pie3DChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
35-
super(context, attrs, defStyleAttr, defStyleRes);
36-
}
32+
3733

3834

3935
@Override

chart/src/main/java/com/daivd/chart/core/PieChart.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.daivd.chart.core;
22

33
import android.content.Context;
4-
import android.os.Build;
5-
import android.support.annotation.RequiresApi;
64
import android.util.AttributeSet;
75

86
import com.daivd.chart.core.base.BaseRotateChart;
@@ -30,10 +28,7 @@ public PieChart(Context context, AttributeSet attrs, int defStyleAttr) {
3028
super(context, attrs, defStyleAttr);
3129
}
3230

33-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
34-
public PieChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
35-
super(context, attrs, defStyleAttr, defStyleRes);
36-
}
31+
3732

3833

3934
@Override

chart/src/main/java/com/daivd/chart/core/RadarChart.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.daivd.chart.core;
22

33
import android.content.Context;
4-
import android.os.Build;
5-
import android.support.annotation.RequiresApi;
64
import android.util.AttributeSet;
75

86
import com.daivd.chart.core.base.BaseRotateChart;
@@ -30,10 +28,7 @@ public RadarChart(Context context, AttributeSet attrs, int defStyleAttr) {
3028
super(context, attrs, defStyleAttr);
3129
}
3230

33-
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
34-
public RadarChart(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
35-
super(context, attrs, defStyleAttr, defStyleRes);
36-
}
31+
3732

3833

3934
@Override

0 commit comments

Comments
 (0)