-1

私はビットマップでペイントするためのアプリケーションを作成していますが、ここここのように SO で以前に何度か尋ねられました

アプリ内でそれらを使用しましたが、どれも機能していないようでした。使用したコードは次のとおりです。

public class EditActivity extends Activity implements OnTouchListener,
        OnClickListener {
    GestureDetector gd;
    View.OnTouchListener gl;
    RelativeLayout parent;
    ImageView im;
    Bitmap bmp;
    Bitmap alteredBitmap;
    Canvas canvas;
    Paint paint;
    Matrix matrix;
    Rect imageBounds;
    Path mPath;
    int dw;
    int dh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        parent = (RelativeLayout) findViewById(R.id.rl);


        matrix = new Matrix();
        parent.setBackgroundColor(Color.BLACK);
        gd = new GestureDetector(this, new MyGestureDetector());
        gl = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                float x = event.getX();
                float y = event.getY();

                int action = event.getAction();

                    switch (action) {

                    case MotionEvent.ACTION_DOWN:

                        mPath.moveTo(x,y);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mPath.lineTo(x,y);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        // canvas.drawPath(mPath, paint);
                        mPath.reset();
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        break;
                    default:
                        break;
                    }
                    return gd.onTouchEvent(event);
                } else {
                    return gd.onTouchEvent(event);
                }
            }
        };

        im = (ImageView) findViewById(R.id.ImageView);

        Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri"));
        Display currentDisplay = getWindowManager().getDefaultDisplay();

        dw = currentDisplay.getWidth();

        dh = currentDisplay.getHeight();

        try {
            // Load up the image's dimensions not the image itself
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(imageFileUri), null, bmpFactoryOptions);

            alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
                    bmp.getHeight(), bmp.getConfig());

            mPath = new Path();
            canvas = new Canvas(alteredBitmap);
            paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG);
            paint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            paint.setFilterBitmap(true);
            paint.setDither(true);
            paint.setAntiAlias(true);
            paint.setTextSize(40f);

            paint.setTypeface(getTypeFaceFromFile("Roboto-Light"));

            canvas.drawBitmap(bmp, matrix, paint);
            canvas.save();
            im.setImageBitmap(alteredBitmap);
            im.setOnClickListener(this);
            im.setOnTouchListener(gl);

        } catch (FileNotFoundException e) {
            Log.v("ERROR", e.toString());
        }

    }

    class MyGestureDetector extends SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent event) {

            return super.onDown(event);
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {

            return super.onSingleTapConfirmed(e);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {

            return super.onDoubleTap(e);
        }

        @Override
        public void onLongPress(MotionEvent e) {

            super.onLongPress(e);
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {

            return super.onDoubleTapEvent(e);
        }

    }

}

今、画面に触れてパスを描くと、それ自体が他の場所に描画されます。また、スケーリングなしで収まるほど小さい画像ではなく、アンドロイド自体によってスケーリングされた画像でのみ発生します。調べてくださいそして私を助けて

編集: 私はこの回答に従いました。これは実装後の私のコードですが、現在はパスがありません....

        gl = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {

                float x = event.getX();
                float y = event.getY();


                float scaledImageOffsetX = x - imageBounds.left;
                float scaledImageOffsetY = y - imageBounds.top;

                float originalImageOffsetX = scaledImageOffsetX * widthRatio;
                float originalImageOffsetY = scaledImageOffsetY * heightRatio;
                int action = event.getAction();

                    switch (action) {

                    case MotionEvent.ACTION_DOWN:

                        mPath.moveTo(originalImageOffsetX,originalImageOffsetY);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        mPath.lineTo(originalImageOffsetX,originalImageOffsetY);
                        im.invalidate();
                        break;
                    case MotionEvent.ACTION_UP:
                        // canvas.drawPath(mPath, paint);
                        mPath.reset();
                        im.invalidate();
                        break;

                    default:
                        break;
                    }
                    return true;
                } 

            }
        };

        im = (ImageView) findViewById(R.id.ImageView);
        Uri imageFileUri = Uri.parse(getIntent().getStringExtra("uri"));
        Display currentDisplay = getWindowManager().getDefaultDisplay();
        dw = currentDisplay.getWidth();
        dh = currentDisplay.getHeight();

        try {

            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();

            bmpFactoryOptions.inJustDecodeBounds = false;
            bmp = BitmapFactory.decodeStream(getContentResolver()
                    .openInputStream(imageFileUri), null, bmpFactoryOptions);

            alteredBitmap = Bitmap.createBitmap(bmp.getWidth(),
                    bmp.getHeight(), bmp.getConfig());

            mPath = new Path();
            canvas = new Canvas(alteredBitmap);
            paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG);
            paint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.WHITE);
            paint.setFilterBitmap(true);
            paint.setDither(true);
            paint.setAntiAlias(true);
            paint.setTextSize(40f);

            paint.setTypeface(getTypeFaceFromFile("Roboto-Light"));

            canvas.drawBitmap(bmp, matrix, paint);
            canvas.save();
            im.setImageBitmap(alteredBitmap);
            Drawable drawable = im.getDrawable();
            imageBounds = drawable.getBounds();

            float intrinsicHeight = drawable.getIntrinsicHeight();
            float intrinsicWidth = drawable.getIntrinsicWidth();;

            float scaledHeight = imageBounds.height();
            float scaledWidth = imageBounds.width();

            heightRatio = intrinsicHeight / scaledHeight;
            widthRatio = intrinsicWidth / scaledWidth;
            im.setOnClickListener(this);
            im.setOnTouchListener(gl);

        } catch (FileNotFoundException e) {
            Log.v("ERROR", e.toString());
        }

    }

}

ありがとう、ジート

4

1 に答える 1