shiva prasad

shiva prasad

  • NA
  • 14
  • 3.1k

how to draw a line with various thickness for speed drawing

May 5 2015 5:54 AM

I want to draw a line with various thickness on drawing application in android. when i move the finger speedy it should be some thickness and i move the finger it is some thickness

Here is my code:

public class MainActivity extends Activity implements OnClickListener {
private final String tag = "MainActivity";
private ImageView eraser;
private Button btnChooseImage;
private ImageButton btnClear, btnSave, btnShare, btnCamera;
private DrawingView drawingView;
private static final int SELECT_PHOTO = 100;
private static final int CAMERA_REQUEST = 1888;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
setContentView(R.layout.activity_main);
drawingView = (DrawingView) findViewById(R.id.drawing);
btnChooseImage = (Button) findViewById(R.id.btnChooseImage);
btnChooseImage.setOnClickListener(this);
btnClear = (ImageButton) findViewById(R.id.btnClear);
btnClear.setOnClickListener(this);
btnSave = (ImageButton) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
btnShare = (ImageButton) findViewById(R.id.btnShare);
btnShare.setOnClickListener(this);
btnCamera = (ImageButton) findViewById(R.id.btnCamera);
btnCamera.setOnClickListener(this);
eraser = (ImageView) findViewById(R.id.eraser);
eraser.setOnClickListener(this);
}
}
@SuppressLint("NewApi") @Override
public void onClick(View v) {
if (v == eraser) {
if (drawingView.isEraserActive()) {
drawingView.deactivateEraser();
eraser.setImageResource(R.drawable.eraser);
} else {
drawingView.activateEraser();
eraser.setImageResource(R.drawable.pencil);
}
} else if (v == btnClear) {
drawingView.reset();
drawingView.setBackground(null);
} else if (v == btnSave) {
saveImage();
} else if (v == btnCamera) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else if (v == btnShare) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(saveImage().getAbsolutePath())); //"file:///sdcard/temporary_file.jpg"
startActivity(Intent.createChooser(share, "Share Image"));
} else if (v == btnChooseImage) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
}
public File saveImage() {
drawingView.setDrawingCacheEnabled(true);
Bitmap bm = drawingView.getDrawingCache();
File fPath = Environment.getExternalStorageDirectory();
File f = null;
f = new File(fPath, UUID.randomUUID().toString() + ".png");
try {
FileOutputStream strm = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 80, strm);
strm.close();
Toast.makeText(getApplicationContext(), "Image is saved successfully.", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
return f;
}
@SuppressLint("NewApi") @Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
if(Build.VERSION.SDK_INT >= 16)
{
drawingView.setBackground(ob);
}else {
drawingView.setBackgroundDrawable(ob);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
case CAMERA_REQUEST:
if (resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
BitmapDrawable ob = new BitmapDrawable(getResources(), photo);
if(Build.VERSION.SDK_INT >= 16)
{
drawingView.setBackground(ob);
}else {
drawingView.setBackgroundDrawable(ob);
}
}
}
}
}
my drawing-view code:
 
public class DrawingView extends View implements OnTouchListener {
private Canvas m_Canvas;
private Path m_Path;
private Paint m_Paint;
private ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
private ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>();
private float mX, mY;
private static final float TOUCH_TOLERANCE = 0.1f;
private boolean isEraserActive = false;
private Xfermode Xfermode;
private Canvas canvas;
private ArrayList<Pair<Path, Paint>> myArrayListOfValues;
public DrawingView(Context context, AttributeSet attr) {
super(context, attr);
setFocusable(true);
setFocusableInTouchMode(true);
setBackgroundColor(Color.WHITE);
this.setOnTouchListener(this);
onCanvasInitialization();
}
public void onCanvasInitialization() {
m_Paint = new Paint();
m_Paint.setAntiAlias(true);
m_Paint.setDither(true);
m_Paint.setColor(Color.parseColor("#000000"));
m_Paint.setStyle(Paint.Style.STROKE);
m_Paint.setStrokeJoin(Paint.Join.ROUND);
m_Paint.setStrokeCap(Paint.Cap.ROUND);
m_Paint.setStrokeWidth(2);
m_Canvas = new Canvas();
m_Path = new Path();
Paint newPaint = new Paint(m_Paint);
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Bitmap mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(mBitmap);
}
public boolean onTouch(View arg0, 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;
}
@Override
protected void onDraw(Canvas canvas) {
for (Pair<Path, Paint> p : paths) {
canvas.drawPath(p.first, p.second);
}
}
private void touch_start(float x, float y) {
if (isEraserActive) {
m_Paint.setColor(Color.WHITE);
m_Paint.setStrokeWidth(10);
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
} else {
m_Paint.setColor(Color.BLACK);
m_Paint.setStrokeWidth(2);
//m_Paint.drawRect(0,0,100,100);
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
m_Paint.setXfermode(Xfermode);
m_Paint.setStyle(Paint.Style.STROKE);
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
m_Path.reset();
m_Path.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) {
m_Path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
m_Path.lineTo(mX, mY);
// commit the path to our offscreen
m_Canvas.drawPath(m_Path, m_Paint);
// kill this so we don't double draw
m_Path = new Path();
Paint newPaint = new Paint(m_Paint); // Clones the mPaint object
paths.add(new Pair<Path, Paint>(m_Path, newPaint));
}
public void activateEraser()
{
isEraserActive = true;
}
public void deactivateEraser()
{
isEraserActive = false;
}
public boolean isEraserActive()
{
return isEraserActive;
}
public void reset()
{
paths.clear();
invalidate();
}
}