2012-01-05 3 views
1

タッチイベントでビットマップを移動するコードを記述しました。残念なことに、createBitmapが呼び出されたときにコードがクラッシュし、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

関連する問題