2014-01-13 14 views
0

編輯我註釋掉的代碼從PhotoSortrView.load()重用信息的onResume() - Android電子

if (this.maxX < SCREEN_MARGIN) 
        cx = SCREEN_MARGIN; 
       else if (this.minX > displayWidth - SCREEN_MARGIN) 
        cx = displayWidth - SCREEN_MARGIN; 
       if (this.maxY > SCREEN_MARGIN) 
        cy = SCREEN_MARGIN; 
       else if (this.minY > displayHeight - SCREEN_MARGIN) 
        cy = displayHeight - SCREEN_MARGIN; 

而現在它的工作原理。這可能是一個黑客修復。專業修復可能在檢查minX和顯示寬度,並查看爲什麼當它在屏幕上時觸發if語句。也許它應該改爲this.centreX而不是this.minX。那些如果陳述太頻繁。無論如何評論它的工作原理,我現在很滿意。謝謝。

我正在使用這個真棒圖書館:https://code.google.com/p/android-multitouch-controller/特別是它提供的示例應用程序的PhotoSortrView類。我爲任何人提供足夠的代碼來嘗試和幫助。

它工作得很好。它使我的圖像可以通過觸摸手勢進行拖動,旋轉和縮放。然而,我不斷離開我的活動來實現它,選擇新的圖像,然後添加圖像並重新輸入活動。看起來好像它必須調用onPause()和onResume(),它們必須調用unloadImages()和loadImages(),否則會導致我的應用程序崩潰,因爲它試圖繪製圖像爲null。但是,它們會導致以前的圖像每次都在屏幕上的不同位置加載。如果我不能調用這些圖片,那麼圖片不會被卸載,只是有一個較慢的應用程序,但圖片保持其位置,或重新使用圖像位置和縮放信息,以便每次都重新加載到相同的位置,這將非常棒。這是代碼。當我說我的活動時,這一切都發生在這一切活動中。唯一涉及的其他類是PhotoSortrView。

我的動態類聲明:

public class MyActivity extends Activity implements OnClickListener { 

我Activity.onPause():

@Override 
    protected void onPause() { 
     super.onPause(); 
     photoSorter.unloadImages(); 
    } 

我Activity.onResume():

@Override 
    protected void onResume() { 
     super.onResume(); 
     photoSorter.loadImages(this); 
    } 

PhotoSortrView類聲明:

public class PhotoSortrView extends View implements MultiTouchObjectCanvas<PhotoSortrView.Img> { 

PhotoSortrView.unloadImages():

/** Called by activity's onPause() method to free memory used for loading the images */ 
    public void unloadImages() { 
     int n = mImages.size(); 
     for (int i = 0; i < n; i++){ 
      //this.imgX = mImages.get(i).getCenterX(); 
      //this.imgY = mImages.get(i).getCenterY(); 
      mImages.get(i).unload(); 
     } 
    } 

PhotoSortrView.loadImages()(注意這個調用PhotoSortrView.load())

/** Called by activity's onResume() method to load the images */ 
    public void loadImages(Context context) { 
     Resources res = context.getResources(); 
     int n = mImages.size(); 
     for (int i = 0; i < n; i++) 
      mImages.get(i).load(res); 
    } 

PhotoSortrView.load():

/** Called by activity's onResume() method to load the images */ 
     public void load(Resources res) { 
      getMetrics(res); 
      this.drawable = res.getDrawable(resId); 
      this.width = drawable.getIntrinsicWidth(); 
      this.height = drawable.getIntrinsicHeight(); 
      float cx, cy, sx, sy; 
      if (firstLoad) { 
       cx = SCREEN_MARGIN + (float) (Math.random() * (displayWidth - 2 * SCREEN_MARGIN)); 
       cy = SCREEN_MARGIN + (float) (Math.random() * (displayHeight - 2 * SCREEN_MARGIN)); 
       float sc = (float) (Math.max(displayWidth, displayHeight)/(float) Math.max(width, height) * Math.random() * 0.3 + 0.2); 
       sx = sy = sc; 
       firstLoad = false; 
      } else { 
       // Reuse position and scale information if it is available 
       // FIXME this doesn't actually work because the whole activity is torn down and re-created on rotate 
       cx = this.centerX; 
       cy = this.centerY; 
       sx = this.scaleX; 
       sy = this.scaleY; 
       // Make sure the image is not off the screen after a screen rotation 
       if (this.maxX < SCREEN_MARGIN) 
        cx = SCREEN_MARGIN; 
       else if (this.minX > displayWidth - SCREEN_MARGIN) 
        cx = displayWidth - SCREEN_MARGIN; 
       if (this.maxY > SCREEN_MARGIN) 
        cy = SCREEN_MARGIN; 
       else if (this.minY > displayHeight - SCREEN_MARGIN) 
        cy = displayHeight - SCREEN_MARGIN; 
      } 
      setPos(cx, cy, sx, sy, 0.0f); 
     } 

請注意PhotoSortrView.load()它表示「重新使用位置和比例尺信息(如果可用)。 FIXME這不實際工作,因爲整個活動被拆除,並在旋轉時重新創建「

現在我的應用程序將不會實現屏幕旋轉,它將永遠是肖像,但我還沒有實現這一點。已經測試了應用程序,而從未旋轉它,並且圖像在重新加載時不保留其位置和縮放比例。我是否應該只將肖像放入我的應用程序中以查看其是否有效?或者查找第三方庫中的旋轉代碼並禁用它或者試圖讓我自己的方法來捕捉圖像的位置和縮放並將其應用於重新加載?我可以很容易地獲得圖像位置。

我離開myActivity:

private void chooseImages(int menuId) { 

     Intent intent = new Intent(this, MyActivity.class); 
     intent.putExtra("menuId", menuId); 
     startActivityForResult(intent, 1); 
    } 

我回:

@SuppressLint("NewApi") 
     @Override 
     public void onActivityResult(int requestCode, int resultCode, Intent data) {  
      super.onActivityResult(requestCode, resultCode, data); 
      switch(requestCode) { 
      case (1) : { 
       if (resultCode == Activity.RESULT_OK) { 
       //add LInearLayout 
       RelativeLayout myLInearLayout =(RelativeLayout) findViewById(R.id.layout); 
       //String newText = data.getStringExtra("imagePath"); 
       int drawable = data.getIntExtra("imagePath", 0); 
       photoSorter.addDrawable(drawable, this); 

       //Log.d("tag", "I have set the text " + newText); 
       } 
       break; 
      } 
      } 
     } 

我活動的onCreate:

PhotoSortrView photoSorter; 

    public static String getmProfilePicPath(){ 
     return mProfilePicPath; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if(savedInstanceState != null) { 
      Toast.makeText(this, savedInstanceState.getString("message"), Toast.LENGTH_LONG).show(); 
      Log.d("message", savedInstanceState.getString("message")); 
      } 
     setContentView(R.layout.activity_dressing_room); 
     mDb = new dbhelper(this); 
     this.mName = getMname(); 
     AppUser user = mDb.getAppUser(mName); 
     mProfilePicPath = user.getImagePath(); 
     setBackgroundImage(); 
     photoSorter = new PhotoSortrView(this); 
     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     this.addContentView(photoSorter, params); 

     gd = new GestureDetector(this, new MyGestureDetector()); 
     otl = new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       // TODO Auto-generated method stub 
       return gd.onTouchEvent(event); 
      } 
     }; 
     photoSorter.setOnClickListener(this); 
     photoSorter.setOnTouchListener(otl); 
    } 

回答

0

您應該使用活動onSaveInstanceState,以節省您需要的時候任何信息該活動被銷燬,並且該信息將通過該包中傳遞onCreate

我不知道爲什麼你需要在每一個onPause和onResume上都這樣做,你能不能卸載並重新加載圖像?

+0

謝謝!我在myActivity.onPause()中註釋了'photoSorter.unloadImages();'。沒有例外,但圖像仍然移動。我想如果我編輯PhotoSortrView.load()我可以阻止他們移動。如果你也要在我的問題中看看這種方法,並且看到你認爲會非常感謝的東西,謝謝。 – user3164083

+0

如果我註釋掉'photoSorter.loadImages(this);'它會在Image.draw方法中導致nullPointerException。我猜沒有加載圖像,他們不在那裏。但我不會卸下它們,這很奇怪。 – user3164083

+0

在執行onSaveInstance狀態時也遇到了麻煩。我從[here](http://stackoverflow.com/a/6525808/3164083)添加了代碼給我的活動,但它從未解僱,因爲它沒有做出敬酒信息。它不會添加到PhotoSortrView,因爲它的超類沒有實現它。謝謝。 – user3164083