Bresenham畫線演算法
我把每條線的每個點的x,y坐標存到list<Point>里面
最后list的size達到幾百W,最大會有1000多W
這就導致頻繁GC,容易卡死
最大4K屏 3840*2160
比如 1920*1080*3(RGB),一條RGB線由3條子像素線組成,
相當于螢屏有(1920*3)*1080那么大,在上面的記憶體區畫斜線,每一根斜線只有一個位元組的大小,
顏色值是0或者255(黑白),那這條線根據演算法畫線就有1080個點
new Thread(new Runnable() {
@Override
public void run() {
try {
points.clear();
//lines是所有子像素線的集合: 每條RGB像素線由3條子像素線組成
for (int i = 0; i < lines.size(); i++) {
//畫線演算法獲得每條子像素線點的x,y坐標 執行程序中容易GC
//引數是子像素線起、終2個點的x,y坐標
//Bresenham畫線演算法獲得每個點的x,y坐標
getPXpoint(lines.get(i).getX_start(), lines.get(i).getY_start(), lines.get(i).getX_end(), screen_height);
}
byte賦值 像這樣{255,0,0,255,0,0,255,0,0,.........} 會GC
bytes = new byte[screen_width * 3 * screen_height];
for (int i = 0; i < points.size(); i++) {
bytes[points.get(i).getY() * screen_width * 3 + points.get(i).getX()] = (byte) 255;
}
//轉ARGB 也會造成GC
if (rgbaColor != null)
rgbaColor = null;
rgbaColor = new int[screen_width * screen_height];
int k = 0;
while (k < screen_width * 3 * screen_height) {
rgbaColor[k / 3] = (255 & 0xff) << 24 | (bytes[k] & 0xff) << 16 | (bytes[k + 1] & 0xff) << 8 | (bytes[k + 2] & 0xff);
k += 3;
}
//config rgba
if (bitmap != null)
bitmap = null;
bitmap = Bitmap.createBitmap(rgbaColor, screen_width, screen_height, Bitmap.Config.ARGB_8888);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/236795.html
標籤:Android
上一篇:微信小程式無法上傳圖片到服務器
