灰度图像显示红色 - android

时间:2015-10-16 01:22:36

标签: java android bitmap pixel

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

    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[]像素数组,它正确显示灰度颜色。

1 个答案:

答案 0 :(得分:1)

使用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的内容为灰度。

相关问题