今天记录一下Android绘图测试的一个程序。
程序要求,
1. 全屏显示 红/绿/蓝/白/黑/灰,6种颜色;
2. 全屏显示颜色条纹,从左到右颜色分别是,白/黄/浅蓝/浅绿/紫/红/蓝;
3. 全屏显示 3×5 的黑白相间的方块;
4. 手工点击屏幕,进入下一种颜色,显示完毕,关闭程序;
下面一起看看要用到的技术,
1. 屏幕全屏化,参考下面整理的文章,
https://blog.csdn.net/jgw2008/article/details/100133261
2. 获取屏幕的宽和高
https://blog.csdn.net/jgw2008/article/details/100140594
3. 绘图,使用Paint和Canvas,需要继承View class,每次需要呼叫setContentView(view)来更新视图。
全部代码整理如下,
package com.xxx.testgraphics;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
private DrawPicture drawPicture;
private int mCount = 0;
private int mScreenHeight, mScreenWidth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
setContentView(R.layout.activity_main);
DisplayMetrics dm = getResources().getDisplayMetrics();
mScreenWidth = dm.widthPixels;
mScreenHeight = dm.heightPixels;
drawPicture = new DrawPicture(this);
drawPicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCount++;
setContentView(drawPicture);
}
});
setContentView(drawPicture);
}
private class DrawPicture extends View {
public DrawPicture(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas){
switch (mCount) {
case 0:
this.setBackgroundColor(Color.RED);
break;
case 1:
this.setBackgroundColor(Color.rgb(0, 176, 80));
break;
case 2:
this.setBackgroundColor(Color.BLUE);
break;
case 3:
this.setBackgroundColor(Color.WHITE);
break;
case 4:
this.setBackgroundColor(Color.BLACK);
break;
case 5:
this.setBackgroundColor(Color.rgb(127, 127, 127));
break;
case 6:
drawColorBar(canvas);
break;
case 7:
drawWhiteBlackBlock(canvas);
break;
default:
finish();
break;
}
super.onDraw(canvas);
}
public void drawColorBar(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(0, 0, mScreenWidth, 1* mScreenHeight / 7), paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(new Rect(0, 1 * mScreenHeight / 7, mScreenWidth, 2 * mScreenHeight / 7), paint);
paint.setColor(Color.rgb(0, 176, 240)); // It's light blue
canvas.drawRect(new Rect(0, 2 * mScreenHeight / 7, mScreenWidth, 3 * mScreenHeight / 7), paint);
paint.setColor(Color.rgb(146, 208, 80)); // It's light green
canvas.drawRect(new Rect(0, 3 * mScreenHeight / 7, mScreenWidth, 4 * mScreenHeight / 7), paint);
paint.setColor(Color.MAGENTA);
canvas.drawRect(new Rect(0, 4 * mScreenHeight / 7, mScreenWidth, 5 * mScreenHeight / 7), paint);
paint.setColor(Color.RED);
canvas.drawRect(new Rect(0, 5 * mScreenHeight / 7, mScreenWidth, 6 * mScreenHeight / 7), paint);
paint.setColor(Color.BLUE);
canvas.drawRect(new Rect(0, 6 * mScreenHeight / 7, mScreenWidth, 7 * mScreenHeight / 7), paint);
}
public void drawWhiteBlackBlock(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(0, 0, 1 * mScreenWidth / 3, 1 * mScreenHeight / 5), paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(0, 1 * mScreenHeight / 5, 1 * mScreenWidth / 3, 2 * mScreenHeight / 5), paint);
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(0, 2 * mScreenHeight / 5, 1 * mScreenWidth / 3, 3 * mScreenHeight / 5), paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(0, 3 * mScreenHeight / 5, 1 * mScreenWidth / 3, 4 * mScreenHeight / 5), paint);
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(0, 4 * mScreenHeight / 5, 1 * mScreenWidth / 3, 5 * mScreenHeight / 5), paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(1 * mScreenWidth / 3, 0, 2 * mScreenWidth / 3, 1 * mScreenHeight / 5), paint);
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(1 * mScreenWidth / 3, 1 * mScreenHeight / 5, 2 * mScreenWidth / 3, 2 * mScreenHeight / 5), paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(1 * mScreenWidth / 3, 2 * mScreenHeight / 5, 2 * mScreenWidth / 3, 3 * mScreenHeight / 5), paint);
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(1 * mScreenWidth / 3, 3 * mScreenHeight / 5, 2 * mScreenWidth / 3, 4 * mScreenHeight / 5), paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(1 * mScreenWidth / 3, 4 * mScreenHeight / 5, 2 * mScreenWidth / 3, 5 * mScreenHeight / 5), paint);
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(2 * mScreenWidth / 3, 0, 3 * mScreenWidth / 3, 1 * mScreenHeight / 5), paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(2 * mScreenWidth / 3, 1 * mScreenHeight / 5, 3 * mScreenWidth / 3, 2 * mScreenHeight / 5), paint);
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(2 * mScreenWidth / 3, 2 * mScreenHeight / 5, 3 * mScreenWidth / 3, 3 * mScreenHeight / 5), paint);
paint.setColor(Color.WHITE);
canvas.drawRect(new Rect(2 * mScreenWidth / 3, 3 * mScreenHeight / 5, 3 * mScreenWidth / 3, 4 * mScreenHeight / 5), paint);
paint.setColor(Color.BLACK);
canvas.drawRect(new Rect(2 * mScreenWidth / 3, 4 * mScreenHeight / 5, 3 * mScreenWidth / 3, 5 * mScreenHeight / 5), paint);
}
}
}