Android拆分Bitmap完整示例

MainActivity如下:

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

}

 

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/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>

 

时间: 2024-12-24 22:09:38

Android拆分Bitmap完整示例的相关文章

Android中AutoCompleteTextView完整示例(一)

MainActivity如下: package cc.testautocompletetextview; import cc.testautocompletetextview1.R; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.app.Activity; /** * Demo描述 * AutoComp

Android中AutoCompleteTextView完整示例(二)

MainActivity如下: package cc.testautocompletetextview2; import android.os.Bundle; import android.app.Activity; /** * Demo描述 * 利用自定义AutoCompleteTextView完成邮箱自动补全功能 * * 参考资料: * 1 http://blog.csdn.net/fx_sky/article/details/9326129 * 2 http://blog.csdn.net

Android图片旋转,缩放,位移,倾斜,对称完整示例(二)——Bitmap.createBitmap()和Matrix

MainActivity如下: package cc.c; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.widget.ImageView; /** * Demo描述: * 利用B

Android清理设备内存详细完整示例(二)

MainActivity如下: package cc.c; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.List; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.MemoryInfo;

Android点亮屏幕和解锁完整示例

MainActivity如下: package cc.test.testwakelock; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; /** * Demo描述: * 点亮屏幕和解开键盘锁完整示例 * * 注意权限: * <uses-permission android:name="android.p

Android图片旋转,缩放,位移,倾斜,对称完整示例(一)——imageView.setImageMatrix(matrix)和Matrix

MainActivity如下: import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import android.app.Activity; import android.graphics.Matrix; /** * Demo描述:

Android利用universal-image-loader异步加载大量图片完整示例

MainActivity如下: package cc.testlistview; import java.util.ArrayList; import com.example.testlistview.R; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.core.ImageLoader; import

Android利用Volley异步加载数据(JSON和图片)完整示例

MainActivity如下: package cc.testvolley; import org.json.JSONObject; import android.app.Activity; import android.app.ProgressDialog; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.util.LruCache; import android.widge

利用LruCache为GridView加载大量本地图片完整示例

MainActivity如下: package cc.testlrucache; import android.os.Bundle; import android.widget.GridView; import android.app.Activity; /** * Demo描述: * Android利用LruCache为GridView加载大量本地图片完整示例,防止OOM * * 更多参考: * http://blog.csdn.net/lfdfhl */ public class MainA