Can't Draw On Canvas
I'm developing a paint activity please take a look on the code I'm working on below: public class MyTouchEventView extends LinearLayout { private Paint paint = new Paint(); pr
Solution 1:
I only made few changes to your code. Check the snap shot
activity_main.xml
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><RelativeLayoutandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_above="@+id/button1"android:id="@+id/rl"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:layout_alignParentTop="true" ></RelativeLayout><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_marginLeft="14dp"android:text="Clear" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentRight="true"android:layout_marginRight="32dp"android:text="Save" /></RelativeLayout>MainActivity
publicclassMainActivityextendsActivity {
DrawingView dv ;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dv = newDrawingView(this);
setContentView(R.layout.activity_main);
RelativeLayoutrl= (RelativeLayout) findViewById(R.id.rl);
rl.addView(dv);
Buttonb= (Button) findViewById(R.id.button1);
Buttonb1= (Button) findViewById(R.id.button2);
b.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
dv.clear(); // on button click clear the draw
}
});
// similarly you can save the draw. i have not added.// if you have trouble let me know
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
returntrue;
}
publicclassDrawingViewextendsView {
privatePaintpaint=newPaint();
privatePathpath=newPath();
privatePaintcirclePaint=newPaint();
privatePathcirclePath=newPath();
public Button btnReset;
public Button btnSave;
public LinearLayout.LayoutParams params;
publicDrawingView(Context context) {
super(context);
paint.setAntiAlias(true);
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(15f);
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
}
publicvoidclear()
{
path.reset();
// Calls the onDraw() method
invalidate();
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(path, paint);
canvas.drawPath(circlePath, circlePaint);
}
@OverridepublicbooleanonTouchEvent(MotionEvent event) {
// Gives you x and y coordinates on the Event.floatpointX= event.getX();
floatpointY= event.getY();
// Checks for the event that occursswitch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
returntrue;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
circlePath.reset();
// (circle's center x-coordinate, y-coordinate, radius of the// circle, direction to wind the shape)
circlePath.addCircle(pointX, pointY, 30, Path.Direction.CW);
//circlePath.addRect(pointX - 25, pointY - 25, pointX + 25, pointY + 25, Path.Direction.CW);/* RectF rect = new RectF(pointX - 25, pointY - 25, pointX + 25, pointY + 25);
circlePath.addRoundRect(rect, 0, 0, Path.Direction.CW);
*/break;
case MotionEvent.ACTION_UP:
circlePath.reset();
break;
default:
returnfalse;
}
// Schedules a repaint.// Force a view to draw.
invalidate();
returntrue;
}
}
}
Snap Shot

Explanation
I used a Relative layout and placed buttons accordingly.
I used another relative layout with id rl. Added custom view to the same.
I extended a view rather than Linear Layout.
I defined a method clear to clear the draw which is called onClick of clear.
I used invalidate to refresh the draw.
Edit 2:
Save:
Note: Only the draw is saved. You can save the entire screen if you want to.
Add permission in Manifest File
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>Use the below to save
Buttonb1= (Button) findViewById(R.id.button2);
b1.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Buildereditalert=newAlertDialog.Builder(MainActivity.this);
editalert.setTitle("Please Enter the name with which you want to Save");
finalEditTextinput=newEditText(MainActivity.this);
LinearLayout.LayoutParamslp=newLinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT);
input.setLayoutParams(lp);
editalert.setView(input);
editalert.setPositiveButton("OK", newDialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
rl.setDrawingCacheEnabled(true);
String name= input.getText().toString();
Bitmapbitmap=rl.getDrawingCache();
Stringroot= Environment.getExternalStorageDirectory().toString();
FilemyDir=newFile(root + "/MyDraw");
myDir.mkdirs();
Filefile=newFile (myDir, name+".png");
if (file.exists ()) file.delete ();
try
{
if(!file.exists())
{
file.createNewFile();
}
FileOutputStreamostream=newFileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 10, ostream);
// System.out.println("saving......................................................"+path);
ostream.close();
rl.invalidate();
}
catch (Exception e)
{
e.printStackTrace();
}finally
{
rl.setDrawingCacheEnabled(false);
}
}
});
editalert.show();
}
});
Post a Comment for "Can't Draw On Canvas"