android使用 2D 方法實現倒影特效

來源:互聯網
上載者:User

每一個映像像素通過一個4位元組整數來展現。最高位位元組用作alpha通道;換言之用來實現透明/不透明控制。255代表完全不透明;0則代表完全透明。接下來一個位元組是red紅色通道;255代表完全是紅色。依次類推接下來兩個位元組相應的實現綠色和藍色通道。

現在你可以對單獨的像素進行處理了。通過使用{
tagshow(event)
}">android.graphics.Bitmap {
tagshow(event)
}">API中的getPixels,可以{
tagshow(event)
}">載入像素到一個整數數組中。在本文{
tagshow(event)
}">例子中,你將按照一定規則對每一個像素實現著色。經過這個處理後,所有的像素將被轉化為一個範圍在0到255的位元組碼。android.graphics.Bitmap API中的setPixels則用來載入這個整數數組到一個映像中。最後一步是通過Image{
tagshow(event)
}">View變數mIV來更新{
tagshow(event)
}">螢幕。以下是實現這個染色過程的{
tagshow(event)
}">程式碼片段。

private void TintThePicture(int deg) {

int[] pix = new int[picw * pich];

mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y;

double angle = (3.14159d * (double)deg) / 180.0d;

int S = (int)(256.0d * Math.sin(angle));

int C = (int)(256.0d * Math.cos(angle));

for (int y = 0; y < pich; y++)

for (int x = 0; x < picw; x++)

{

int index = y * picw + x;

int r = (pix[index] >> 16) & 0xff;

int g = (pix[index] >> 8) & 0xff;

int b = pix[index] & 0xff;

RY = ( 70 * r – 59 * g – 11 * b) / 100;

GY = (-30 * r + 41 * g – 11 * b) / 100;

BY = (-30 * r – 59 * g + 89 * b) / 100;

Y = ( 30 * r + 59 * g + 11 * b) / 100;

RYY = (S * BY + C * RY) / 256;

BYY = (C * BY – S * RY) / 256;

GYY = (-51 * RYY – 19 * BYY) / 100;

R = Y + RYY;

R = (R < 0) ? 0 : ((R > 255) ? 255 : R);

G = Y + GYY;

G = (G < 0) ? 0 : ((G > 255) ? 255 : G);

B = Y + BYY;

B = (B < 0) ? 0 : ((B > 255) ? 255 : B);

pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;

}

Bitmap bm = Bitmap.createBitmap(picw, pich, false);

bm.setPixels(pix, 0, picw, 0, 0, picw, pich);

// Put the updated bitmap into the main view

mIV.setImageBitmap(bm);

mIV.invalidate();

mBitmap = bm;

pix = null;

}

 

 

使用 2D 方法實現倒影特效

倒影可以為 UI {
tagshow(event)
}">介面帶來立體感,是一個常用的 UI 特效。是一個圖片瀏覽{
tagshow(event)
}">程式的 GridView,該 View 的倒影特效為 UI 介面增色不少。


實現原理

實現倒影可以使用 OpenGL 等 3D 介面方法,也可以用 2D 的方法{
tagshow(event)
}">類比。

用 2D 方法實現倒影需要從兩個方面考慮:

1.倒影是上、下翻轉的映像;

2.從上到下透明度越來越大。


映像翻轉的實現

原理上講,映像的翻轉實際就是將映像{
tagshow(event)
}">資料上下行互換。

Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

 

for (int y = 0; y < h; y++)

{

      bm.setPixels(srcPix, y * w, w, 0, h - y - 1, w, 1);

}

 

 

透明度漸層

實現透明度漸層有兩種方法,一是使用蒙板;二是直接修改像素資料,修改每個
像素的 alpha 值。

對於蒙板法,事先做好一張透明度漸層的圖,這就是我們的蒙板,在繪製完映像之後把蒙板圖片繪製上去,這樣就產生了透明度漸層的{
tagshow(event)
}">效果。

對於第二種方法,我們需要首先讀出圖片的資料,然後修改每個像素的 alpha 值。下面的程式碼片段的{
tagshow(event)
}">功能是逐行增加 alpha 值,產生的效果是自上向下由暗
變亮。

int alpha = 0x00000000;

 

mBitmap.getPixels (pix, 0, w, 0, 0, w, h);

 

for (int y = 0; y<h; y++) {

for (int x=0; x<w; x++) {

int index = y * w + x;

int r = (pix[index] >> 16) & 0xff;

int g = (pix[index] >> 8) & 0xff;

int b = pix[index] & 0xff;

 

pix[index] = alpha | (r<<16) | (g<<8) | b;

}

alpha = alpha + 0x01000000;

 

}

{
showMenu({'ctrlid':this.id,'pos':'13'})
}">

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.