2012-01-05 56 views
1

我寫了一些代碼來移動觸摸事件中的位圖。不幸的是,當createBitmapis調用時代碼崩潰,並且xval或yval是0以外的任何值。下面是與此問題相關的代碼。任何援助將不勝感激:當createBitmap被調用時,Android崩潰X或Y座標大於0

public class AndroidBitmap extends Activity { 

private int yval=0; 
private int xval=0; 
Bitmap bitmapOrg; 
private int bmpWidth; 
private int bmpHeight; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.image_main); 
    myImageView = (ImageView)findViewById(R.id.imageview); 

    bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.android); 

    bmpWidth = bitmapOrg.getWidth(); 
    bmpHeight = bitmapOrg.getHeight(); 

    drawMatrix(); 
} 

public boolean onTouchEvent(MotionEvent event) { 
    int eventaction = event.getAction(); 

    switch (eventaction) { 
     case MotionEvent.ACTION_DOWN: 
      // finger touches the screen 
      break; 

     case MotionEvent.ACTION_MOVE: 
      // finger moves on the screen 
      break; 

     case MotionEvent.ACTION_UP: 
      // finger leaves the screen 
      xval = (int) event.getX(); 
      yval = (int) event.getY(); 
      drawMatrix(); 
      break; 
    } 
    // tell the system that we handled the event and no further processing is required 
    return true; 
} 

private void drawMatrix(){ 

    Matrix matrix = new Matrix(); 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, xval, yval, 
      bmpWidth, bmpHeight, matrix, true); 
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 
    myImageView.setImageDrawable(bmd);  
} 
} 

回答

2

xval或yval是0以外的任何東西。是的,這是真的。它必須大於0,否則如果寬度或高度爲< = 0,則會給出IllegalArgumentException的錯誤。在您的代碼中,第一次調用drawMatrix();方法然後xval和yval傳遞0.所以它給出了錯誤。

+0

感謝您的回答,但問題恰恰相反。當xval或yval的值爲0時,代碼工作正常。如果它們更大,它會崩潰。 – user1131553 2012-01-05 19:36:42