gpt4 book ai didi

android - 无缝背景作为自定义 View 组件,onDraw 不会被 invalidate() 调用

转载 作者:行者123 更新时间:2023-11-29 02:09:46 34 4
gpt4 key购买 nike

friend ,

我想制作一个无缝背景作为自定义 View 组件。

调试时发现的问题,

我第一次在线程中调用 invalidate() 时,调用了我的 onDraw 回调方法。其他时候我的线程正在调用 invalidate(),但不会调用 onDraw 回调方法。

所以它只是在 invalidate() 方法上运行,就好像它根本不存在一样。

应用程序显示 seamlessbackground png。而是静态的。它没有得到更新。

我发布了我所有的代码,因为错误可能在线程之外,invalidate() 所在的位置或 onDraw 方法之外。

感谢任何形式的帮助!所以谢谢

              public class MyBringBackSurface extends View implements Runnable {

Bitmap bitmapResource;
int leftFrameX, rightFrameX, startX, stopX, bitmapPixelWidth,
bitmapPixelHight;
int pixelPerFrame = 10;
int framerate = 100;
int threadSleepTime;
int offsetYInPixel = 200;
Thread ourthread = null;
boolean isrunning = false;

Canvas c;

public MyBringBackSurface(Context context) {
super(context);

init();

}

public MyBringBackSurface(Context context, AttributeSet attrs) {
super(context, attrs);

init();

}

public MyBringBackSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);

init();

}

private void init() {


// Set up Bitmap Resource

bitmapResource = BitmapFactory.decodeResource(getResources(),
R.drawable.simelessbackground);


// c = new Canvas(bitmapResource);


// Implementing leftFrameX and rightFrameX
leftFrameX = 0;
rightFrameX = bitmapResource.getWidth();

// Calculating the Thread sleep time
threadSleepTime = 1000 / framerate;



}

public void pause() { // destroy the currently running thread because
// anyways in the on resume will be created a
// new one again

isrunning = false;
while (true) {

try { // goes through this thread until our thread died
ourthread.join(); // Blocks the current Thread
// (Thread.currentThread()) until the
// receiver finishes its execution and
// dies.
} catch (InterruptedException e) {

e.printStackTrace();
}
break;
}


}

public void resume() {

isrunning = true;
ourthread = new Thread(this);

ourthread.start();

}

public void run() {

while (isrunning) {

try {
Thread.sleep(threadSleepTime);
// formula is 1000/ sleep time (here 5) = frame rate
} catch (InterruptedException e) {

e.printStackTrace();
}

invalidate();

// Add pixelPerFrame and draw again
leftFrameX = leftFrameX - pixelPerFrame;
rightFrameX = rightFrameX - pixelPerFrame;

// if picture is completely out of the screen, start over again
if (leftFrameX <= -bitmapResource.getWidth()) {
leftFrameX = 0;
rightFrameX = bitmapResource.getWidth();
}
}
}

/**
* Render the text
*
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);


canvas.drawRGB(255, 255, 255);

// Draw Rectangle 1250 pixel
Rect rect1000 = new Rect();
rect1000.set(0, 0, 1250, 20);
Paint blue = new Paint();
blue.setColor(Color.BLUE);
canvas.drawRect(rect1000, blue);

// Draw first Bitmap
Rect rectBitmapSource = new Rect(0, 0, bitmapResource.getWidth(),
bitmapResource.getHeight());
Rect rectBitmapDestinationFirst = new Rect(leftFrameX, offsetYInPixel,
rightFrameX, offsetYInPixel + bitmapResource.getHeight());
canvas.drawBitmap(bitmapResource, rectBitmapSource,
rectBitmapDestinationFirst, null);

// Draw second Bitmap

Rect rectBitmapDestinationSecond = new Rect(
(leftFrameX + bitmapResource.getWidth()), offsetYInPixel,
(rightFrameX + bitmapResource.getWidth()), offsetYInPixel
+ bitmapResource.getHeight());
canvas.drawBitmap(bitmapResource, rectBitmapSource,
rectBitmapDestinationSecond, null);



}

}

最佳答案

我通过谷歌文档找到了答案。

如果你想使 UI-Thread 之外的 View 无效,你必须使用postinvalidate() 命令而不是 invalidate()。

就是这样。它会像魅力一样工作!!!

关于android - 无缝背景作为自定义 View 组件,onDraw 不会被 invalidate() 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8149993/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com