2013-03-30 60 views

回答

1

首先研究圖形文件夾下的樣本中的fingerpaint.java。

您應該重寫視圖的onDraw方法。

screen_drawing_room.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rlid" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<LinearLayout //set background for the bottom layout set image here. 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:weightSum="1.0" > 

    <LinearLayout 
     android:id="@+id/view_drawing_pad"//your drawing pad on foreground 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </LinearLayout> 
</LinearLayout> 
</RelativeLayout 

在你活動的onCreate()

Drawing View mDrawingView=new DrawingView(this); 

    setContentView(R.layout.screen_drawing_room);  

    LinearLayout mDrawingPad=(LinearLayout)findViewById(R.id.view_drawing_pad); 

    mDrawingPad.addView(mDrawingView); 

DrawingView.java

定義你的DrawingView。以下可用於徒手繪製。修改相同的內容以繪製線條,文本和填充顏色(封閉區域)。

您應該添加一個顏色選擇器並將顏色設置爲顏色對象以允許用戶選擇顏色。

對於洪水填充,請參閱鏈接android using flood fill algorithm getting out of memory exception中接受的答案。

class DrawingView extends View 
{ 
     Paint  mPaint; 
     //MaskFilter mEmboss; 
     //MaskFilter mBlur; 
     Bitmap mBitmap; 
     Canvas mCanvas; 
     Path mPath; 
     Paint mBitmapPaint; 

    public DrawingView(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
      mPaint = new Paint(); 
      mPaint.setAntiAlias(true); 
      mPaint.setDither(true); 
      mPaint.setColor(0xFFFF0000); 
      mPaint.setStyle(Paint.Style.STROKE); 
      mPaint.setStrokeJoin(Paint.Join.ROUND); 
      mPaint.setStrokeCap(Paint.Cap.ROUND); 
      mPaint.setStrokeWidth(20); 

     mPath = new Path(); 
     mBitmapPaint = new Paint(); 
     mBitmapPaint.setColor(Color.RED); 
    } 
    @Override 
     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      super.onSizeChanged(w, h, oldw, oldh); 
      mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mBitmap); 
     } 
    @Override 
    public void draw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     super.draw(canvas); 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
     canvas.drawPath(mPath, mPaint); 
    } 
    private float mX, mY; 
    private static final float TOUCH_TOLERANCE = 4; 

    private void touch_start(float x, float y) { 
     //mPath.reset(); 
     mPath.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) { 
      mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2); 
      mX = x; 
      mY = y; 
     } 
    } 
    private void touch_up() { 
     mPath.lineTo(mX, mY); 
     // commit the path to our offscreen 
     mCanvas.drawPath(mPath, mPaint); 
     //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN)); 
     // kill this so we don't double draw 
     mPath.reset(); 
     // mPath= new Path(); 
    } 

    @Override 
    public boolean onTouchEvent(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; 
    } 


} 

} 

您的圖紙視圖已設置。從圖形文件夾中的sdk上的示例研究fingerpaint.java。

結果sanpshot。你在後臺看到的是一張圖片。在前景我畫嗨。我已經在角落畫線了。如果你能確定它是紅色的。

畫出看起來像邊框的線條。設置筆畫寬度爲任何你喜歡的。同樣,您可以通過更改x1,y1和x2,y2座標來繪製所需的線條。

   Display display = ((Activity) mcontext).getWindowManager().getDefaultDisplay(); 
       float w = display.getWidth(); 
       float h = display.getHeight(); 
       canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 
       canvas.drawLine(0, 0, w, 0,mBitmapPaint); 
       canvas.drawLine(0, 0, 0, h,mBitmapPaint); 
       canvas.drawLine(w,h,w,0,mBitmapPaint); 
       canvas.drawLine(w, h, 0,h , mBitmapPaint); 

enter image description here

爲了節省

AlertDialog.Builder editalert = new AlertDialog.Builder(DrawingRoomScreen.this); 
editalert.setTitle("Please Enter the name with which you want to Save"); 
final EditText input = new EditText(DrawingRoomScreen.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) { 
         content.setDrawingCacheEnabled(true); 
         String name= input.getText().toString(); 
         Bitmap bitmap = mDrawingPad.getDrawingCache();//root view 

        String path = Environment.getExternalStorageDirectory().getAbsolutePath(); 
         File file = new File("/sdcard/"+name+".png");   
         try 
         { 
          if(!file.exists()) 
         { 
          file.createNewFile(); 
         } 
          FileOutputStream ostream = new FileOutputStream(file); 
          bitmap.compress(CompressFormat.PNG, 10, ostream); 
          System.out.println("saving......................................................"+path); 
          ostream.close(); 
          //content.invalidate();       
         } 
         catch (Exception e) 
         { 
          e.printStackTrace(); 
         }finally 
         { 


         } 
        } 
       }); 

       editalert.show();  

從畫廊得到圖像

File fp; 
    Drawable d; 

public void setImagefrmGallery() 
{ 
// To open up a gallery browser 
Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1); 
// To handle when an image is selected from the browser, add the following to your Activity 
} 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
if (resultCode == RESULT_OK) { 
if (requestCode == 1) { 
// currImageURI is the global variable I�m using to hold the content:// URI of the image 
Uri currImageURI = data.getData(); 
System.out.println("Hello======="+getRealPathFromURI(currImageURI)); 
String s= getRealPathFromURI(currImageURI); 
File file = new File(s); 

    if (file.exists()) { 
    fp=file.getAbsolutePath(); 
    d = Drawable.createFromPath(file.getAbsolutePath()); 
    mDrawingPad.setBackgroundDrawable(d); 
    } 
    else 
    { 
    System.out.println("File Not Found"); 
    } 
} 
} 
} 
// And to convert the image URI to the direct file system path of the image file 
public String getRealPathFromURI(Uri contentUri) { 
// can post image 
String [] proj={MediaStore.Images.Media.DATA}; 
Cursor cursor = managedQuery(contentUri, 
proj, // Which columns to return 
null, // WHERE clause; which rows to return (all rows) 
null, // WHERE clause selection arguments (none) 
null); // Order-by clause (ascending by name) 
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
cursor.moveToFirst(); 
return cursor.getString(column_index); 
    }