Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android拆分Bitmap完整示例

Android拆分Bitmap完整示例

編輯:關於Android編程

package cc.testsplitimage;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/**
 * Demo描述:
 * 拆分圖片
 * 
 * 參考資料:
 * http://blog.csdn.net/arui319/article/details/7470193
 * Thank you very much
 */
public class MainActivity extends Activity {
    private Button mSplitButton;
    private Bitmap mRawBitmap;
    private ImageView mImageView;
    private int row;
    private int column;
    private ArrayList<Bitmap> mPartImagesArrayLis;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
	
    private void init(){
    	mSplitButton=(Button) findViewById(R.id.splitButton);
    	mSplitButton.setOnClickListener(new ClickListenerImpl());
    	mImageView=(ImageView) findViewById(R.id.imageView);
    	mRawBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.e);
    	row=3;
    	column=3;
    }
    
    private class ClickListenerImpl implements OnClickListener{
		@Override
		public void onClick(View view) {
			switch (view.getId()) {
			case R.id.splitButton:
				mPartImagesArrayLis=splitImage(mRawBitmap, row, column);
				mImageView.setImageBitmap(null);
				break;
			default:
				break;
			}
		}
    }
    /**
     * @param rawBitmap 原來的Bitmap
     * @param row       切成幾行
     * @param column    切成幾列
     * @return
     */
    private ArrayList<Bitmap> splitImage(Bitmap rawBitmap,int row,int column){
    	ArrayList<Bitmap> partImagesArrayList=new ArrayList<Bitmap>(row*column);
    	int rawBitmapWidth=rawBitmap.getWidth();
    	int rawBitmapHeight=rawBitmap.getHeight();
    	System.out.println("rawBitmapWidth="+rawBitmapWidth+",rawBitmapHeight="+rawBitmapHeight);
    	int perPartWidth=rawBitmapWidth/column;
    	int perPartHeight=rawBitmapHeight/row;
    	System.out.println("perPartWidth="+perPartWidth+",perPartHeight="+perPartHeight);
    	Bitmap perBitmap=null;
    	for (int i = 0; i < row; i++) {
			for (int j = 0; j < column; j++) {
				int x=j*perPartWidth;
				int y=i*perPartHeight;
				System.out.println("i="+i+",j="+j+",x="+x+",y="+y);
				perBitmap=Bitmap.createBitmap(rawBitmap, x, y, perPartWidth, perPartHeight);
				partImagesArrayList.add(perBitmap);
			}
		}
    	System.out.println("size="+partImagesArrayList.size());
		return partImagesArrayList;
    }
   
}
<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/splitButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dip"
        android:text="拆分圖片" />

   

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/e" />

</RelativeLayout>

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved