学习如何在Android 里将一张图片切割成几张的图片,并保存到相应项目的文件夹下。
布局:行数,列数,图片,按钮切割
布局文件:
<LinearLayout 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"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="150dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rows" />
<EditText
android:id="@+id/edRows"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="150dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cols" />
<EditText
android:id="@+id/edCols"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="@+id/orgImg"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:src="@drawable/orgimg" />
<Button
android:id="@+id/btCut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/cut" />
</LinearLayout>
MainActiviey:
package com.lyang.cutimage;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText edRows, edCols;
private int row, col;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edRows = (EditText) findViewById(R.id.edRows);
edCols = (EditText) findViewById(R.id.edCols);
Button btnCut = (Button) findViewById(R.id.btCut);
btnCut.setOnClickListener(new btnCutClickLister());
}
private final class btnCutClickLister implements OnClickListener{
@Override
public void onClick(View v) {
/*
* 得到行数和列数
*/
int rows, cols;
String str;
str = edRows.getText().toString();
try {
rows = Integer.parseInt(str);
} catch (Exception e) {
rows = 1;
}
str = edCols.getText().toString();
try {
cols = Integer.parseInt(str);
} catch (Exception e) {
cols = 1;
}
/*
* 取得要切割的图片
*/
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.orgimg);
/*
* 切割图片
*/
ImageCut imgCut = new ImageCut();
imgCut.context = MainActivity.this;
if (imgCut.cutImage(bmp, cols, rows)) {
Toast.makeText(MainActivity.this, "切割成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "切割失败", Toast.LENGTH_SHORT).show();
}
}
}
}
切割图片的类:
package com.lyang.cutimage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.graphics.Bitmap;
public class ImageCut {
public Context context;
<span style="white-space:pre"> </span>/*
<span style="white-space:pre"> </span> *切割图片
<span style="white-space:pre"> </span>*/
public Boolean cutImage(Bitmap bmp, int cols, int rows) {
int width = bmp.getWidth();
int height = bmp.getHeight();
int oneWidth = width / cols;
int oneHeight = height / rows;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int xValue = j * oneWidth;
int yValue = i * oneHeight;
Bitmap newBmp = Bitmap.createBitmap(bmp, xValue, yValue,
oneWidth, oneHeight);
saveImage(newBmp, "Cut_" +Integer.toString(i * rows + j)+ ".jpg");
}
}
return true;
}
/*
* 保存图片到项目文件夹下
*/
public void saveImage(Bitmap bmp, String picName) {
// 得到项目文件夹 /data/data/com.xxxx.xxxx/files
File f = new File(context.getFilesDir().getAbsolutePath(), picName);
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Context getContext() {
return context;
}
public void setContext(Context context) {
this.context = context;
}
}