在imageView上画一条线

时间:2011-11-08 05:03:53

标签: android image drawing

我是Android的新手,目前正在对Android进行一些研究。我在图像顶部画一条线或另一个物体时遇到了问题。

我的情况是,我使用imageView方法在setImageBitmap上绘制图像。现在我要在它上面添加一行或另一个对象。我发现有几篇文章描述我可以覆盖onDraw方法,但是当我这样做并使用canvas.drawBitmap方法绘制我的图像时,我的imageView上会有两个图像。他们互相吸引。

如何只绘制一张图片?

任何线索,或者是描述这个的链接?

这里是代码:

public void draw(){

    // Declaration output pixels vector
    int[] outputPixels = new int[mImage.getDataLength()];

    // Get the gray scale window width
    int windowWidth = mDICOMViewerData.getWindowWidth();

    // Compute the window offset x the number of gray levels (256)
    int windowOffset = ((2 * mDICOMViewerData.getWindowCenter() - windowWidth)) / 2;

    switch(mDICOMViewerData.getCLUTMode()) {

    case CLUTMode.NORMAL:
        computeGrayscaleRGBImage(windowWidth, windowOffset, outputPixels);
        break;

    case CLUTMode.INVERSE:
        computeInverseGrayscaleRGBImage(windowWidth, windowOffset, outputPixels);
        break;

    case CLUTMode.RAINBOW:
        computeRainbowRGBImage(windowWidth, windowOffset, outputPixels);
        break;

    };

    // Create the bitmap
    Bitmap imageBitmap = Bitmap.createBitmap(outputPixels, mImage.getWidth(),
            mImage.getHeight(), Bitmap.Config.ARGB_8888);

    // Check if image is to be rotated 90 degrees
    if (mIsRotate) {
        Matrix m = new Matrix();
        m.postRotate(90);
        imageBitmap = Bitmap.createBitmap(imageBitmap,
                0,  0, mImage.getWidth(), mImage.getHeight(), 
                m, true);
    }


    // Set the image
    setImageBitmap(imageBitmap);

}

这些代码是从另一个类调用的。

这里是onDraw方法的覆盖

public void onDraw(Canvas canvas)     {         short toolMode = mDICOMViewerData.getToolMode();         if(toolMode == ToolMode.MEASURE)         {             if(this.getImage()!= null)             {                 for(Point point:points)                 {                     canvas.drawCircle(point.x,point.y,1,油漆);                 }

            int[] outputPixels = new int[mImage.getDataLength()];

            // Get the gray scale window width
            int windowWidth = mDICOMViewerData.getWindowWidth();

            // Compute the window offset x the number of gray levels (256)
            int windowOffset = ((2 * mDICOMViewerData.getWindowCenter() - windowWidth)) / 2;

            switch(mDICOMViewerData.getCLUTMode()) {

            case CLUTMode.NORMAL:
                computeGrayscaleRGBImage(windowWidth, windowOffset, outputPixels);
                break;

            case CLUTMode.INVERSE:
                computeInverseGrayscaleRGBImage(windowWidth, windowOffset, outputPixels);
                break;

            case CLUTMode.RAINBOW:
                computeRainbowRGBImage(windowWidth, windowOffset, outputPixels);
                break;

            };

            // Create the bitmap
            Bitmap imageBitmap = Bitmap.createBitmap(outputPixels, mImage.getWidth(),mImage.getHeight(), Bitmap.Config.ARGB_8888);

            //drawing image here

            canvas.drawBitmap(imageBitmap, 153, 0, null);
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            canvas.drawLine(0, 0, 400, 400, paint);

            super.onDraw(canvas);
        }
    }
}

0 个答案:

没有答案