at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1225)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at android.view.View.requestLayout(View.java:23093)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
at android.view.View.requestLayout(View.java:23093)
at android.widget.TextView.checkForRelayout(TextView.java:8908)
at android.widget.TextView.setText(TextView.java:5730)
at android.widget.TextView.setText(TextView.java:5571)
at android.widget.TextView.setText(TextView.java:5528)
at com.ding.carshdemo.MainActivity$1.run(MainActivity.java:27)
和猜想一致,那么ViewRootImpl是什么时候被启动起来的呢? 在Android 绘制原理浅析【干货】 中提到,当Activity准备好后,最终会调用到Activity中的makeVisible,并通过WindowManager添加View,代码如下
//Activity
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
看一下wm addView方法
//WindowManagerImpl
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
applyDefaultTok
en(params);
mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
}
在看一下mGlobal.addView方法
//WindowManagerGlobal
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
ViewRootImpl root;
…
View panelParentView = null;
synchronized (mLock) {
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
}
…
}
终于找到了ViewRootImpl的创建。那么回到上面makeVisible是什么时候被调用到的呢? 看Activity启动流程时,我们知道,Ativity的启动和AMS交互的代码在ActivityThread中,搜索makeVisible方法,可以看到调用地方为
//ActivityThrea
public void handleResumeActivity(IBinder token, boolean finalStateRequest, boolean isForward,
String reason) {
…
if (r.activity.mVisibleFromClient) {
r.activity.makeVisible();
}
…
}
private void updateVisibility(ActivityClientRecord r, boolean show) {
…
if (show) {
if (!r.activity.mVisibleFromServer) {
if (r.activity.mVisibleFromClient) {
r.activity.makeVisible();
}
…
}
//调用updateVisibility地方为
handleStopActivity() handleWindowVisibility() handleSendResult()
这里我们只关注ViewRootImpl创建的第一个地方,从Acitivity声明周期handleResumeActivity会被优先调用到,也就是说在handleResumeActivity启动后(OnResume),ViewRootImpl就被创建了,这个时候,就无法在在子线程中访问UI了,上面子线程延迟了一会,handleResumeActivity已经被调用了,所以发生了崩溃。
SurfaceView是为什么可以直接子线程绘制呢?
在Android 绘制原理浅析【干货】 提到了,我们一般的View有一个Surface,并且对应SurfaceFlinger的一块内存区域。这个本地Surface和View是绑定的,他的绘制操作,最终都会调用到ViewRootImpl,那么这个就会被检查是否主线程了,所以只要在ViewRootImpl启动后,访问UI的所有操作都不可以在子线程中进行。
那SurfaceView为什么可以子线程访问他的画布呢?如下
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView surfaceView = findViewById(R.id.sv);
surfaceView.getHolder().addCallback(this);
}
@Override
public void surfaceCreated(final SurfaceHolder holder) {
new Thread(new Runnable() {
@Override
public void run() {
while (true){
Canvas canvas = holder.lockCanvas();
canvas.drawColor(Color.RED);
holder.unlockCanvasAndPost(canvas);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
其实查看SurfaceView的代码,可以发现他自带一个Surface
public class SurfaceView extends View implements ViewRootImpl.WindowStoppedCallback {
…
final Surface mSurface = new Surface();
…
}
在SurfaceView的updateSurface()中
protected void updateSurface() {
…
if (creating) {
//View自带Surface的创建
mSurfaceSession = new SurfaceSession(viewRoot.mSurface);
mDeferredDestroySurfaceControl = mSurfaceControl;
updateOpaqueFlag();
final String name = "SurfaceView - " + viewRoot.getTitle().toString();
mSurfaceControl = new SurfaceControlWithBackground(
name,
(mSurfaceFlags & SurfaceControl.OPAQUE) != 0,
new SurfaceControl.Builder(mSurfaceSession)
.setSize(mSurfaceWidth, mSurfaceHeight)
.setFormat(mFormat)
.setFlags(mSurfaceFlags));
}
//SurfaceView 中自带的Surface
if (creating) {
mSurface.copyFrom(mSurfaceControl);
}
…
}
SurfaceView中的mSurface也有在SurfaceFlinger对应的内存区域,这样就很容易实现子线程访问画布了。
这样设计有什么不好的地方吗?
因为这个 mSurface 不在 View 体系中,它的显示也不受 View 的属性控制,所以不能进行平移,缩放等变换,也不能放在其它 ViewGroup 中,一些 View 中的特性也无法使用。
别踩百块
我们知道SurfaceView可以在子线程中刷新画布(所称的离屏刷新),那做一些刷新频率高的游戏,就很适合.下面我们开始撸一个前些年比较火的小游戏。
看游戏分为几个步骤,这里主要讲一下原理和关键代码(下面有完整代码地址)
- 绘制一帧
- 动起来
- 手势交互
- 判断游戏是否结束
- 优化内存
绘制一帧
我们把一行都成一个图像,那么他有一个黑色块,和多个白色块组成. 那就可以简单抽象为:
public class Block {
private int height;
private int top;
private int random = 0; //第几个是黑色块
}
绘制逻辑
public void draw(Canvas canvas,int random){
this.random=random;
canvas.save();
for(int i=0;i<WhiteAndBlack.DEAFAUL_LINE_NUME;i++){
if(random == i){
blackRect=new Rect(left+iwidth,top,width+widthi,top+height);
canvas.drawRect(left+iwidth,top,width+widthi,top+height,mPaint);
}else if(error == i){
canvas.drawRect(left+iwidth,top,width+widthi,top+height, errorPaint);
}else{
canvas.drawRect(left+iwidth,top,width+widthi,top+height,mDefaultPaint);
}
}
canvas.restore();
}
那么一行的数据有了,我只需要一个List就可以绘制一屏幕的数据
//List list;
private void drawBg() {
synchronized (list) {
mCanvas.drawColor(Color.WHITE);
if (list.size() == 0) {
for (int i = 0; i <= DEAULT_HEIGHT_NUM; i++) {
addBlock(i);
}
} else {
…
}
}
}
private void addBlock(int i) {
Block blok = new Block(mContext);
blok.setTop(mHeight - (mHeight / DEAULT_HEIGHT_NUM) * i);
int random = (int) (Math.random() * DEAFAUL_LINE_NUME);
blok.draw(mCanvas, random);
list.add(blok);
}
要让其动起来
SurfaceView在不断的刷新,那么只要让List里面的数据每一行的top不断增加,下面没有数据了,直接添加到上面
//SurfaceView 新开的子线程Thread
@Override
public void run() {
isRunning=true;
while (isRunning){
draw();
}
}
private void draw() {
try {
mCanvas = mHolder.lockCanvas();
if(mCanvas !=null) {
drawBg();
// removeNotBg();
// checkGameover(-1,-1);
}
}catch (Exception e){
}finally {
mHolder.unlockCanvasAndPost(mCanvas);
}
}
private void drawBg() {
synchronized (list) {
mCanvas.drawColor(Color.WHITE);
if (list.size() == 0) {
…
} else {
for (Block block : list) {
//top 不断添加
block.setTop(block.getTop() + mSpeend);
block.draw(mCanvas, block.getRandom());
}
if (list.get(list.size() - 1).getTop() >= 0) {
Block block = new Block(mContext);
block.setTop(list.get(list.size() - 1).getTop() - (mHeight / DEAULT_HEIGHT_NUM));
int random = (int) (Math.random() * DEAFAUL_LINE_NUME);
block.draw(mCanvas, random);
//如果上面的top出去了,那下面在加一个block
list.add(block);
}
}
mCanvas.drawText(String.valueOf(count),350,mHeight/8,textPaint);
}
}
手势交互
如果用户黑块点击了,就开始游戏,如果已经开始,那么点击了正确的黑块,就绘制成灰色并加速,并检查游戏是否结束了
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(isRunning) {
checkGameover((int) event.getX(), (int) event.getY());
}else{
count=0;
list.clear();
mSpeend=0;
thread = new Thread(this);
thread.start();
}
break;
}
return super.onTouchEvent(event);
}
绘制灰色代码见下面
判断游戏是否结束了
- 下面到屏幕底端了,还未点击
- 点击错误
private boolean checkGameover(int x,int y){
synchronized (list) {
for (Block block : list) {
if(x !=-1 && y !=-1) {
if (block.getBlackRect().contains(x, y)) {
count++;
if(mSpeend == 0){
mSpeend=DensityUtils.dp2px(getContext(),10);
}else if(mSpeend <=10){
mSpeend+=DensityUtils.dp2px(getContext(),2);
}else if(count == 60){
mSpeend+=DensityUtils.dp2px(getContext(),2);
} else if(count == 100){
mSpeend+=DensityUtils.dp2px(getContext(),2);
}else if(count == 200){
mSpeend+=DensityUtils.dp2px(getContext(),1);
} else if(count == 300){
mSpeend+=DensityUtils.dp2px(getContext(),1);
} else if(count == 400){
mSpeend+=DensityUtils.dp2px(getContext(),1);
}
block.setBlcakPaint();
} else if (y > block.getTop() && y < block.getTop() + block.getHeight()) {
isRunning = false;
block.setError(x / block.getWidth());
}
}else{
if(block.getTop()+block.getHeight()-50 >=mHeight && !block.isChick()){
isRunning=false;
block.setError(block.getRandom());
}
}
}
}
return false;
}
最后优化一下内存
因为我们在不断的添加block,玩一会内存就爆了,可以学习ListView,划出屏幕后上方就移除.
private void removeNotBg() {
synchronized (list) {
for (Block block : list) {
if (block.getTop() >= mHeight) {
needRemoveList.add(block);
}
}else{
if(block.getTop()+block.getHeight()-50 >=mHeight && !block.isChick()){
isRunning=false;
block.setError(block.getRandom());
}
}
}
}
return false;
}
最后优化一下内存
因为我们在不断的添加block,玩一会内存就爆了,可以学习ListView,划出屏幕后上方就移除.
private void removeNotBg() {
synchronized (list) {
for (Block block : list) {
if (block.getTop() >= mHeight) {
needRemoveList.add(block);
}