我已经编写了一些代码来在触摸事件上 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/