2

朋友们,

我想将无缝背景作为自定义视图组件。

我在调试过程中发现的问题,

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

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

应用程序显示无缝背景 png。但作为静态。它没有得到更新。

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

apprechate任何形式的帮助!所以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);



}

}

4

1 回答 1

1

经过googledocs后,我找到了答案。

如果您想从 UI 线程外部使视图无效,则必须使用 postinvalidate() 命令而不是 invalidate()。

而已。它会像魅力一样工作!!!

于 2011-11-29T10:27:05.883 回答