2012-05-13 70 views
0

我想創建一個窗口,用戶可以從圖像的邊界出去但路徑仍在繼續。Android:在由ImageView中的位圖確定的框架內繪製

我有這樣的代碼爲我的活動

public class PaintActivity extends Activity { 
PaintView maskView; 
ImageView imagen; 

Bitmap bitmap; 
Bitmap mask; 
int bitmapWidth, bitmapHeight; 

public int[] getBitmapMinCoords() { 
    float[] minCoordF = {0, 0}; 

    Matrix matriz = imagen.getImageMatrix(); 
    matriz.mapPoints(minCoordF); 

    int[] minCoords = new int[2]; 
    minCoords[0] = Math.round(minCoordF[0]); 
    minCoords[1] = Math.round(minCoordF[1]); 

    return minCoords; 
} 

public int[] getBitmapMaxCoords() { 
    float[] maxCoordF = {bitmapWidth, bitmapHeight}; 

    Matrix matriz = imagen.getImageMatrix(); 
    matriz.mapPoints(maxCoordF); 

    int[] maxCoords = new int[2]; 
    maxCoords[0] = Math.round(maxCoordF[0]); 
    maxCoords[1] = Math.round(maxCoordF[1]); 

    return maxCoords; 
} 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.paint); 

    imagen = (ImageView) findViewById(R.id.image_paint); 
    maskView = (PaintView) findViewById(R.id.mask_paint); 

    String imagePath = "/sdcard/images/NewOrleans.gif"; 

    bitmap = BitmapFactory.decodeFile(imagePath); 
    imagen.setImageBitmap(bitmap); 

    bitmapWidth = bitmap.getWidth(); 
    bitmapHeight = bitmap.getHeight(); 

    ViewTreeObserver vto = imagen.getViewTreeObserver();  
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {   
     public void onGlobalLayout() {    
      Matrix matrizTransformacion = imagen.getImageMatrix(); 
      float[] values = new float[9]; 
      if (matrizTransformacion != null) { 
       matrizTransformacion.getValues(values); 
       Log.i(PaintActivity.class.toString() + ".globalListener()", "La matriz tiene valores: " + matrizTransformacion.toString()); 
      } 

      int[] minC = getBitmapMinCoords(); 
      int[] maxC = getBitmapMaxCoords(); 

      Log.i("Listener MinCOORDS (0,0)", minC[0] + ", " + minC[1]); 
      Log.i("Listener MaxCOORDS (" + bitmapWidth + "," + bitmapHeight + ")", maxC[0] + ", " + maxC[1]); 

      Bitmap mapaBits = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888); 
      Canvas canvas = new Canvas(mapaBits); 

      maskView.layout(minC[0], minC[1], maxC[0], maxC[1]); 
      maskView.draw(canvas); 
      maskView.refreshDrawableState(); 

      imagen.getViewTreeObserver().removeGlobalOnLayoutListener(this);   
     }  
    }); 

} 

Bitmap getMaskBitmap(View view) { 

    Matrix matrizTransformacion = imagen.getImageMatrix(); 
    float[] values = new float[9]; 
    if (matrizTransformacion != null) { 
     matrizTransformacion.getValues(values); 
     Log.i(PaintActivity.class.toString() + ".onStart()", "La matriz tiene valores: " + matrizTransformacion.toString()); 
    } 

    Bitmap mapaBits = Bitmap.createBitmap(Math.round(bitmapWidth*values[0]), Math.round(bitmapHeight*values[4]), Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(mapaBits); 

    int[] minCoords = getBitmapMinCoords(); 
    int[] maxCoords = getBitmapMaxCoords(); 

    view.layout(minCoords[0], minCoords[1], maxCoords[0], maxCoords[1]); 
    view.draw(canvas); 

    return mapaBits; 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater menuInflater = getMenuInflater(); 
    menuInflater.inflate(R.menu.inpainting_color_selection_menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 

    switch (item.getItemId()) { 
    case R.id.inpainting_color_accept_opt: 
     ProgressDialog pd = ProgressDialog.show(this, "", "Guardando imagen"); 

     Bitmap mascara = getMaskBitmap(maskView); 


     // GUARDANDO BITMAP EN HDD 
     try { 
      String filename = "/sdcard/PhotoRestore/mask.png"; 
      File file = new File(filename); 
      FileOutputStream out = new FileOutputStream(file); 
      mascara.compress(Bitmap.CompressFormat.PNG, 100, out); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 


     pd.dismiss(); 

     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
} 
} 

這是我的PaintView

public class PaintView extends View { 

    float previousX = -1; 
    float previousY = -1; 

    float currentX = -1; 
    float currentY = -1; 

    PaintActivity activity; 

    Path path; 
    Paint paintLine = new Paint(); 

    public PaintView(Context context) { 
     super(context); 
     init(); 
    } 

    public PaintView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public PaintView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     path = new Path(); 

     activity = (PaintActivity) getContext(); 

     paintLine = new Paint(Paint.ANTI_ALIAS_FLAG); 
     paintLine.setStyle(Paint.Style.STROKE); 
     paintLine.setStrokeWidth(10); 
     paintLine.setColor(Color.GREEN); 
     paintLine.setAlpha(150); 
    } 

    public void onDraw(Canvas canvas) { 
     canvas.drawPath(path, paintLine); 
    } 

    @Override 
    public boolean onTouchEvent(final MotionEvent event) { 

     currentX = event.getX(); 
     currentY = event.getY(); 

      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       path.moveTo(currentX, currentY); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       path.quadTo(previousX, previousY, currentX, currentY); 
       break; 
      case MotionEvent.ACTION_UP: 
       path.quadTo(previousX, previousY, currentX, currentY); 
       break; 
      } 

      previousX = currentX; 
      previousY = currentY; 

      postInvalidate(); 

      return true; 

    } 
} 

的代碼,這是我的XML視圖

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/image_paint" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:contentDescription="@string/hola" /> 

    <com.paint.PaintView 
     android:id="@+id/mask_paint" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

當我按下按鈕我在菜單中添加了保存圖像的內存,屏幕繼續存在,但現在我已經有了邊框。遠離邊界的路徑會繼續繪製,但它會在黑色表面下被遮擋,路徑不會被切斷,這就是我想要在活動開始時出現的情況。

我試圖在活動的'onCreate()'方法中做同樣的事情(在全局佈局偵聽器中),但它不運行。

有人可以幫助我嗎?

首先,謝謝了!

回答

0

我找到了解決我的問題的方法。 我需要把view.layout()放在onDraw()方法的視圖中,我想要進行佈局大小調整。