图片无法正常淡出

时间:2019-06-06 21:13:29

标签: android

我正在尝试显示一个图像,该图像在我的android应用中具有淡出的区域。 基本上,图像的左手边应该是不透明的,在中间有一个过渡区域,在该过渡区域中,图像逐渐淡入透明状态,其右侧的任何内容都将完全透明。我已经提出了一些代码,但是图像在“淡出区域”的右侧仅是半透明的-您仍然可以看到一些细节。在这个半完全透明的区域中,您应该能够看到视图中图像的下方-

我正在将此imageview添加到版式视图上:

               imageView = new ImageView( getApplicationContext() );
               frmLayout.addView( imageView, width, height );

这似乎正常。我能提供的最好的类比是,您仍然应该能够在该视图下看到正在发生的事情,有点像飞行员的平视显示器,但是正如我所说的,图像应该在右侧透明,事实并非如此。有人可以帮忙吗?我还希望使代码更高效。由于图像的大小和嵌套循环的原因,它非常缓慢,令人失望。

    int seekbarValue = (int) (progressChangedValue*img2.getWidth()/simpleSeekBar.getMax());
    int maxVal = seekbarValue + widthValue/2;
    int minVal = seekbarValue - widthValue/2;

// seekbarValue is the "mid point" of the faded out region
// widthValue is how wide the faded out region will be
// so, in the other words the fade out should begin at minVal
// and should end at maxVal; anything to the right of this is transparent

    if(maxVal>img2.getWidth()) maxVal=img2.getWidth();
    if(minVal<0) minVal=0;

    int startOfOpacity = minVal;
    int endOfOpacity = maxVal;

    double opacityScaler = (double) 255/(endOfOpacity-startOfOpacity);
    // This is how much the "fade zone" degrades the opacity per pixel


    double Opacity=255;
    int OpacityInt=255;

     // img2 is the loaded image

    for(int length=0; length<img2.getWidth(); length++)
    {


            if( length<startOfOpacity )
            {
                OpacityInt=255;

            }
            else if( length>=startOfOpacity && length<endOfOpacity )
            {
                Opacity-=opacityScaler;
                OpacityInt = (int) Opacity;
            }
            else
            {
                OpacityInt = 0;
            }


        }


        for(int height=0; height<img2.getHeight(); height++)
        {

            int myColor = img2.getPixel(length, height); // width, height

            int redValue = Color.red(myColor);
            int blueValue = Color.blue(myColor);
            int greenValue = Color.green(myColor);

            img2.setPixel(length, height, Color.argb(OpacityInt, redValue, greenValue, blueValue));


        }
    }

    // imageView is the view to draw the faded out image to

    Bitmap bitmapx = Bitmap.createBitmap(imageView.getMeasuredWidth(), imageView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
// width and height are the dimensions of the imageview
    Canvas canvas = new Canvas(bitmapx);
    canvas.drawColor( Color.argb(0,0,0,0)); 

// background of the imageview is transparent; the opaque area of img2
// should obscure the view "behind" the
// imageview, but for the completely and partially transparent regions, 
// you should be able to see what is underneath.

    canvas.drawBitmap( img2, left, top, null ); // the bitmap is offset from 0,0 on the canvas
    imageView.setImageBitmap(bitmapx);

0 个答案:

没有答案