我想在图像视图上绘制画布我正在使用手指画的概念,但它不起作用
public class show_image extends Activity implements OnTouchListener,ColorPickerDialog.OnColorChangedListener {
ImageView img_vw_show;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
float x1, y1, x2, y2;
Bitmap bmp;
int i = 1, width, height;
private static final float MINP = 0.25f;
// private static final float MAXP = 0.75f;
int w, h;
Bitmap mBitmap, bMap, newBitmap;
Canvas mCanvas;
Path mPath;
Paint mBitmapPaint, mPaint;
MaskFilter mEmboss;
MaskFilter mBlur;
BufferedInputStream buf;
MyView mv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_image);
System.out.println("oncreate method called");
img_vw_show = (ImageView) this.findViewById(R.id.img_vw_show);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);
// mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
img_vw_show.setOnTouchListener((OnTouchListener) this);
String responseImagePath = this.getIntent().getStringExtra(
"responseImagePath");
String selectedImagePath = this.getIntent().getStringExtra(
"selectedImagePath");
System.out.println("responseImagePath in show image========"
+ responseImagePath);
System.out.println("selectedImagePath in show image========"
+ selectedImagePath);
Boolean selectedImagePathFlag = false;
if (selectedImagePath == null) {
selectedImagePathFlag = true;
} else if (selectedImagePath.equals("")) {
selectedImagePathFlag = true;
}
if (selectedImagePathFlag) {
if (responseImagePath != null && !responseImagePath.equals("")) {
URL url1;
InputStream in;
try {
url1 = new
// URL("http://mail.sshanghvi.com:8080/sacplupload/checklist/"+responseImagePath);
URL("http://192.168.1.49:82/uploads/" + responseImagePath);
in = url1.openStream();
// Read the inputstream
buf = new BufferedInputStream(in);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of
// 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bMap = BitmapFactory.decodeStream(buf, null, o2);
img_vw_show.setImageBitmap(bMap);
width = bMap.getWidth();
height = bMap.getHeight();
System.out.println("Width and Height is==" + width + " & "
+ height);
mv = new MyView(show_image.this, bMap);
System.out.println("Setting the Myview");
mv.invalidate();
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
} else {
Toast.makeText(getApplicationContext(), "Image Not Available",
Toast.LENGTH_SHORT).show();
}
} else {
File imgFile = new File(selectedImagePath);
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false; // Disable Dithering mode
bfOptions.inPurgeable = true; // Tell to gc that whether it needs
// free memory, the Bitmap can be
// cleared
bfOptions.inInputShareable = true; // Which kind of reference will
// be used to recover the Bitmap
// data after being clear, when
// it will be used in the future
bfOptions.inTempStorage = new byte[32 * 1024];
Bitmap myBitmap = BitmapFactory.decodeFile(
imgFile.getAbsolutePath(), bfOptions);
img_vw_show.setImageBitmap(myBitmap);
}
}
public class MyView extends View {
public MyView(Context c, Bitmap img) {
super(c);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(8);
setWillNotDraw(false);
mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6,
3.5f);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
Bitmap newBitmap;
Canvas newCanvas;
public void onDraw(Canvas canvas) {
super.onDraw(newCanvas);
newBitmap= Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
newCanvas= new Canvas(newBitmap);
System.out.println("canvas == "+canvas);
System.out.println("newCanvas == "+newCanvas);
System.out.println("onDraw Called");
img_vw_show.draw(newCanvas);
newCanvas.drawBitmap(newBitmap, 0, 0, mBitmapPaint);
newCanvas.drawPath(mPath, mPaint);
img_vw_show.setImageBitmap(newBitmap);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
System.out.println("Touch Start Called");
onDraw(newCanvas);
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
System.out.println("Touch Move Called");
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
System.out.println("Touch Up Called");
mPath.lineTo(mX, mY);
// commit the path to our offscreen
newCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
}
private static final int COLOR_MENU_ID = Menu.FIRST;
private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
private static final int BLUR_MENU_ID = Menu.FIRST + 2;
private static final int ERASE_MENU_ID = Menu.FIRST + 3;
private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');
/****
* Is this the mechanism to extend with filter effects? Intent intent =
* new Intent(null, getIntent().getData());
* intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
* menu.addIntentOptions( Menu.ALTERNATIVE, 0, new ComponentName(this,
* NotesList.class), null, intent, 0, null);
*****/
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
mPaint.setXfermode(null);
mPaint.setAlpha(0xFF);
switch (item.getItemId()) {
case COLOR_MENU_ID:
new ColorPickerDialog(this, this, mPaint.getColor()).show();
return true;
case EMBOSS_MENU_ID:
if (mPaint.getMaskFilter() != mEmboss) {
mPaint.setMaskFilter(mEmboss);
} else {
mPaint.setMaskFilter(null);
}
return true;
case BLUR_MENU_ID:
if (mPaint.getMaskFilter() != mBlur) {
mPaint.setMaskFilter(mBlur);
} else {
mPaint.setMaskFilter(null);
}
return true;
case ERASE_MENU_ID:
// mPaint.setXfermode(new
// PorterDuffXfermode(PorterDuff.Mode.CLEAR));
return true;
case SRCATOP_MENU_ID:
// mPaint.setXfermode(new
// PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
mPaint.setAlpha(0x80);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void colorChanged(int color) {
}
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mv.touch_start(x, y);
v.invalidate();
break;
case MotionEvent.ACTION_MOVE:
mv.touch_move(x, y);
v.invalidate();
break;
case MotionEvent.ACTION_UP:
mv.touch_up();
v.invalidate();
break;
}
return true;
}
}