I have create my own custom view and i've drawn using canvas. After that I'm adding the custom view to the linear layout. iIm not able to apply multi touch for linear layout. Can multi touch applicable for linear Layout? If so please share me a sample snippet.
Here is my code snippet:
package com.hcl.MobileIdeaNapkin;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
public class NapkinActivity extends GraphicsActivity implements ColorPickerDialog.OnColorChangedListener {
private int item_id;
public Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
}
private Paint mPaint;
public void colorChanged(int color) {
mPaint.setColor(color);
}
public class MyView extends View {
private Bitmap mBitmap;
private Canvas mCanvas ;
private Path mPath;
private Paint mBitmapPaint;
public MyView(Context c) {
super(c);
mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(0xFFAAAAAA);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
if(item_id==3){
canvas.drawText(AppModel.contents, 100, 100, mPaint);
}
else{
canvas.d开发者_如何学运维rawPath(mPath, mPaint);
}
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
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() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
private static final int COLOR_MENU_ID = Menu.FIRST;
private static final int ERASE_MENU_ID = Menu.FIRST + 1;
private static final int TEXT_MENU_ID=Menu.FIRST+2;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, COLOR_MENU_ID, 0, "Color");
menu.add(0, ERASE_MENU_ID, 0, "Erase");
menu.add(0,TEXT_MENU_ID,0,"Text");
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:
System.out.println(item.getItemId());
new ColorPickerDialog(this, this, mPaint.getColor()).show();
return true;
case ERASE_MENU_ID:
System.out.println(item.getItemId());
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
return true;
case TEXT_MENU_ID:
item_id=item.getItemId();
System.out.println(item.getItemId());
new TextDialog(this).show();
}
return super.onOptionsItemSelected(item);
}
}
There shouldn't be a problem with the code you have. Have you added:
<uses-feature android:name="android.hardware.touchscreen.multitouch"/>
to your AndroidManifest.xml? Also, I believe you have to target Android 2.0 and above for it to work.
Just some suggestions.
-Dan
精彩评论