2013-05-03 111 views
-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; 
} 

但問題是,我不能在畫布上繪製。我在這裏錯過了什麼?任何幫助和迴應都是真正的讚賞。謝謝。

+1

你可能要開始用手指油漆例子,然後修改它爲你的程序:HTTPS:/ /github.com/pocorall/scaloid-apidemos/blob/master/src/main/java/com/example/android/apis/graphics/FingerPaint.java – 2013-05-03 14:09:18

+0

它可能只是一個錯字,但是類開放線('public我的......)缺少'擴展視圖'。 – 2013-05-03 14:16:59

+0

你是否在xml中使用了這個(通過不同的構造函數)? – f20k 2013-05-03 14:22:16

回答

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> 

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

捕捉的快照

enter image description here

說明

我使用了相對佈局並相應地放置了按鈕。

我用id rl使用了另一個相對佈局。添加自定義視圖相同。

我擴展了視圖而不是線性佈局。

我定義了一個清除方法,清除名爲onClick的draw。

我用invalidate刷新繪製。

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

    }); 
+0

謝謝你,夥計,我會試試這個,並保持你已經更新了結果 – 2013-05-03 15:22:57

+0

你好,你還可以爲我提供保存圖紙的代碼嗎?謝謝 – 2013-05-03 15:29:26

+0

好的,我會爲你做這個工作嗎? – Raghunandan 2013-05-03 15:30:07