2011-08-23 23 views
0

如何在我的應用程序中多次調用繪畫我嘗試使用無效,但它不調用繪畫我認爲任何人都可以提供多次調用繪畫的示例代碼。如何在黑莓中調用繪畫方法

package mypackage; 

import com.rss.logger.Log; 

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.MainScreen; 

/** 
* A class extending the MainScreen class, which provides default standard 
* behavior for BlackBerry GUI applications. 
*/ 
public final class MyScreen extends MainScreen 
{ 
    BitmapField objBitmapField; 
    boolean objBoolean; 
    int postion=0; 
    public MyScreen() 
    {   
     objBitmapField=new BitmapField(Bitmap.getBitmapResource("bb.png")); 
     // Set the displayed title of the screen  
     setTitle("MyTitle"); 
     objBoolean=false; 
     new AnimationThread().start(); 

    } 
    private class AnimationThread extends Thread{ 

     public void run() { 
      super.run(); 
      UiApplication.getUiApplication().invokeLater(new Runnable() { 
        public void run() 
        { 
         objBoolean=true; 
        //Add a new LabelField to the screen. 
        //theScreen.add(new LabelField("Hello there."); 

        //Call the screen’s invalidate method to 
        //force the screen to redraw itself. 
        //Note that invalidate can be called 
        //at the screen, manager or field level, 
        //which means you can inform the 
        //BlackBerry to only redraw the area that 
        //has changed. 

         for (int i = 0; i < 20; i++) { 
          try { 
          sleep(200); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         Log.info("in run"); 
          postion=postion+5; 
          MyScreen.this.invalidate(); 
        } 


        } 
       }); 
     } 

    } 

    protected void paint(Graphics graphics) { 

     super.paint(graphics); 
     Log.info("in paint"); 
     if(objBoolean)   
     graphics.drawBitmap(20, postion, 50, 50, Bitmap.getBitmapResource("bb.png"), 30, 40); 

    } 

} 
+0

請提供你如何調用invalidate()的代碼示例 –

回答

0

嗨,piyus找到工作代碼。 invalidate()需要在paint()方法中調用。有一件事情也很重要,不應該在繪畫方法中創建對象。

import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.container.MainScreen; 

public final class MyScreen extends MainScreen 
{ 
    private boolean objBoolean=false; 
    private int postion=0; 
    private Bitmap bb=Bitmap.getBitmapResource("bb.png"); 

    public MyScreen() 
    {   
     setTitle("MyTitle"); 
     new AnimationThread().start(); 
    } 

    private class AnimationThread extends Thread 
    { 
     public void run() 
     { 
      objBoolean=true; 

      for (int i = 0; i < 20; i++) 
      { 
       try 
       { 
        sleep(200); 
       } 
       catch (InterruptedException e) 
       { 
       } 
       postion=postion+5; 
      } 
     } 
    } 

    protected void paint(Graphics graphics) 
    { 
     super.paint(graphics); 
     if(objBoolean)   
      graphics.drawBitmap(20, postion, 50, 50, bb, 30, 40); 
     invalidate(); 
    } 
} 
+0

位圖的大小必須大於30x40,因爲我們使用drawBitmap方法,如graphics.drawBitmap(20,postion,50,50,bb,30,40); 。 –

+0

好的謝謝你的代碼,我會看到它是如何工作的 – Piyush

+0

我想你想從一個位置移動圖像到另一個。 –

0

paint()方法可以通過使用無效的方法或通過創建你現在使用類的對象被調用。

我認爲在這裏提到的示例代碼中存在錯字錯誤。只有通過定義它,才能在類中創建類。來面對你正面臨的問題

雖然,調用invalidate方法,因爲你正在使用它作爲MyScreen.this.invalidate這是不正確的方式來調用它,你不能調用另一個類的paint方法,就像你的情況一樣是AnimationThread是不可能的。 Screen class和paint方法的特點在於,一旦創建了屏幕對象,它就會被調用。 據我所知,可以在同一個類中調用管理器或屏幕的invalidate方法,但不能通過在另一個類中創建屏幕對象並調用它。

它應該簡單地調用,而不使用任何對象,invalidate();,獲取屏幕重繪或無效。

也可以通過創建同一屏幕的對象來實現它,可以是如果想以不同的方式顯示屏幕,他們可以通過使用不同的參數來創建對象,並在繪製方法中相應地處理它。 在這裏,每當線程運行時,您可以創建一個對象並根據您的選擇進行處理。

作爲一個樣本:

public class theScreen extends MainScreen{ 
int dummy; 
public theScreen(int firstArg){ 
    // Handle the firstArgument here 
    dummy=0; 
} 
public theScreen(int secondArg,int dummy){ 
    // Handle the second Argument here 
    this.dummy = dummy; 
} 
public void paint(Graphics graphcis) 
{ 
    if(dummy == 0) 
    { 
     //Handle what you would like to paint here 
    } 
    else{ 
     //Handle with a different paint here 
    } 
}} 

希望這樣可以解決你的問題 編碼愉快。 乾杯

+0

我想你可能會對BB開發和Java有一些誤解。你當然可以在另一個類中聲明類 - 它們被稱爲內部類。另外,'MyScreen.this.invalidate()'和'invalidate()'是一樣的調用,因爲'Thread'沒有自己的'invalidate()'方法。 – jprofitt

0

您可以嘗試使用Screen.doPaint()

您的位圖大到足以從上/左(30,40)開始完全繪製?它可能只是因爲你不在繪畫的範圍之內,因此它看起來並不像繪畫。