-1

我正在开发绘画活动,请查看下面我正在处理的代码:

    public class MyTouchEventView extends LinearLayout {

private Paint paint = new Paint();
private Path path = new Path();
private Paint circlePaint = new Paint();
private Path circlePath = new Path();

public Button btnReset;
public Button btnSave;
public LinearLayout.LayoutParams params;

public MyTouchEventView(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);


    btnReset = new Button(context);
    btnReset.setText("Clear Screen");
    btnSave = new Button(context);
    btnSave.setText("Save Image");

    params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    btnReset.setLayoutParams(params);
    btnSave.setLayoutParams(params);
    addView(btnReset);
    addView(btnSave);



    btnSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // resets the screen
            path.reset();
            // Calls the onDraw() method
            postInvalidate();
        }
    });

    btnReset.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // resets the screen
            path.reset();
            // Calls the onDraw() method
            postInvalidate();
        }
    });

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawPath(path, paint);
    canvas.drawPath(circlePath, circlePaint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    // Gives you x and y coordinates on the Event.
    float pointX = event.getX();
    float pointY = event.getY();

    // Checks for the event that occurs
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        path.moveTo(pointX, pointY);

        return true;
    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:
        return false;
    }

    // Schedules a repaint.
    // Force a view to draw.
    postInvalidate();
    return true;
}

但问题是,我不能在画布上画画。我在这里缺少什么?任何帮助和回应都非常感谢。谢谢。

4

1 回答 1

3

我只对您的代码进行了一些更改。检查快照

activity_main.xml

 <RelativeLayout xmlns: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" >

   <RelativeLayout
       android: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>

<Button
    android: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" />

<Button
    android: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>

主要活动

    public class MainActivity extends Activity {

DrawingView dv ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dv = new DrawingView(this);
    setContentView(R.layout.activity_main);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
    rl.addView(dv);
    Button b = (Button) findViewById(R.id.button1);
    Button b1 = (Button) findViewById(R.id.button2);
    b.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

 public class DrawingView extends View {

     private Paint paint = new Paint();
     private Path path = new Path();
     private Paint circlePaint = new Paint();
     private Path circlePath = new Path();

     public Button btnReset;
     public Button btnSave;
     public LinearLayout.LayoutParams params;

     public DrawingView (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);

     }
   public void clear()
   {
       path.reset();
       // Calls the onDraw() method
       invalidate();
   }
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         canvas.drawPath(path, paint);
         canvas.drawPath(circlePath, circlePaint);
     }

     @Override
     public boolean onTouchEvent(MotionEvent event) {

         // Gives you x and y coordinates on the Event.
         float pointX = event.getX();
         float pointY = event.getY();

         // Checks for the event that occurs
         switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:
             path.moveTo(pointX, pointY);

             return true;
         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:
             return false;
         }

         // Schedules a repaint.
         // Force a view to draw.
         invalidate();
         return true;
     }
 }
 }

快照

在此处输入图像描述

解释

我使用了相对布局并相应地放置了按钮。

我使用了另一个 ID 为 rl 的相对布局。添加了自定义视图。

我扩展了一个视图而不是线性布局。

我定义了一个方法 clear 来清除被称为清除的 onClick 的绘制。

我用无效来刷新平局。

编辑2:

节省:

注意:只有抽奖被保存。如果需要,您可以保存整个屏幕。

在清单文件中添加权限

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

使用下面的保存

    Button b1 = (Button) findViewById(R.id.button2);
    b1.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog.Builder editalert = new AlertDialog.Builder(MainActivity.this);
            editalert.setTitle("Please Enter the name with which you want to Save");
            final EditText input = new EditText(MainActivity.this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT);
            input.setLayoutParams(lp);
            editalert.setView(input);
            editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    rl.setDrawingCacheEnabled(true);
                    String name= input.getText().toString();
                    Bitmap bitmap =rl.getDrawingCache();
                    String root = Environment.getExternalStorageDirectory().toString();
                    File myDir = new File(root + "/MyDraw");    
                    myDir.mkdirs();
                    File file = new File (myDir, name+".png");
                    if (file.exists ()) file.delete ();         
                    try 
                    {
                        if(!file.exists())
                    {
                        file.createNewFile();
                    }
                        FileOutputStream ostream = new FileOutputStream(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();   
        }

    });
于 2013-05-03T15:14:38.983 回答