当前位置:操作系统 > 安卓/Android >>

Android拆分Bitmap完整示例

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>

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,