0

我想创建一个窗口,用户可以在其中走出图像的边界,但路径仍在继续。

我的活动有此代码

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()”方法中(在全局布局侦听器内)执行相同的操作,但它没有运行。

有人可以帮助我吗?

首先,失去感谢!

4

1 回答 1

0

I found the way to solve my problem. Where I need to put the view.layout() is inside the onDraw() method of the view that I want to be layout resized.

于 2012-05-15T09:25:02.500 回答