Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull 练习 demo 示例代码 #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
![](images/icon.png)

### Fork 自伟大的扔物线大神的练习 demo ,目前已经完成除饼状图以外所有,仅供大家参考~

HenCoder 绘制 1 练习项目
===

Expand Down Expand Up @@ -29,4 +31,4 @@ HenCoder 绘制 1 练习项目

现在的 Android 默认是开启了硬件加速的,而 Canvas 和 Paint 有一些方法是不支持硬件加速的,你需要把它手动关闭才行。硬件加速的支持情况和手动关闭硬件加速的方法你可以看一下这个 Android 的[官方文档](https://developer.android.com/guide/topics/graphics/hardware-accel.html)。

以后我会专门用一节来讲硬件加速和图像的离屏缓冲(off-screen buffer),现在先不多说了。
以后我会专门用一节来讲硬件加速和图像的离屏缓冲(off-screen buffer),现在先不多说了。
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/hencoder/hencoderpracticedraw1/PxDpUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.hencoder.hencoderpracticedraw1;

import android.content.Context;

/**
* Author: nanchen
* Email: [email protected]
* 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -20,12 +25,21 @@ 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);

// 练习内容:使用 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);


}
}
Loading