Android -- 影像處理(資訊量超大)

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   使用   

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);

這裡是手冊上寫的,我們可以開啟API Demos看一下效果:

顏色處理                                                                                      

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);    }}

我是天王蓋地虎的分割線                                                               

原始碼:http://pan.baidu.com/s/1dD1Qx01

圖形合成.zip

圖形顏色處理.zip

圖形變換.zip

 

 

 

轉載請註明出處:http://www.cnblogs.com/yydcdut

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.