可以在画布上画不出 [英] Can't draw on canvas

查看:202
本文介绍了可以在画布上画不出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个漆的活动,请拿上code我正在下面一起来看看:

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();
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;
}

但问题是,我不能在画布上绘制。我是什么在这里失踪?任何帮助和响应是真正的AP preciated。谢谢你。

But the problem is, I can't draw on canvas. What am I missing in here? Any help and response are truly appreciated. Thanks.

推荐答案

我只作了一些更改您的code。检查快照

I only made few changes to your code. Check the snap shot

activity_main.xml

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>

MainActivity

MainActivity

    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;
     }
 }
 }

快拍

说明

我用了一个相对布局,并放置相应的按钮。

I used a Relative layout and placed buttons accordingly.

我用id为RL另一个相对布局。添加自定义视图是一样的。

I used another relative layout with id rl. Added custom view to the same.

我伸出一个视图,而不是线性布局。

I extended a view rather than Linear Layout.

我定义了一个明确的方法来清除它被称为明确的onClick抽奖。

I defined a method clear to clear the draw which is called onClick of clear.

我用无效刷新平局。

编辑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();   
        }

    });

这篇关于可以在画布上画不出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆