android影像編輯和處理(四)

來源:互聯網
上載者:User

標籤:android   圖片合成   android圖片編輯   

通過在Canvas對象上繪製一個位元影像對象,然後在相同的Canvas上繪製第二個映像來合成。區別在於繪製第二個映像時,需要再Paint對象上指定一個過渡模式(Xfermode)。

範例程式碼如下:

package com.example.testphotoedit;import java.io.FileNotFoundException;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.PorterDuffXfermode;import android.net.Uri;import android.os.Bundle;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class Picturecomposite extends Activity implements OnClickListener {private Button bt1, bt2;private ImageView composite;private static final int PICKED_ONE = 1;private static final int PICKED_TWO = 2;private static boolean onePicked=false;private static boolean twoPicked=false;private Bitmap bmp1,bmp2;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);bt1 = (Button) findViewById(R.id.picture_1);bt2 = (Button) findViewById(R.id.picture_2);composite = (ImageView) findViewById(R.id.composite_all);bt1.setOnClickListener(this);bt2.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubint which = -1;switch (v.getId()) {case R.id.picture_1:which = PICKED_ONE;break;case R.id.picture_2:which = PICKED_TWO;break;}Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(intent, which);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (resultCode == RESULT_OK) {Uri imageFileUri = data.getData();if(requestCode==PICKED_ONE){bmp1=loadBitmap(imageFileUri);onePicked=true;}else if(requestCode==PICKED_TWO){bmp2=loadBitmap(imageFileUri);twoPicked=true;}}//如果選擇了兩幅映像,且兩個位元影像均已完成執行個體化,繼續合成操作if(onePicked&&twoPicked){Bitmap alteredBitmap = Bitmap.createBitmap(bmp1.getWidth(),bmp1.getHeight(), bmp1.getConfig());//建立一個空的位元影像對象,大小和配置與第一個位元影像對象(bmp1)相同Canvas canvas=new Canvas(alteredBitmap);Paint  paint=new Paint();canvas.drawBitmap(bmp1, 0, 0, paint);paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));//執行個體化對象canvas.drawBitmap(bmp2, 0, 0, paint);composite.setImageBitmap(alteredBitmap);}}@SuppressWarnings("deprecation")private Bitmap loadBitmap(Uri imageFileUri) {Display currentDisply = getWindowManager().getDefaultDisplay();int dw = currentDisply.getWidth() / 2 - 100;int dh = currentDisply.getHeight() / 2 - 100;try {BitmapFactory.Options bmpFactory = new BitmapFactory.Options();bmpFactory.inJustDecodeBounds = true;// 載入映像的尺寸而非映像本身Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactory);int heightRatio = (int) Math.ceil(bmpFactory.outHeight / (float) dh);int widthRatio = (int) Math.ceil(bmpFactory.outWidth / (float) dw);if (heightRatio > 1 && widthRatio > 1) {if (heightRatio > widthRatio) {bmpFactory.inSampleSize = heightRatio;} else {bmpFactory.inSampleSize = widthRatio;}}bmpFactory.inJustDecodeBounds = false;// 載入真實的映像bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageFileUri), null, bmpFactory);return bmp;} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}}

activity_main.xml檔案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <Button         android:id="@+id/picture_1"        android:layout_width="match_parent"        android:layout_height="50dp"        android:text="choose Picture1"/>    <Button         android:id="@+id/picture_2"        android:layout_width="match_parent"        android:layout_height="50dp"        android:layout_below="@+id/picture_1"        android:text="choose Picture2"/>    <ImageView         android:id="@+id/composite_all"        android:layout_below="@+id/picture_2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="5dp"        android:contentDescription="@null"/></RelativeLayout>


運行結果如下:





 

android影像編輯和處理(四)

聯繫我們

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