package cn.vaga.blazedemo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class BlazeView extends SurfaceView implements SurfaceHolder.Callback {
private int backWidth;
private int backHeight;
private Bitmap bgImage = null;
private boolean firstLoad = false;
BlazeThread mBlazeThread = new BlazeThread();
SurfaceHolder mSurfaceHolder = null;
private static RandNums randNums;
public static int[] palette = new int[512];
private int[] blazeBuf;
private int[] blazePalItems;
private Paint paint = new Paint();
static {
int index = 0;
int cIndex = 0;
double alpha = 0.0;
randNums = new RandNums();
// 生成调色板
palette[0] = Color.argb(0, 0, 0, 0); // 0 黑色的
for (index = 1; index < 70; index++) {
cIndex = index >> 1;
palette[index] = Color.argb((int)(alpha++ / 512 * 255), cIndex + 25,
randNums.nextInt() % 10, randNums.nextInt() % 10); // 1-34
// 接近于黑色的
}
for (index = 70; index < 110; index++) {
cIndex = index >> 1;
palette[index] = Color.argb((int)(alpha++ / 512 * 255), cIndex + 25, cIndex - 25, randNums
.nextInt() % 10); // 35-54 接近于黑色的,比上面的要淡一些
}
for (index = 110; index < 190; index++) {
cIndex = index >> 1;
palette[index] = Color.argb((int)(alpha++ / 512 * 255), cIndex + 75, cIndex, randNums
.nextInt() % 5); // 55-94 接近于深橘红色
}
for (index = 190; index < 400; index++) {
cIndex = index >> 1;
palette[index] = Color.argb((int)(alpha++ / 512 * 255), (cIndex + 70 > 255) ? 255 : (cIndex + 70), cIndex + 30, randNums
.nextInt() % 10); // 95-199 橘红色的
}
for (index = 400; index < 512; index++) {
cIndex = index >> 1;
palette[index] = Color.argb((int)(alpha++ / 512 * 255), cIndex, cIndex - randNums.nextInt()
% 25, randNums.nextInt() % 5); // 200-254
// 接近于黄色
}
}
public BlazeView(Context context) {
super(context);
mSurfaceHolder = getHolder();
mSurfaceHolder.addCallback(this);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (!firstLoad) {
bgImage = BitmapFactory.decodeResource(this.getResources(),
R.drawable.bg);
bgImage = Bitmap.createScaledBitmap(bgImage, w, h, false);// 缩放而已
backWidth = bgImage.getWidth();
backHeight = bgImage.getHeight();
blazeBuf = new int[backWidth * backHeight];
blazePalItems = new int[backWidth * backHeight];
for (int i = 0; i < backWidth * backHeight; ++i) {
blazeBuf[i] = 0;
blazePalItems[i] = 0;
}
firstLoad = true;
}
}
class BlazeThread extends Thread {
boolean running = true;
public void setRunning(boolean running) {
this.running = running;
}
@Override
public void run() {
Canvas c = null;
while (running) {
c = mSurfaceHolder.lockCanvas();
doDraw(c);
makFire();
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
private void makFire() {
int x = 0, y = 0;
int fireoffset = 0;
int firevalue = 0;
int backWidthAdd1 = backWidth + 1;
int backWidthSub1 = backWidth - 1;
int heightSize = backHeight - 1;
int widthSize = backWidth - 1;
int tmpVal = 0;
// 绘制最后一行。。。
int wmh = backWidth * (backHeight - 2) - 1;
int ranV = randNums.nextInt();
for (x = 0; x < backWidth; x += ranV % 3) {
tmpVal = wmh + x;
if (ranV % 2 != 0)
blazePalItems[tmpVal] = 511;
else
blazePalItems[tmpVal] = 0;
blazeBuf[tmpVal] = palette[blazePalItems[tmpVal]];
ranV = randNums.nextInt();
}
// 计算环境像素
for (y = 1; y < heightSize; ++y) {
for (x = 1; x < widthSize; ++x) {
fireoffset = (y * backWidth) + x;
// blazeBuf[fireoffset] = palette[(int)(x * 1.0f / widthSize * 512)];
firevalue = ((
blazePalItems[fireoffset - backWidth]
+ blazePalItems[fireoffset + backWidth]
+ blazePalItems[fireoffset + 1]
+ blazePalItems[fireoffset - 1]
+ blazePalItems[fireoffset - backWidthAdd1]
+ blazePalItems[fireoffset - backWidthSub1]
+ blazePalItems[fireoffset + backWidthSub1]
+ blazePalItems[fireoffset + backWidthAdd1])
>> 3);
if (firevalue > 0) // is it black?
{
--firevalue; // Nope. Beam me down Scotty.
tmpVal = fireoffset - backWidth;
blazePalItems[tmpVal] = firevalue;
blazeBuf[tmpVal] = palette[blazePalItems[tmpVal]]; // Plot that
// new color
// on the above pixel
// remember the typewriter analogy
}
}
}
}
protected void doDraw(Canvas canvas) {
canvas.drawBitmap(blazeBuf, 0, backWidth, 0, 0, backWidth, backHeight,
true, paint);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
mBlazeThread.setRunning(true);
mBlazeThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
mBlazeThread.setRunning(false);
// 非暴力关闭线程,直到此次该线程运行结束之前,主线程停止运行,以防止Surface被重新激活
while (retry) {
try {
mBlazeThread.join(); // 阻塞current
// Thread(当前执行线程)直到被调用线程(thread)完成。
retry = false;
} catch (InterruptedException e) {
}
}
}
}
yxkfw
- 粉丝: 82
- 资源: 2万+
最新资源
- 教师入职培训岗前培训.pptx
- 病毒性肝炎预防和治疗.pptx
- xtuoj平方数及其倍数
- Delphi 12 控件之A Guide To Using The TClientDataSet in Delphi Applications.pdf
- 220v转12v开关电源电路仿真 Multisim仿真 该电路增加了光耦合器和可调精密并联稳压器r4为限流电阻,r5r6为取样电阻 当U0变化时,取样电压与TL431内部基准电压进行比较,实现精密
- 添加系统级res资源包
- 基于MPC的分布式电动汽车协同自适应巡航控制,采用上下分层控制方式,上层控制器采用模型预测控制mpc方式,产生期望的加速度,下层根据期望的加速度分配扭矩;仿真结果良好,能够实现前车在加减速情况下,规划
- 丛枝菌根真菌对Cd胁迫下柳...和能源品质的影响及机理研究_孙红.caj
- python renlizhiliao
- 基于MPC算法实现的车辆稳定性控制,建立了横摆角速度r、侧向速度、前后质心侧偏角动力学模型作为预测模型,同时考虑车辆的稳定性可通过控制车辆的侧向速度维持在一定范围内保证车辆的稳定性,因此在模型预测控制
- 基于HTML的圣诞树完整代码,有很好的参考意义
- Delphi 12 控件之Bergsoft NextSuite (VCL) v6.35.0 (08 Dec 2024) for Delphi & CB 6-12 Athens Full Source
- 基于pid控制的超车轨迹跟踪,能够很好的跟踪期望轨迹 有详细的说明文档
- TSNE特征可视化,能够在PyCharm中很好的展示数据训练情况
- Delhhi 12 控件之NxAssociates6.pas
- 咸鱼之王H5稀有卡牌回合手游经典版,修复内购bug,可完美运营
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈