转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~
老规矩,先上图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
先了解一下FontMetrics,然后再看看Demo:
MainActivity.java:
转载请注明出处:http://blog.csdn.net/iwanghang/
觉得博文有用,请点赞,请评论,请关注,谢谢!~
觉得博文有用,请点赞,请评论,请关注,谢谢!~
老规矩,先上图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
先了解一下FontMetrics,然后再看看Demo:
MainActivity.java:
package com.iwanghang.newview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getSupportActionBar().hide(); //setContentView(R.layout.activity_main); NewView newView = new NewView(this); setContentView(newView); } }NewView.java:
package com.iwanghang.newview; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class NewView extends View{ // 单元格的宽度和高度 private float width; private float height; public NewView(Context context, AttributeSet attrs) { super(context, attrs); } public NewView(MainActivity mainActivity) { super(mainActivity); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { this.width = w / 9f; this.height = h / 9f; super.onSizeChanged(w, h, oldw, oldh); } // 当Android系统需要绘制一个View对象时,就会调用该对象的onDraw @Override protected void onDraw(Canvas canvas) { // 画背景 Paint backgroundPaint = new Paint(); backgroundPaint.setColor(getResources().getColor(R.color.colorBackground)); canvas.drawRect(0,0,getWidth(),getHeight(),backgroundPaint); Paint darkPaint = new Paint(); darkPaint.setColor(getResources().getColor(R.color.colorDark)); Paint hilitePaint = new Paint(); hilitePaint.setColor(getResources().getColor(R.color.colorHilite)); Paint lightPaint = new Paint(); lightPaint.setColor(getResources().getColor(R.color.colorLight)); // 绘制9X9的网络格 // 两条距离为1的直线,视觉上会有割裂的效果 for (int i = 0; i < 9; i++) { canvas.drawLine(0, i*height, getWidth(), i*height, lightPaint); canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilitePaint); canvas.drawLine(i*width, 0, i*width, getHeight(), lightPaint); canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hilitePaint); } // 绘制3X3的网络格 for (int i = 0; i < 9; i++) { if (i % 3 != 0) { continue; } canvas.drawLine(0, i*height, getWidth(), i*height, darkPaint); canvas.drawLine(0, i*height+1, getWidth(), i*height+1, hilitePaint); canvas.drawLine(i*width, 0, i*width, getHeight(), darkPaint); canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), hilitePaint); } // 绘制数字 Paint numberPaint = new Paint(); numberPaint.setColor(Color.BLACK); numberPaint.setStyle(Paint.Style.STROKE); numberPaint.setTextSize(height*0.75f); numberPaint.setTextAlign(Paint.Align.CENTER); numberPaint.setAntiAlias(true); // 抗锯齿 /** * 数字居中位置 * x轴居中比较容易计算 * y轴居中的计算,依赖于FontMetrics,大家很容易百度到相关的知识 */ Paint.FontMetrics fm = numberPaint.getFontMetrics(); float x = width / 2; float y = height / 2 - (fm.ascent + fm.descent) / 2; canvas.drawText("1", 0 * width + x, 0 * height + y, numberPaint); canvas.drawText("2", 1 * width + x, 1 * height + y, numberPaint); canvas.drawText("3", 2 * width + x, 2 * height + y, numberPaint); canvas.drawText("4", 3 * width + x, 3 * height + y, numberPaint); canvas.drawText("5", 4 * width + x, 4 * height + y, numberPaint); super.onDraw(canvas); } }colors.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="colorHilite">#ffffff</color> <color name="colorLight">#64c6d4ef</color> <color name="colorDark">#6456648f</color> <color name="colorBackground">#ffe6f0ff</color> </resources>
转载请注明出处:http://blog.csdn.net/iwanghang/
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
觉得博文有用,请点赞,请评论,请关注,谢谢!~
作者:iwanghang 发表于2017/1/4 16:14:48 原文链接
阅读:163 评论:0 查看评论