友
我想将无缝背景作为自定义视图组件。
我在调试过程中发现的问题,
我第一次在线程中调用invalidate()调用我的onDraw回调方法。 其他时候我的线程调用invalidate(),不调用onDraw回调方法。
所以它只是运行invalidate()方法,就像它甚至不存在一样。
应用程序显示无缝背景png。但作为静态。它没有得到更新。
我发布了所有代码,因为错误可能在线程之外,其中invalidate()位于onDraw方法之外或者在onDraw方法之外。
获取任何帮助!所以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);
}
}
答案 0 :(得分:1)
经过googledocs后我找到了答案。
如果要从UI-Thread外部使View无效,则必须使用 postinvalidate()命令而不是invalidate()。
多数民众赞成。它会像魅力一样工作!!!