Android的允許通過(guò)添加不同種類(lèi)的處理圖像效果??梢暂p松地應(yīng)用圖像處理技術(shù)來(lái)增加某些種類(lèi)的圖像效果。這些影響可能是亮度,黑暗中,灰度轉(zhuǎn)換等
Android提供了Bitmap類(lèi)來(lái)處理圖像。這可以在 android.graphics.bitmap 下找到。有很多種方法,通過(guò)它可以位圖 Bitmap 實(shí)例調(diào)用。如下創(chuàng)建的圖像從ImageView的位圖。
private Bitmap bmp; private ImageView img; img = (ImageView)findViewById(R.id.imageView1); BitmapDrawable abmp = (BitmapDrawable)img.getDrawable();
現(xiàn)在,我們將通過(guò)調(diào)用BitmapDrawable類(lèi)的getBitmap()函數(shù)來(lái)創(chuàng)建位圖。它的語(yǔ)法如下:
bmp = abmp.getBitmap();
圖像不過(guò)是一個(gè)二維矩陣。同樣的方式處理位圖。圖像由像素組成。所以,從中得到位圖的像素并應(yīng)用處理它。它的語(yǔ)法如下:
for(int i=0; i<bmp.getWidth(); i++){ for(int j=0; j<bmp.getHeight(); j++){ int p = bmp.getPixel(i, j); } }
所述的 getWidth()和 getHeight()函數(shù)返回矩陣的高度和寬度。使用getPixel()方法返回像素的指定索引處。得到了像素之后可以根據(jù)需要方便地操縱它。
除了這些方法,還有其他方法,幫助我們更好地處理圖像。
| Sr.No | 方法及說(shuō)明 |
|---|---|
| 1 |
copy(Bitmap.Config config, boolean isMutable) 這種方法復(fù)制此位圖的像素到新位圖 |
| 2 |
createBitmap(DisplayMetrics display, int width, int height, Bitmap.Config config) 返回一個(gè)可變的位圖指定的寬度和高度 |
| 3 |
createBitmap(int width, int height, Bitmap.Config config) 返回一個(gè)可變的位圖指定的寬度和高度 |
| 4 |
createBitmap(Bitmap src) 從源位圖返回一個(gè)不可變位 |
| 5 |
extractAlpha() 返回一個(gè)新的位圖,它捕獲原有的alpha值 |
| 6 |
getConfig() 這個(gè)方法返回配置,或者返回null |
| 7 |
getDensity() 返回此位圖密度 |
| 8 |
getRowBytes() 返回位圖的像素字節(jié)行之間的數(shù) |
| 9 |
setPixel(int x, int y, int color) 寫(xiě)入指定的顏色成位圖(假設(shè)它是可變的)在X,Y坐標(biāo) |
| 10 |
setDensity(int density) 這種方法指定此位圖的密度 |
下面的例子演示了一些對(duì)位圖上的圖像效果。它創(chuàng)建了一個(gè)基本的應(yīng)用程序,讓圖片轉(zhuǎn)換成灰度等等。
為了試驗(yàn)這個(gè)例子,需要在實(shí)際設(shè)備上運(yùn)行此程序。
| 步驟 | 描述 |
|---|---|
| 1 | 使用Eclipse IDE創(chuàng)建Android應(yīng)用程序,并將其命名為 ImageEffects。在創(chuàng)建這個(gè)項(xiàng)目時(shí)確保目標(biāo)SDK編譯在A(yíng)ndroid SDK的最新版本或使用更高級(jí)別的API。 |
| 2 | 修改 src/MainActivity.java文件中添加必要的代碼 |
| 3 | 修改res/layout/activity_main添加相應(yīng)的XML組件 |
| 4 | 修改res/values/string.xml添加必要的字符串 |
| 5 | 運(yùn)行應(yīng)用程序并選擇運(yùn)行Android的設(shè)備,并在其上安裝的應(yīng)用和驗(yàn)證結(jié)果 |
以下是修改主活動(dòng)文件的內(nèi)容src/com.yiibai.imageeffects/MainActivity.java.
package com.example.imageeffects; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView img; private Bitmap bmp; private Bitmap operation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img = (ImageView)findViewById(R.id.imageView1); BitmapDrawable abmp = (BitmapDrawable)img.getDrawable(); bmp = abmp.getBitmap(); } public void gray(View view){ operation= Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),bmp.getConfig()); double red = 0.33; double green = 0.59; double blue = 0.11; for(int上一篇:Android撥打電話(huà)下一篇:Android LinearLayout