In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve picture grayscale display in Android development. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
The typical application of grayscale display picture is the user's profile picture, such as the user's online profile picture shows color (original image), but not gray (black and white image). To sum up, it is a more original picture to calculate the different display effects through the color filtering processing. The API of this method is mainly located in: android.
Using the gray scale calculation method mentioned in the article "three algorithms to convert color grayscale" mentioned above, the black-and-white image display effect is as follows:
Description: Use Matrix is achieved using Android's ColorMatrix and ColorFilter, where setting ColorMatrix's setSaturation (float sat) saturation to 0 displays grayscale after color filtering, and the internal implementation of android.graphics.ColorMatrix and the specific RGB color weight values are approximately equal to the weights in the BT709 in the figure.
Code example (depending on the grayscale calculation method attached in this article to encapsulate the class)
@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_grayscale); ButterKnife.bind (this); BitmapDrawable bd = (BitmapDrawable) Original_ImageView.getDrawable (); Bitmap bitmap = bd.getBitmap (); Log.d (TAG, "w =" + bitmap.getWidth () + ", h =" + bitmap.getHeight () + ", c =" + bitmap.getConfig () .toString ()) / / 0 BT709 Bitmap matrix = Bitmap.createBitmap (bitmap.getWidth (), bitmap.getHeight (), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas (matrix); Paint paint = new Paint (); ColorMatrix colorMatrix = new ColorMatrix (); / / passing a number greater than 1 increases saturation, while passing a number between 0 and 1 reduces saturation. A value of 0 will result in a grayscale image / / Android ColorMatrix default grayscale calculation uses the following BT709 standard colorMatrix.setSaturation (0f); ColorMatrixColorFilter colorMatrixColorFilter = new ColorMatrixColorFilter (colorMatrix); paint.setColorFilter (colorMatrixColorFilter); canvas.drawBitmap (bitmap, 0f, 0f, paint); Matrix_ImageView.setImageBitmap (matrix); / / original image Bitmap sunflower = XUtils.BitmapUtil.decodeMutableBitmapFromResourceId (this, R.drawable.sunflower) / / 1 Bitmap lightness = grayScale (sunflower, XUtils.GrayScaleUtil.GrayScale.Lightness); Lightness_ImageView.setImageBitmap (lightness); / / 2 Bitmap average = grayScale (sunflower, XUtils.GrayScaleUtil.GrayScale.Average); Average_ImageView.setImageBitmap (average); / / 3 Bitmap luminosity = grayScale (sunflower, XUtils.GrayScaleUtil.GrayScale.Luminosity); Luminosity_ImageView.setImageBitmap (luminosity) / / 4 Bitmap bt709 = grayScale (sunflower, XUtils.GrayScaleUtil.GrayScale.BT709); BT709_ImageView.setImageBitmap (bt709); / / 5 Bitmap rmy = grayScale (sunflower, XUtils.GrayScaleUtil.GrayScale.RMY); RMY_ImageView.setImageBitmap (rmy); / / 6 Bitmap y = grayScale (sunflower, XUtils.GrayScaleUtil.GrayScale.Y); Y_ImageView.setImageBitmap (y) } public Bitmap grayScale (final Bitmap bitmap, XUtils.GrayScaleUtil.GrayScale grayScale) {if (null = = bitmap | | null = = grayScale) {return null;} Bitmap rs = Bitmap.createBitmap (bitmap.getWidth (), bitmap.getHeight (), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas (rs); Paint paint = new Paint (); for (int x = 0, w = bitmap.getWidth (); x < w ) {for (int y = 0, h = bitmap.getHeight (); y < h; YBG +) {int c = bitmap.getPixel (x, y); int a = Color.alpha (c); int r = Color.red (c); int g = Color.red (c); int b = Color.blue (c) Int gc = grayScale.grayScale (r, g, b); paint.setColor (Color.argb (a, gc, gc, gc)); canvas.drawPoint (x, y, paint);}} return rs;}
This is the end of this article on "how to achieve picture grayscale display in Android development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.