国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現(xiàn)圖片特效的方法

2020-02-21 17:35:29
字體:
供稿:網(wǎng)友

在日常的開發(fā)過程中,圖像的特效處理是一個非常普遍的要求,其實除了顏色效果外,還有形狀的特殊效果,今天武林技術(shù)頻道小編將帶大家進入下文學(xué)習(xí)Android實現(xiàn)圖片特效的方法。

1.黑白效果

?

復(fù)制代碼 代碼如下:

/**
???? * 將彩色圖轉(zhuǎn)換為黑白圖
???? *
???? * @param 位圖
???? * @return 返回轉(zhuǎn)換好的位圖
???? */
??? public static Bitmap convertToBlackWhite(Bitmap bmp) {
??????? int width = bmp.getWidth(); // 獲取位圖的寬
??????? int height = bmp.getHeight(); // 獲取位圖的高

?

??????? int[] pixels = new int[width * height]; // 通過位圖的大小創(chuàng)建像素點數(shù)組

??????? bmp.getPixels(pixels, 0, width, 0, 0, width, height);
??????? int alpha = 0xFF << 24;
??????? for (int i = 0; i < height; i++) {
??????????? for (int j = 0; j < width; j++) {
??????????????? int grey = pixels[width * i + j];

??????????????? int red = ((grey & 0x00FF0000) >> 16);
??????????????? int green = ((grey & 0x0000FF00) >> 8);
??????????????? int blue = (grey & 0x000000FF);

??????????????? grey = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
??????????????? grey = alpha | (grey << 16) | (grey << 8) | grey;
??????????????? pixels[width * i + j] = grey;
??????????? }
??????? }
??????? Bitmap newBmp = Bitmap.createBitmap(width, height, Config.RGB_565);
??????? newBmp.setPixels(pixels, 0, width, 0, 0, width, height);
??????? return newBmp;
??? }

?

2.圖片圓角

?

復(fù)制代碼 代碼如下:

/**
???? * 轉(zhuǎn)換成圓角
???? *
???? * @param bmp
???? * @param roundPx
???? * @return
???? */
??? public static Bitmap convertToRoundedCorner(Bitmap bmp, float roundPx) {

?

??????? Bitmap newBmp = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
??????????????? Config.ARGB_8888);
??????? // 得到畫布
??????? Canvas canvas = new Canvas(newBmp);

??????? final int color = 0xff424242;
??????? final Paint paint = new Paint();
??????? final Rect rect = new Rect(0, 0, bmp.getWidth(), bmp.getHeight());
??????? final RectF rectF = new RectF(rect);

??????? paint.setAntiAlias(true);
??????? canvas.drawARGB(0, 0, 0, 0);
??????? paint.setColor(color);
??????? // 第二個和第三個參數(shù)一樣則畫的是正圓的一角,否則是橢圓的一角
??????? canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

??????? paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
??????? canvas.drawBitmap(bmp, rect, rect, paint);

??????? return newBmp;
??? }

?

3.高斯模糊

?

復(fù)制代碼 代碼如下:

/**
???? * 高斯模糊
???? *
???? * @param bmp
???? * @return
???? */
??? public static Bitmap convertToBlur(Bitmap bmp) {
??????? // 高斯矩陣
??????? int[] gauss = new int[] { 1, 2, 1, 2, 4, 2, 1, 2, 1 };

?

??????? int width = bmp.getWidth();
??????? int height = bmp.getHeight();
??????? Bitmap newBmp = Bitmap.createBitmap(width, height,
??????????????? Bitmap.Config.RGB_565);

??????? int pixR = 0;
??????? int pixG = 0;
??????? int pixB = 0;

??????? int pixColor = 0;

??????? int newR = 0;
??????? int newG = 0;
??????? int newB = 0;

??????? int delta = 16; // 值越小圖片會越亮,越大則越暗

??????? int idx = 0;
??????? int[] pixels = new int[width * height];
??????? bmp.getPixels(pixels, 0, width, 0, 0, width, height);
??????? for (int i = 1, length = height - 1; i < length; i++) {
??????????? for (int k = 1, len = width - 1; k < len; k++) {
??????????????? idx = 0;
??????????????? for (int m = -1; m <= 1; m++) {
??????????????????? for (int n = -1; n <= 1; n++) {
??????????????????????? pixColor = pixels[(i + m) * width + k + n];
??????????????????????? pixR = Color.red(pixColor);
??????????????????????? pixG = Color.green(pixColor);
??????????????????????? pixB = Color.blue(pixColor);

??????????????????????? newR = newR + pixR * gauss[idx];
??????????????????????? newG = newG + pixG * gauss[idx];
??????????????????????? newB = newB + pixB * gauss[idx];
??????????????????????? idx++;
??????????????????? }
??????????????? }

??????????????? newR /= delta;
??????????????? newG /= delta;
??????????????? newB /= delta;

??????????????? newR = Math.min(255, Math.max(0, newR));
??????????????? newG = Math.min(255, Math.max(0, newG));
??????????????? newB = Math.min(255, Math.max(0, newB));

??????????????? pixels[i * width + k] = Color.argb(255, newR, newG, newB);

??????????????? newR = 0;
??????????????? newG = 0;
??????????????? newB = 0;
??????????? }
??????? }

??????? newBmp.setPixels(pixels, 0, width, 0, 0, width, height);

??????? return newBmp;
??? }

以上就是Android實現(xiàn)圖片特效的方法介紹,更多內(nèi)容請繼續(xù)關(guān)注武林技術(shù)頻道的內(nèi)容!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 青神县| 南川市| 大宁县| 藁城市| 高清| 和林格尔县| 深圳市| 竹溪县| 香河县| 兰西县| 扬中市| 梧州市| 儋州市| 颍上县| 商河县| 上栗县| 晋城| 台江县| 灯塔市| 綦江县| 保靖县| 太谷县| 长兴县| 邵东县| 白沙| 高台县| 衡阳县| 通榆县| 晋城| 阿坝县| 丹棱县| 阜阳市| 阳春市| 玛曲县| 鹰潭市| 香港| 泗洪县| 嘉禾县| 百色市| 碌曲县| 托里县|