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

标签 android bitmap move touch-event

我已经编写了一些代码来在触摸事件上 move 位图。不幸的是,当调用 createBitmap 并且 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);      
}
}

最佳答案

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

关于当调用 createBitmap X 或 Y 坐标 > 0 时,Android 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8739101/

相关文章:

android - 将imageView放在android中的textview之后

android - android :actionBarDivider and android:divider for ActionBar styles之间的区别

android - 在不创建新文件的情况下裁剪图像

c++ - 为什么没有 std::uninitialized_move_if_noexcept?

java - 需要 Android 方向技巧

android - 无法解析 xml 中的基本小部件类

android - 如何将存储在数据库中的图像作为字符串显示为真实图像?

c# 在另一个位图上绘制位图

html - Div 仅在缩小时 move

javascript - 如何在 javascript 中将一个元素从一个数组 move 到另一个数组?