无缝背景作为自定义视图组件,onDraw 不会被 invalidate() 调用
朋友们,
我想制作一个无缝背景作为自定义视图组件。
我在调试过程中发现的问题是,
第一次在线程中调用 invalidate() 时,我的 onDraw 回调方法被调用。 其他时候,我的线程调用 invalidate() 时,不会调用 onDraw 回调方法。
所以它只是运行 invalidate() 方法,就像它根本不存在一样。
该应用程序显示无缝背景 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);
}
}
Friends,
i want to make a seamless background as a custom view component.
the problem i figured out during debugging,
the first time i call invalidate() within the thread my onDraw callback method is called.
The other times my thread is calling invalidate() the onDraw callback method is not called.
so its just running over the invalidate() method , like it would not even exist.
The application displays the seamlessbackground png. But as static. it not gets updated.
I post all my code because the bug might be outside of the thread, where invalidate() is located or outside the onDraw method.
apprechate any kind of help! so THX
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在浏览了谷歌文档后找到了答案。
如果你想从 UI 线程外部使视图失效,你必须使用
postinvalidate() 命令而不是 invalidate()。
就是这样。它会像魅力一样发挥作用!
I figured out the answer after going throught the googledocs.
if you want to invalidate a View from outside of the UI-Thread you have to use the
postinvalidate() command instead of invalidate().
Thats it. It will work like charme!!!