3

我想在图像视图上绘制画布我正在使用手指画的概念,但它不起作用

public class show_image extends Activity implements OnTouchListener,ColorPickerDialog.OnColorChangedListener {
    ImageView img_vw_show;
    Matrix matrix = new Matrix();
    Matrix savedMatrix = new Matrix();
    static final int NONE = 0;
    static final int DRAG = 1;
    static final int ZOOM = 2;
    int mode = NONE;

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    float oldDist = 1f;
    float x1, y1, x2, y2;
    Bitmap bmp;
    int i = 1, width, height;
    private static final float MINP = 0.25f;
    // private static final float MAXP = 0.75f;
    int w, h;
    Bitmap mBitmap, bMap, newBitmap;
    Canvas mCanvas;
    Path mPath;
    Paint mBitmapPaint, mPaint;
    MaskFilter mEmboss;
    MaskFilter mBlur;
    BufferedInputStream buf;
    MyView mv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_image);
        System.out.println("oncreate method called");
        img_vw_show = (ImageView) this.findViewById(R.id.img_vw_show);

        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);

        mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

        // mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);
        img_vw_show.setOnTouchListener((OnTouchListener) this);
        String responseImagePath = this.getIntent().getStringExtra(
                "responseImagePath");
        String selectedImagePath = this.getIntent().getStringExtra(
                "selectedImagePath");
        System.out.println("responseImagePath in show image========"
                + responseImagePath);
        System.out.println("selectedImagePath in show image========"
                + selectedImagePath);
        Boolean selectedImagePathFlag = false;

        if (selectedImagePath == null) {
            selectedImagePathFlag = true;

        } else if (selectedImagePath.equals("")) {
            selectedImagePathFlag = true;
        }

        if (selectedImagePathFlag) {
            if (responseImagePath != null && !responseImagePath.equals("")) {
                URL url1;
                InputStream in;

                try {
                    url1 = new
                    // URL("http://mail.sshanghvi.com:8080/sacplupload/checklist/"+responseImagePath);
                    URL("http://192.168.1.49:82/uploads/" + responseImagePath);
                    in = url1.openStream();
                    // Read the inputstream
                    buf = new BufferedInputStream(in);

                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inJustDecodeBounds = true;
                    // The new size we want to scale to
                    final int REQUIRED_SIZE = 70;

                    // Find the correct scale value. It should be the power of
                    // 2.
                    int width_tmp = o.outWidth, height_tmp = o.outHeight;
                    int scale = 1;
                    while (true) {
                        if (width_tmp / 2 < REQUIRED_SIZE
                                || height_tmp / 2 < REQUIRED_SIZE)
                            break;
                        width_tmp /= 2;
                        height_tmp /= 2;
                        scale *= 2;
                    }

                    // Decode with inSampleSize
                    BitmapFactory.Options o2 = new BitmapFactory.Options();
                    o2.inSampleSize = scale;
                    bMap = BitmapFactory.decodeStream(buf, null, o2);
                    img_vw_show.setImageBitmap(bMap);
                    width = bMap.getWidth();
                    height = bMap.getHeight();
                    System.out.println("Width and Height is==" + width + " & "
                            + height);
                    mv = new MyView(show_image.this, bMap);
                    System.out.println("Setting the Myview");
                    mv.invalidate();
                    if (in != null) {
                        in.close();
                    }
                    if (buf != null) {
                        buf.close();
                    }

                } catch (Exception e) {
                    Log.e("Error reading file", e.toString());
                }
            } else {
                Toast.makeText(getApplicationContext(), "Image Not Available",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            File imgFile = new File(selectedImagePath);
            BitmapFactory.Options bfOptions = new BitmapFactory.Options();

            bfOptions.inDither = false; // Disable Dithering mode
            bfOptions.inPurgeable = true; // Tell to gc that whether it needs
                                            // free memory, the Bitmap can be
                                            // cleared
            bfOptions.inInputShareable = true; // Which kind of reference will
                                                // be used to recover the Bitmap
                                                // data after being clear, when
                                                // it will be used in the future
            bfOptions.inTempStorage = new byte[32 * 1024];
            Bitmap myBitmap = BitmapFactory.decodeFile(
                    imgFile.getAbsolutePath(), bfOptions);
            img_vw_show.setImageBitmap(myBitmap);

        }
    }

    public class MyView extends View {

        public MyView(Context c, Bitmap img) {
            super(c);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(8);
            setWillNotDraw(false);
            mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6,
                    3.5f);
        }
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }

        Bitmap newBitmap;
        Canvas newCanvas;
        public void onDraw(Canvas canvas) {
            super.onDraw(newCanvas);
            newBitmap= Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            newCanvas= new Canvas(newBitmap);
            System.out.println("canvas == "+canvas);
            System.out.println("newCanvas == "+newCanvas);
            System.out.println("onDraw Called");

            img_vw_show.draw(newCanvas);

            newCanvas.drawBitmap(newBitmap, 0, 0, mBitmapPaint);

            newCanvas.drawPath(mPath, mPaint);

            img_vw_show.setImageBitmap(newBitmap);
        }

        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            System.out.println("Touch Start Called");
            onDraw(newCanvas);
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }

        private void touch_move(float x, float y) {
            System.out.println("Touch Move Called");
            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() {

            System.out.println("Touch Up Called");
            mPath.lineTo(mX, mY);
            // commit the path to our offscreen
            newCanvas.drawPath(mPath, mPaint);
            // kill this so we don't double draw
            mPath.reset();
        }
    }
    private static final int COLOR_MENU_ID = Menu.FIRST;
    private static final int EMBOSS_MENU_ID = Menu.FIRST + 1;
    private static final int BLUR_MENU_ID = Menu.FIRST + 2;
    private static final int ERASE_MENU_ID = Menu.FIRST + 3;
    private static final int SRCATOP_MENU_ID = Menu.FIRST + 4;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
        menu.add(0, EMBOSS_MENU_ID, 0, "Emboss").setShortcut('4', 's');
        menu.add(0, BLUR_MENU_ID, 0, "Blur").setShortcut('5', 'z');
        menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
        menu.add(0, SRCATOP_MENU_ID, 0, "SrcATop").setShortcut('5', 'z');

        /****
         * Is this the mechanism to extend with filter effects? Intent intent =
         * new Intent(null, getIntent().getData());
         * intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
         * menu.addIntentOptions( Menu.ALTERNATIVE, 0, new ComponentName(this,
         * NotesList.class), null, intent, 0, null);
         *****/
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        mPaint.setXfermode(null);
        mPaint.setAlpha(0xFF);

        switch (item.getItemId()) {
        case COLOR_MENU_ID:
            new ColorPickerDialog(this, this, mPaint.getColor()).show();
            return true;
        case EMBOSS_MENU_ID:
            if (mPaint.getMaskFilter() != mEmboss) {
                mPaint.setMaskFilter(mEmboss);
            } else {
                mPaint.setMaskFilter(null);
            }
            return true;
        case BLUR_MENU_ID:
            if (mPaint.getMaskFilter() != mBlur) {
                mPaint.setMaskFilter(mBlur);
            } else {
                mPaint.setMaskFilter(null);
            }
            return true;
        case ERASE_MENU_ID:
            // mPaint.setXfermode(new
            // PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            return true;
        case SRCATOP_MENU_ID:
            // mPaint.setXfermode(new
            // PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
            mPaint.setAlpha(0x80);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void colorChanged(int color) {
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mv.touch_start(x, y);
            v.invalidate();
            break;
        case MotionEvent.ACTION_MOVE:
            mv.touch_move(x, y);
            v.invalidate();
            break;
        case MotionEvent.ACTION_UP:
            mv.touch_up();
            v.invalidate();
            break;
        }
        return true;
    }

}
4

2 回答 2

1

即使我在我的应用程序中也遇到了同样的问题。将画布上的形状绘制到 imageViews 上,然后每次更新它们似乎都不起作用。

我通过将形状提取到我的 UI 类中来完成相同的操作,并在那里编写了一个方法 showView(View v, color color) {

}

并在需要时在我的主要活动中调用它。在我的应用程序中,场景是动态更改画布的颜色。

所以我就这样处理它并且它奏效了。希望能帮助到你。

于 2012-11-26T10:37:58.367 回答
0
     Canvas Class:
       public void init() 
 {
    basePaint = new Paint();
    basePaint.setColor(Color.RED);
    basePaint.setStrokeWidth(2);
    basePaint.setStyle(Paint.Style.FILL_AND_STROKE);

    paint = new Paint();
    paint.setStrokeWidth(2);
    paint.setColor(color);


    triPaint = new Paint();
    triPaint.setColor(Color.RED);
    triPaint.setStrokeWidth(2);
    triPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    triPaint1 = new Paint();
    triPaint1.setColor(Color.RED);
    triPaint1.setStrokeWidth(2);
    triPaint1.setStyle(Paint.Style.FILL_AND_STROKE);

    diffPaint = new Paint();
    diffPaint.setColor(color);
    diffPaint.setStrokeWidth(2);
    diffPaint.setStyle(Paint.Style.FILL_AND_STROKE);

    Log.i("color","="+color);
    paint.setStyle(Paint.Style.FILL_AND_STROKE);

}

  @Override
   public void onDraw(Canvas canvas) {

    Log.d("check","="+color);
    Log.e("check","after set:"+buf);
    Log.i("new view ", " on draw ");
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(5);
    paint.setColor(Color.WHITE);

    /*basePaint.setStyle(Paint.Style.FILL);
    basePaint.setStrokeWidth(3);
    basePaint.setColor(Color.RED);

    canvas.drawLine(0,40,80,40,basePaint);*/
    path = new Path();
    path.moveTo(0, 30);                             /*For Borders*/
    path.lineTo(30, 0);
    path.lineTo(-30, 0);
    path.close();
    path.offset(20, -5);
    canvas.drawPath(path, paint);

    diffPaint.setStyle(Paint.Style.FILL);
    diffPaint.setStrokeWidth(3);
    diffPaint.setColor(color);                    //To fill The Triangle
    Log.d("Filling","Triangles");

    diffPath = new Path();
    diffPath.moveTo(0, 30);
    Log.d("After","1st line");

    diffPath.lineTo(30, 0);
    Log.d("After","2nd line");

    diffPath.lineTo(-30, 0);
    Log.d("After","3rd line");

    diffPath.close();
    diffPath.offset(20, -5);
    Log.d("Locating","Arrow");

    canvas.drawPath(diffPath, diffPaint);
    Log.d("Drew","Arrow");
    Log.d("Color","="+color);

         triPaint.setStyle(Paint.Style.FILL);
    triPaint.setStrokeWidth(100);
    triPaint.setColor(Color.BLACK);

    triPath = new Path();
    triPath.moveTo(0, -20);
    triPath.lineTo(20, 0);
    triPath.lineTo(-20, 0);                     //To Fill the extra Space
    triPath.close();
    triPath.offset(42,26);
    canvas.drawPath(triPath, triPaint);

    triPaint1.setStyle(Paint.Style.FILL);
    triPaint1.setStrokeWidth(100);
    triPaint1.setColor(Color.BLACK);


    triPath1 = new Path();
    triPath1.moveTo(0, -20);
    triPath1.lineTo(20, 0);
    triPath1.lineTo(-20, 0);
    triPath1.close();
    triPath1.offset(-2,26);
    canvas.drawPath(triPath1, triPaint1);       
}


   UI class:

   public void show(View anchor,int color) {
    preShow();

    int xPos, yPos, arrowPos;

    mDidAction = false; /*Setting the position of arrows drawn using canvas in UI*/

    int[] location = new int[2];

    anchor.getLocationOnScreen(location);

    Rect anchorRect = new Rect(location[0], location[1], location[0]
            + anchor.getWidth(), location[1] + anchor.getHeight());

    mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    int rootHeight = mRootView.getMeasuredHeight();

    if (rootWidth == 0) {
        rootWidth = mRootView.getMeasuredWidth();
    }

    int screenWidth = mWindowManager.getDefaultDisplay().getWidth();
    int screenHeight = mWindowManager.getDefaultDisplay().getHeight();

    if ((anchorRect.left + rootWidth) > screenWidth) {
        xPos = anchorRect.left - (rootWidth - anchor.getWidth());
        xPos = (xPos < 0) ? 0 : xPos;

        arrowPos = anchorRect.centerX() - xPos;

    } else {
        if (anchor.getWidth() > rootWidth) {
            xPos = anchorRect.centerX() - (rootWidth / 2);
        } else {
            xPos = anchorRect.left;
        }

        arrowPos = anchorRect.centerX() - xPos;
    }

    int dyTop = anchorRect.top;
    int dyBottom = screenHeight - anchorRect.bottom;

    boolean onTop = (dyTop > dyBottom) ? true : false;

    if (onTop) {
        if (rootHeight > dyTop) {
            yPos = 15;
            LayoutParams l = mScroller.getLayoutParams();
            l.height = dyTop - anchor.getHeight();
        } else {
            yPos = anchorRect.top - rootHeight;
        }
    } else {
        yPos = anchorRect.bottom;

        if (rootHeight > dyBottom) {
            LayoutParams l = mScroller.getLayoutParams();
            l.height = dyBottom;
        }
    }

     showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), arrowPos,color);
             /*updating color of arrow which is the shape drawn in canvas*/

                 container.setBackgroundColor(color);

    setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);
    mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);


} 
  Main Activity:

   Whenever the shape needs to be updated by a modification of color:


    btn1 = (Button) this.findViewById(R.id.btn1);
    btn1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {


            Log.d("In","View");    
            quickAction.show(v,Color.BLUE);

     /*By passing the color the color is getting updated and modified on each button click for the shape drawn using canvas on the imageView*/
        }
    });
于 2012-11-26T13:24:12.427 回答