我正在制作一个位图上的绘画应用程序,之前已经多次询问了here和here
我在我的应用程序中使用它们似乎没有工作,这是我使用的代码:
public class EditActivity extends Activity implements OnTouchListener,
OnClickListener {
GestureDetector gd;
View.OnTouchListener gl;
RelativeLayout parent;
ImageView im;
Bitmap bmp;
Bitmap alteredBitmap;
Canvas canvas;
Paint paint;
Matrix matrix;
Rect imageBounds;
Path mPath;
int dw;
int dh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit);
parent = (RelativeLayout) findViewById(R.id.rl);
matrix = new Matrix();
parent.setBackgroundColor(Color.BLACK);
gd = new GestureDetector(this, new MyGestureDetector());
gl = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mPath.moveTo(x,y);
im.invalidate();
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(x,y);
im.invalidate();
break;
case MotionEvent.ACTION_UP:
// canvas.drawPath(mPath, paint);
mPath.reset();
im.invalidate();
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
break;
}
return gd.onTouchEvent(event);
} else {
return gd.onTouchEvent(event);
}
}
};
im = (ImageView) findViewById(R.id.ImageView);
Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri"));
Display currentDisplay = getWindowManager().getDefaultDisplay();
dw = currentDisplay.getWidth();
dh = currentDisplay.getHeight();
try {
// Load up the image's dimensions not the image itself
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri), null, bmpFactoryOptions);
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
bmp.getHeight(), bmp.getConfig());
mPath = new Path();
canvas = new Canvas(alteredBitmap);
paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG);
paint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setFilterBitmap(true);
paint.setDither(true);
paint.setAntiAlias(true);
paint.setTextSize(40f);
paint.setTypeface(getTypeFaceFromFile("Roboto-Light"));
canvas.drawBitmap(bmp, matrix, paint);
canvas.save();
im.setImageBitmap(alteredBitmap);
im.setOnClickListener(this);
im.setOnTouchListener(gl);
} catch (FileNotFoundException e) {
Log.v("ERROR", e.toString());
}
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
return super.onDown(event);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return super.onDoubleTap(e);
}
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return super.onDoubleTapEvent(e);
}
}
}
现在,当我通过触摸屏幕绘制路径时,它只会将自己绘制到其他地方,也只会出现在已经由android本身缩放的图像中,而不是在尺寸足够小而无需缩放的图像上,请调查并帮助我
修改 我已经遵循了这个answer,这是我实施后的代码,但现在没有路径......
gl = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
float scaledImageOffsetX = x - imageBounds.left;
float scaledImageOffsetY = y - imageBounds.top;
float originalImageOffsetX = scaledImageOffsetX * widthRatio;
float originalImageOffsetY = scaledImageOffsetY * heightRatio;
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mPath.moveTo(originalImageOffsetX,originalImageOffsetY);
im.invalidate();
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(originalImageOffsetX,originalImageOffsetY);
im.invalidate();
break;
case MotionEvent.ACTION_UP:
// canvas.drawPath(mPath, paint);
mPath.reset();
im.invalidate();
break;
default:
break;
}
return true;
}
}
};
im = (ImageView) findViewById(R.id.ImageView);
Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri"));
Display currentDisplay = getWindowManager().getDefaultDisplay();
dw = currentDisplay.getWidth();
dh = currentDisplay.getHeight();
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(imageFileUri), null, bmpFactoryOptions);
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
bmp.getHeight(), bmp.getConfig());
mPath = new Path();
canvas = new Canvas(alteredBitmap);
paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG);
paint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setFilterBitmap(true);
paint.setDither(true);
paint.setAntiAlias(true);
paint.setTextSize(40f);
paint.setTypeface(getTypeFaceFromFile("Roboto-Light"));
canvas.drawBitmap(bmp, matrix, paint);
canvas.save();
im.setImageBitmap(alteredBitmap);
Drawable drawable = im.getDrawable();
imageBounds = drawable.getBounds();
float intrinsicHeight = drawable.getIntrinsicHeight();
float intrinsicWidth = drawable.getIntrinsicWidth();;
float scaledHeight = imageBounds.height();
float scaledWidth = imageBounds.width();
heightRatio = intrinsicHeight / scaledHeight;
widthRatio = intrinsicWidth / scaledWidth;
im.setOnClickListener(this);
im.setOnTouchListener(gl);
} catch (FileNotFoundException e) {
Log.v("ERROR", e.toString());
}
}
}
谢谢, 截拳道
答案 0 :(得分:-1)
好的,我通过自己修补找到答案,我希望它对别人有用
这是我遵循的步骤
1)计算原始宽度和缩放宽度 2)找到比例 3)将缩放比率乘以事件的x和y坐标,你就可以获得所需的坐标......