当调用createBitmap时,Android崩溃X或Y坐标是> 0

时间:2012-01-05 07:36:50

标签: android bitmap move ontouchevent

我已经编写了一些代码来移动触摸事件上的位图。不幸的是,当调用createBitmapis时代码崩溃,而xval或yval是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);      
}
}

1 个答案:

答案 0 :(得分:2)

xval或yval是除0以外的任何值。是的,它是真的。它必须大于0,否则如果宽度或高度为< = 0,则会出现IllegalArgumentException错误。在第一次调用drawMatrix()时的代码中;方法然后xval和yval传递0.所以它给出了错误。