Android的圖像處理提供的API很幫,但是不適合用來寫游戲,寫游戲還是用專門的引擎比較好。
Android的圖像處理還有3D的處理的API,感覺超屌。
我先分享一下Android的一般的處理,比如平移、翻轉等:
縮放、旋轉、平移、鏡面、倒影
縮放
復制代碼
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
img_one.setImageBitmap(bitmap1);
Bitmap alterBitmap = Bitmap.createBitmap(bitmap1.getWidth(),bitmap1.getHeight(),bitmap1.getConfig());
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
Matrix matrix = new Matrix();
matrix.setValues(new float[]{
0.5f, 0, 0,
0, 1, 0,
0, 0, 1
});
//matrix.setScale(2.0f, 1);
canvas.drawBitmap(bitmap1, matrix, paint);
img_two.setImageBitmap(alterBitmap);
復制代碼
旋轉
Matrix matrix = new Matrix();
//matrix.setRotate(180,bitmap1.getWidth()/2,bitmap1.getHeight()/2);
matrix.setRotate(15);
//消除鋸齒
paint.setAntiAlias(true);
平移
Matrix matrix = new Matrix();
//這個是當drawBitmap()的時候才發生變化
//matrix.setTranslate(10, 10);
//執行這句話就發生平移
matrix.postTranslate(10, 10);
鏡子
Matrix matrix = new Matrix();
//這個圖片要選擇左右不對稱的效果才明顯
matrix.setScale(-1, 1);
matrix.postTranslate(bitmap1.getWidth(), 0);
倒影
Matrix matrix = new Matrix();
matrix.setScale(1, -1);
matrix.postTranslate(0, bitmap1.getHeight());
合成
方法主要就是把兩張圖分別畫在Canvas上,Android的提供了API去設置這兩張圖片的合成形式:
復制代碼
iv = (ImageView) findViewById(R.id.iv);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.appstore);
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());
Canvas canvas = new Canvas(alterBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
//這裡是合成形式
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DARKEN));
canvas.drawBitmap(bitmap, new Matrix(), paint);
Bitmap ic_luncher = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(ic_luncher, new Matrix(), paint);
iv.setImageBitmap(alterBitmap);
復制代碼
image
這裡是手冊上寫的,我們可以打開API Demos看一下效果:
imageimage
顏色處理
android中可以通過顏色矩陣(ColorMatrix類)方面的操作顏色,顏色矩陣是一個5x4 的矩陣。
矩陣的運算規則是矩陣A的一行乘以矩陣C的一列作為矩陣R的一行,C矩陣是圖片中包含的ARGB信息,R矩陣是用顏色矩陣應用於C之後的新的顏色分量,運算結果如下:
R' = a*R + b*G + c*B + d*A + e;
G' = f*R + g*G + h*B + i*A + j;
B' = k*R + l*G + m*B + n*A + o;
A' = p*R + q*G + r*B + s*A + t;
顏色矩陣並不是看上去那麼深奧,其實需要使用的參數很少,而且很有規律第一行決定紅色第二行決定綠色第三行決定藍色,第四行決定了透明度,第五列是顏色的偏移量。
復制代碼
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());
canvas = new Canvas(alterBitmap);
paint = new Paint();
paint.setColor(Color.BLACK);
matrix = new Matrix();
final ColorMatrix cm = new ColorMatrix();
paint.setColorFilter(new ColorMatrixColorFilter(cm));
paint.setAntiAlias(true);
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(alterBitmap);
sb1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
int progress = seekBar.getProgress();
System.out.println("progress1--->"+progress);
cm.set(new float[]{
progress/128.0f,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(bitmap, matrix, paint);
iv.setImageBitmap(alterBitmap);
}
}