Skip to content

Commit 4419b50

Browse files
committed
06 绘制 1
1 parent 55bc896 commit 4419b50

31 files changed

+565
-0
lines changed

06-drawing/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

06-drawing/build.gradle

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
defaultConfig {
6+
applicationId "com.hencoder.plus"
7+
minSdkVersion 21
8+
targetSdkVersion 28
9+
versionCode 1
10+
versionName "1.0"
11+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
20+
21+
dependencies {
22+
implementation fileTree(dir: 'libs', include: ['*.jar'])
23+
implementation 'com.android.support:appcompat-v7:28.0.0'
24+
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
25+
testImplementation 'junit:junit:4.12'
26+
androidTestImplementation 'com.android.support.test:runner:1.0.2'
27+
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28+
}

06-drawing/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.hencoder.plus;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.hencoder.plus2", appContext.getPackageName());
25+
}
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.hencoder.plus">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/AppTheme">
14+
<activity android:name="com.hencoder.plus.MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.hencoder.plus;
2+
3+
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
6+
public class MainActivity extends AppCompatActivity {
7+
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
setContentView(R.layout.activity_main);
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.hencoder.plus;
2+
3+
import android.content.res.Resources;
4+
import android.util.TypedValue;
5+
6+
public class Utils {
7+
public static float dp2px(float dp) {
8+
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
9+
}
10+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.hencoder.plus.view;
2+
3+
import android.content.Context;
4+
import android.graphics.Bitmap;
5+
import android.graphics.BitmapFactory;
6+
import android.graphics.Canvas;
7+
import android.graphics.Paint;
8+
import android.graphics.PorterDuff;
9+
import android.graphics.PorterDuffXfermode;
10+
import android.graphics.Rect;
11+
import android.graphics.RectF;
12+
import android.graphics.Xfermode;
13+
import android.support.annotation.Nullable;
14+
import android.util.AttributeSet;
15+
import android.view.View;
16+
17+
import com.hencoder.plus.R;
18+
import com.hencoder.plus.Utils;
19+
20+
public class AvatarView extends View {
21+
private static final float WIDTH = Utils.dp2px(300);
22+
private static final float PADDING = Utils.dp2px(50);
23+
private static final float EDGE_WIDTH = Utils.dp2px(10);
24+
25+
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
26+
Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
27+
Bitmap bitmap;
28+
RectF savedArea = new RectF();
29+
30+
public AvatarView(Context context, @Nullable AttributeSet attrs) {
31+
super(context, attrs);
32+
}
33+
34+
{
35+
bitmap = getAvatar((int) WIDTH);
36+
}
37+
38+
@Override
39+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
40+
super.onSizeChanged(w, h, oldw, oldh);
41+
42+
savedArea.set(PADDING, PADDING, PADDING + WIDTH, PADDING + WIDTH);
43+
}
44+
45+
@Override
46+
protected void onDraw(Canvas canvas) {
47+
super.onDraw(canvas);
48+
49+
canvas.drawOval(PADDING, PADDING, PADDING + WIDTH, PADDING + WIDTH, paint);
50+
int saved = canvas.saveLayer(savedArea, paint);
51+
canvas.drawOval(PADDING + EDGE_WIDTH, PADDING + EDGE_WIDTH, PADDING + WIDTH - EDGE_WIDTH, PADDING + WIDTH - EDGE_WIDTH, paint);
52+
paint.setXfermode(xfermode);
53+
canvas.drawBitmap(bitmap, PADDING, PADDING, paint);
54+
paint.setXfermode(null);
55+
canvas.restoreToCount(saved);
56+
}
57+
58+
Bitmap getAvatar(int width) {
59+
BitmapFactory.Options options = new BitmapFactory.Options();
60+
options.inJustDecodeBounds = true;
61+
BitmapFactory.decodeResource(getResources(), R.drawable.avatar_rengwuxian, options);
62+
options.inJustDecodeBounds = false;
63+
options.inDensity = options.outWidth;
64+
options.inTargetDensity = width;
65+
return BitmapFactory.decodeResource(getResources(), R.drawable.avatar_rengwuxian, options);
66+
}
67+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.hencoder.plus.view;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.Path;
7+
import android.graphics.PathDashPathEffect;
8+
import android.graphics.PathMeasure;
9+
import android.support.annotation.Nullable;
10+
import android.util.AttributeSet;
11+
import android.view.View;
12+
13+
import com.hencoder.plus.Utils;
14+
15+
public class Dashboard extends View {
16+
private static final int ANGLE = 120;
17+
private static final float RADIUS = Utils.dp2px(150);
18+
private static final float LENGTH = Utils.dp2px(100);
19+
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
20+
Path dash = new Path();
21+
PathDashPathEffect effect;
22+
23+
public Dashboard(Context context, @Nullable AttributeSet attrs) {
24+
super(context, attrs);
25+
}
26+
27+
{
28+
paint.setStyle(Paint.Style.STROKE);
29+
paint.setStrokeWidth(Utils.dp2px(2));
30+
dash.addRect(0, 0, Utils.dp2px(2), Utils.dp2px(10), Path.Direction.CW);
31+
Path arc = new Path();
32+
arc.addArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS, getWidth() / 2 + RADIUS , getHeight() / 2 + RADIUS, 90 + ANGLE / 2, 360 - ANGLE);
33+
PathMeasure pathMeasure = new PathMeasure(arc, false);
34+
effect = new PathDashPathEffect(dash, (pathMeasure.getLength() - Utils.dp2px(2)) / 20, 0, PathDashPathEffect.Style.ROTATE);
35+
}
36+
37+
@Override
38+
protected void onDraw(Canvas canvas) {
39+
super.onDraw(canvas);
40+
41+
// 画线
42+
canvas.drawArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS, getWidth() / 2 + RADIUS , getHeight() / 2 + RADIUS, 90 + ANGLE / 2, 360 - ANGLE, false, paint);
43+
44+
// 画刻度
45+
paint.setPathEffect(effect);
46+
canvas.drawArc(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS, getWidth() / 2 + RADIUS , getHeight() / 2 + RADIUS, 90 + ANGLE / 2, 360 - ANGLE, false, paint);
47+
paint.setPathEffect(null);
48+
49+
// 画指针
50+
canvas.drawLine(getWidth() / 2, getHeight() / 2,
51+
(float) Math.cos(Math.toRadians(getAngleFromMark(5))) * LENGTH + getWidth() / 2,
52+
(float) Math.sin(Math.toRadians(getAngleFromMark(5))) * LENGTH + getHeight() / 2,
53+
paint);
54+
55+
}
56+
57+
int getAngleFromMark(int mark) {
58+
return (int) (90 + (float) ANGLE / 2 + (360 - (float) ANGLE) / 20 * mark);
59+
}
60+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.hencoder.plus.view;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Color;
6+
import android.graphics.Paint;
7+
import android.graphics.RectF;
8+
import android.support.annotation.Nullable;
9+
import android.util.AttributeSet;
10+
import android.view.View;
11+
12+
import com.hencoder.plus.Utils;
13+
14+
public class PieChart extends View {
15+
private static final int RADIUS = (int) Utils.dp2px(150);
16+
private static final int LENGTH = (int) Utils.dp2px(20);
17+
private static final int PULLED_OUT_INDEX = 2;
18+
19+
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
20+
RectF bounds = new RectF();
21+
int[] angles = {60, 100, 120, 80};
22+
int[] colors = {Color.parseColor("#2979FF"), Color.parseColor("#C2185B"),
23+
Color.parseColor("#009688"), Color.parseColor("#FF8F00")};
24+
25+
public PieChart(Context context, @Nullable AttributeSet attrs) {
26+
super(context, attrs);
27+
}
28+
29+
@Override
30+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
31+
super.onSizeChanged(w, h, oldw, oldh);
32+
33+
bounds.set(getWidth() / 2 - RADIUS, getHeight() / 2 - RADIUS, getWidth() / 2 + RADIUS, getHeight() / 2 + RADIUS);
34+
}
35+
36+
@Override
37+
protected void onDraw(Canvas canvas) {
38+
super.onDraw(canvas);
39+
40+
int currentAngle = 0;
41+
for (int i = 0; i < angles.length; i++) {
42+
paint.setColor(colors[i]);
43+
canvas.save();
44+
if (i == PULLED_OUT_INDEX) {
45+
canvas.translate((float) Math.cos(Math.toRadians(currentAngle + angles[i] / 2)) * LENGTH,
46+
(float) Math.sin(Math.toRadians(currentAngle + angles[i] / 2)) * LENGTH);
47+
}
48+
canvas.drawArc(bounds, currentAngle, angles[i], true, paint);
49+
canvas.restore();
50+
currentAngle += angles[i];
51+
}
52+
}
53+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeWidth="1"
11+
android:strokeColor="#00000000">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeWidth="1"
33+
android:strokeColor="#00000000" />
34+
</vector>
Loading

0 commit comments

Comments
 (0)