我正在为Android开发一个应用。目标是绘制图像。它可以工作,但是当我在图像上绘画(运行代码时)时,触摸输入会延迟。
我看到了很多绘制图像的方法,并尝试复制它们的示例,但是触摸输入仍然会有所延迟。
下面是我班上的代码
如果您需要更多信息,请问我。
谢谢。
public class ImageActivity extends Activity implements View.OnClickListener,
View.OnTouchListener {
private ImageView photo;
private ImageButton cancel;
private ImageButton savePicture;
private Bitmap bmp;
private Bitmap alteredBitmap;
private Canvas canvas;
private Paint paint;
private Matrix matrix;
private float downx = 0;
private float downy = 0;
private float upx = 0;
private float upy = 0;
private String photoPath;
private File photoFile;
private CreateFileImage createFileImage = new CreateFileImage();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
Intent i = getIntent();
Bundle extra = i.getExtras();
photoPath = extra.getString("photoPath");
photo = this.findViewById(R.id.photo);
isStoragePermissionGranted();
cancel = this.findViewById(R.id.cancel);
savePicture = this.findViewById(R.id.save);
savePicture.setOnClickListener(this);
cancel.setOnClickListener(this);
photo.setOnTouchListener(this);
}
public void onClick(View v) {
if (v == cancel) {
Intent i = new Intent();
setResult(RESULT_CANCELED, i);
finish();
} else if (v == savePicture) {
if (alteredBitmap != null) {
ContentValues contentValues = new ContentValues(3);
contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, "Draw On Me");
Uri imageFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
try {
OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
alteredBitmap.compress(Bitmap.CompressFormat.JPEG, 90, imageFileOS);
Toast t = Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT);
t.show();
} catch (Exception e) {
Log.v("EXCEPTION", e.getMessage());
}
}
}
}
private void setPhoto(){
Uri imageFileUri = Uri.parse(photoPath);
try {
BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
File imageFile = new File(photoPath);
bmpFactoryOptions.inJustDecodeBounds = true;
bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
bmpFactoryOptions.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp
.getHeight(), bmp.getConfig());
canvas = new Canvas(alteredBitmap);
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setXfermode(null);
paint.setAlpha(0xff);
matrix = new Matrix();
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(bmp, matrix, paint);
photo.setImageBitmap(alteredBitmap);
photo.setOnTouchListener(this);
} catch (Exception e) {
Log.v("ERROR", e.toString());
}
}
private Path mPath;
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touchStart(float x, float y){
mPath = new Path();
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downx = getPointerCoords(event)[0];
downy = getPointerCoords(event)[1];
touchStart(downx, downy);
// photo.invalidate();
break;
case MotionEvent.ACTION_MOVE:
upx = getPointerCoords(event)[0];//event.getX();
upy = getPointerCoords(event)[1];//event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
downx = upx;
downy = upy;
// photo.invalidate();
break;
case MotionEvent.ACTION_UP:
upx = getPointerCoords(event)[0];//event.getX();
upy = getPointerCoords(event)[1];//event.getY();
canvas.drawLine(downx, downy, upx, upy, paint);
photo.invalidate();
break;
default:
break;
}
return true;
}
final float[] getPointerCoords(MotionEvent e)
{
final int index = e.getActionIndex();
final float[] coords = new float[] { e.getX(index), e.getY(index) };
Matrix matrix = new Matrix();
photo.getImageMatrix().invert(matrix);
matrix.postTranslate(photo.getScrollX(), photo.getScrollY());
matrix.mapPoints(coords);
return coords;
}
public void isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED &&
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("Permission", "Permission EXTERNAL_STORAGE is granted");
setPhoto();
} else {
Log.v("Permission", "Permission EXTERNAL_STORAGE is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
} else { //permission is automatically granted on sdk<23 upon installation
Log.v("Permission", "Permission EXTERNAL_STORAGE is granted");
setPhoto();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setPhoto();
}
}
}
帮帮我