灰度图像显示红色- android



我有一些奇怪的问题。我从int[]像素数组中设置了灰度Bitmap。所以我把这个

    bitmap = Bitmap.createBitmap(512, 512, Bitmap.Config.ARGB_8888);
    byte[] byteArray = new String(pixelsArray).getBytes("UTF-8");
    Bitmap bitmap = getBitmapFromByte(byteArray, pixelsArray.length);
    ImageView theImage = (ImageView) findViewById(R.id.echo);
    theImage.setImageBitmap(bitmap);

问题是,我得到了正确的图像,但在红色。所以我认为这与持有位图的ImageView包装器有关。

<LinearLayout
        android:id="@+id/some_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/theme_blue"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/echo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/some_image" />

所以我写了

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.vMiddle);
linearLayout.setBackgroundColor(Color.BLACK);

但它仍然显示为红色。有人有主意吗?顺便说一句,我在测试纯java应用程序上测试了int[]像素数组,它正确地显示了灰度色。

使用ColorMatrix和ColorMatrixColorFilter将ImageView的内容设置为灰度

ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);

然后简单地设置ImageView来使用滤镜

ImageView imageView = (ImageView)findViewById(R.id.imageView);
imageView.setColorFilter(filter);

这将使ImageView的内容为灰度

最新更新