diff --git a/.idea/misc.xml b/.idea/misc.xml
index 0fa1f567..47166006 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -37,7 +37,7 @@
-
+
diff --git a/.idea/modules.xml b/.idea/modules.xml
index cd4eccc5..f82e656a 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -3,6 +3,8 @@
+
+
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 00000000..94a25f7f
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
index 6d36d85f..ff1f8fb9 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,7 @@
![](images/icon.png)
+### Fork 自伟大的扔物线大神的练习 demo ,目前已经完成除饼状图以外所有,仅供大家参考~
+
HenCoder 绘制 1 练习项目
===
@@ -29,4 +31,4 @@ HenCoder 绘制 1 练习项目
现在的 Android 默认是开启了硬件加速的,而 Canvas 和 Paint 有一些方法是不支持硬件加速的,你需要把它手动关闭才行。硬件加速的支持情况和手动关闭硬件加速的方法你可以看一下这个 Android 的[官方文档](https://developer.android.com/guide/topics/graphics/hardware-accel.html)。
-以后我会专门用一节来讲硬件加速和图像的离屏缓冲(off-screen buffer),现在先不多说了。
\ No newline at end of file
+以后我会专门用一节来讲硬件加速和图像的离屏缓冲(off-screen buffer),现在先不多说了。
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 529addf3..36b3399c 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -7,7 +7,9 @@
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
- android:theme="@style/AppTheme">
+ android:theme="@style/AppTheme"
+ android:hardwareAccelerated="false">
+
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/PxDpUtil.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/PxDpUtil.java
new file mode 100644
index 00000000..96af5850
--- /dev/null
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/PxDpUtil.java
@@ -0,0 +1,22 @@
+package com.hencoder.hencoderpracticedraw1;
+
+import android.content.Context;
+
+/**
+ * Author: nanchen
+ * Email: liushilin520@foxmail.com
+ * Date: 2017-07-11 15:18
+ */
+
+public class PxDpUtil {
+
+ public static int dip2px(Context context, float dipValue) {
+ final float scale = context.getResources().getDisplayMetrics().density;
+ return (int) (dipValue * scale + 0.5f);
+ }
+
+ public static int px2dip(Context context, float pxValue) {
+ final float scale = context.getResources().getDisplayMetrics().density;
+ return (int) (pxValue / scale + 0.5f);
+ }
+}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice10HistogramView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice10HistogramView.java
index 374d0571..16af5458 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice10HistogramView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice10HistogramView.java
@@ -2,10 +2,14 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
+import com.hencoder.hencoderpracticedraw1.PxDpUtil;
+
public class Practice10HistogramView extends View {
public Practice10HistogramView(Context context) {
@@ -20,11 +24,39 @@ public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, in
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 综合练习
// 练习内容:使用各种 Canvas.drawXXX() 方法画直方图
+ float width = 70;
+
+ mPaint.setColor(Color.WHITE);
+ mPaint.setStrokeWidth(PxDpUtil.dip2px(getContext(), 1));
+ canvas.drawLine(50, 50, 50, 600, mPaint); // 纵坐标
+
+ canvas.drawLine(50, 600, 600, 600, mPaint); // 横坐标
+
+ mPaint.setTextSize(18);
+ canvas.drawText("Froyo", 85, 650, mPaint);
+ canvas.drawText("GB", 85 + width * 1, 650, mPaint);
+ canvas.drawText("ICS", 85 + width * 2, 650, mPaint);
+ canvas.drawText("JB", 85 + width * 3, 650, mPaint);
+ canvas.drawText("KitKat", 85 + width * 4, 650, mPaint);
+ canvas.drawText("L", 85 + width * 5, 650, mPaint);
+ canvas.drawText("M", 85 + width * 6, 650, mPaint);
+
+ mPaint.setColor(Color.GREEN);
+ mPaint.setStrokeWidth(PxDpUtil.dip2px(getContext(), 20));
+ canvas.drawLine(110, 600, 110, 580, mPaint);
+ canvas.drawLine(110 + width, 600, 110 + width, 550, mPaint);
+ canvas.drawLine(110 + width * 2, 600, 110 + width * 2, 550, mPaint);
+ canvas.drawLine(110 + width * 3, 600, 110 + width * 3, 450, mPaint);
+ canvas.drawLine(110 + width * 4, 600, 110 + width * 4, 300, mPaint);
+ canvas.drawLine(110 + width * 5, 600, 110 + width * 5, 250, mPaint);
+ canvas.drawLine(110 + width * 6, 600, 110 + width * 6, 480, mPaint);
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice1DrawColorView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice1DrawColorView.java
index 31dc0f82..f82d9186 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice1DrawColorView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice1DrawColorView.java
@@ -2,6 +2,8 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
@@ -20,11 +22,15 @@ public Practice1DrawColorView(Context context, @Nullable AttributeSet attrs, int
super(context, attrs, defStyleAttr);
}
+ Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawColor() 方法把 View 涂成黄色
// 黄色: Color.YELLOW
+ paint.setColor(Color.YELLOW);
+ canvas.drawPaint(paint);
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice2DrawCircleView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice2DrawCircleView.java
index f6e1e924..88627673 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice2DrawCircleView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice2DrawCircleView.java
@@ -2,10 +2,15 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Paint.Style;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
+import com.hencoder.hencoderpracticedraw1.PxDpUtil;
+
public class Practice2DrawCircleView extends View {
public Practice2DrawCircleView(Context context) {
@@ -20,11 +25,34 @@ public Practice2DrawCircleView(Context context, @Nullable AttributeSet attrs, in
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawCircle() 方法画圆
// 一共四个圆:1.实心圆 2.空心圆 3.蓝色实心圆 4.线宽为 20 的空心圆
+
+ // 1.实心圆
+ mPaint.setColor(Color.BLACK);
+ mPaint.setStyle(Style.FILL);
+ canvas.drawCircle(250,180,150,mPaint);
+
+ // 2.空心圆
+ mPaint.setStyle(Style.STROKE);
+ mPaint.setStrokeWidth(PxDpUtil.dip2px(getContext(),2));
+ canvas.drawCircle(600,180,150,mPaint);
+
+ // 3. 蓝色实心圆
+ mPaint.setColor(Color.BLUE);
+ mPaint.setStyle(Style.FILL);
+ canvas.drawCircle(250,530,150,mPaint);
+
+ // 4.线宽为20的空心圆
+ mPaint.setStyle(Style.STROKE);
+ mPaint.setStrokeWidth(PxDpUtil.dip2px(getContext(),20));
+ mPaint.setColor(Color.BLACK);
+ canvas.drawCircle(600,530,150,mPaint);
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice3DrawRectView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice3DrawRectView.java
index ace7f0ce..ad8688de 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice3DrawRectView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice3DrawRectView.java
@@ -2,6 +2,8 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
@@ -20,10 +22,16 @@ public Practice3DrawRectView(Context context, @Nullable AttributeSet attrs, int
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawRect() 方法画矩形
+
+ mPaint.setColor(Color.BLACK);
+ canvas.drawRect(300,150,600,450,mPaint);
+
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice4DrawPointView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice4DrawPointView.java
index 318538e0..cad3350a 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice4DrawPointView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice4DrawPointView.java
@@ -2,10 +2,15 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Paint.Cap;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
+import com.hencoder.hencoderpracticedraw1.PxDpUtil;
+
public class Practice4DrawPointView extends View {
public Practice4DrawPointView(Context context) {
@@ -20,6 +25,7 @@ public Practice4DrawPointView(Context context, @Nullable AttributeSet attrs, int
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
@@ -27,5 +33,13 @@ protected void onDraw(Canvas canvas) {
// 练习内容:使用 canvas.drawPoint() 方法画点
// 一个圆点,一个方点
// 圆点和方点的切换使用 paint.setStrokeCap(cap):`ROUND` 是圆点,`BUTT` 或 `SQUARE` 是方点
+
+ mPaint.setStrokeCap(Cap.ROUND);
+ mPaint.setColor(Color.BLACK);
+ mPaint.setStrokeWidth(PxDpUtil.dip2px(getContext(),20));
+ canvas.drawPoint(200,200,mPaint);
+
+ mPaint.setStrokeCap(Cap.BUTT);
+ canvas.drawPoint(400,200,mPaint);
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice5DrawOvalView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice5DrawOvalView.java
index c276d8b7..a1ae28d0 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice5DrawOvalView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice5DrawOvalView.java
@@ -2,6 +2,9 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.Paint.Style;
+import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
@@ -19,11 +22,16 @@ public Practice5DrawOvalView(Context context, @Nullable AttributeSet attrs) {
public Practice5DrawOvalView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+ RectF mRectF = new RectF(200,100,600,300);
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawOval() 方法画椭圆
+ mPaint.setStyle(Style.FILL);
+// canvas.drawOval(200,100,300,200,mPaint);
+ canvas.drawOval(mRectF,mPaint);
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice6DrawLineView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice6DrawLineView.java
index 7063c165..06baeb13 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice6DrawLineView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice6DrawLineView.java
@@ -2,10 +2,13 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
+import com.hencoder.hencoderpracticedraw1.PxDpUtil;
+
public class Practice6DrawLineView extends View {
public Practice6DrawLineView(Context context) {
@@ -20,10 +23,14 @@ public Practice6DrawLineView(Context context, @Nullable AttributeSet attrs, int
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawLine() 方法画直线
+ mPaint.setStrokeWidth(PxDpUtil.dip2px(getContext(),5));
+ canvas.drawLine(100,100,400,400,mPaint);
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice7DrawRoundRectView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice7DrawRoundRectView.java
index 694cf7e8..4b1631e9 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice7DrawRoundRectView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice7DrawRoundRectView.java
@@ -2,6 +2,9 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.Paint.Style;
+import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
@@ -20,10 +23,16 @@ public Practice7DrawRoundRectView(Context context, @Nullable AttributeSet attrs,
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+ RectF mRectF = new RectF(300,100,800,500);
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawRoundRect() 方法画圆角矩形
+
+ mPaint.setStyle(Style.FILL);
+ canvas.drawRoundRect(mRectF,50,50,mPaint);
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice8DrawArcView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice8DrawArcView.java
index d9e440c1..ca6be76b 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice8DrawArcView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice8DrawArcView.java
@@ -2,6 +2,9 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.Paint.Style;
+import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
@@ -20,10 +23,23 @@ public Practice8DrawArcView(Context context, @Nullable AttributeSet attrs, int d
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+ RectF mRectF = new RectF(200,100,800,500);
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawArc() 方法画弧形和扇形
+
+ mPaint.setStyle(Style.FILL);
+ canvas.drawArc(mRectF,-130,110,true,mPaint);// 从-130开始,跨度110
+
+ canvas.drawArc(mRectF,20,120,false,mPaint);
+
+ mPaint.setStyle(Style.STROKE);
+ canvas.drawArc(mRectF,180,40,false,mPaint);
+
+
}
}
diff --git a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice9DrawPathView.java b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice9DrawPathView.java
index 19f7881d..bdf6e000 100644
--- a/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice9DrawPathView.java
+++ b/app/src/main/java/com/hencoder/hencoderpracticedraw1/practice/Practice9DrawPathView.java
@@ -2,6 +2,9 @@
import android.content.Context;
import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.Path;
+import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
@@ -20,10 +23,20 @@ public Practice9DrawPathView(Context context, @Nullable AttributeSet attrs, int
super(context, attrs, defStyleAttr);
}
+ Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
+ Path mPath = new Path();
+
+ {
+ mPath.addArc(new RectF(200, 200, 400, 400), -225, 225); // 半径 100 的扇形 225度,从-225开始
+ mPath.arcTo(new RectF(400, 200, 600, 400), -180, 225, false); // false 不留下移笔痕迹
+ mPath.lineTo(400, 542); // 从原点画直线到坐标400,542
+ }
+
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 练习内容:使用 canvas.drawPath() 方法画心形
+ canvas.drawPath(mPath,mPaint);
}
}
diff --git a/app/src/main/res/layout/practice_histogram.xml b/app/src/main/res/layout/practice_histogram.xml
index 4df9021f..1e578ddd 100644
--- a/app/src/main/res/layout/practice_histogram.xml
+++ b/app/src/main/res/layout/practice_histogram.xml
@@ -1,5 +1,6 @@
-
\ No newline at end of file
diff --git a/app/src/main/res/layout/practice_point.xml b/app/src/main/res/layout/practice_point.xml
index 9b4ce8c1..fdc859f8 100644
--- a/app/src/main/res/layout/practice_point.xml
+++ b/app/src/main/res/layout/practice_point.xml
@@ -1,4 +1,5 @@
-
\ No newline at end of file