学习Android切割图片并保存到项目文件夹
来源:互联网 发布:阿里云 证书被ios信任 编辑:程序博客网 时间:2024/05/16 03:10
学习如何在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{@Overridepublic 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/filesFile 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;}}
0 0
- 学习Android切割图片并保存到项目文件夹
- opencv切割并保存图片
- Android保存Bitmap图片到指定文件夹
- Android保存图片到自定义文件夹并展示在系统图库
- opencv视频转图片并保存到文件夹下
- Android画图并保存图片到本地
- android 图片获取策略,下载并保存至指定文件夹
- android异常收集错误信息并保存到crash文件夹下
- android图片添加文字保存到指定文件夹
- android保存Bitmap图片到指定文件夹示例
- android 保存bitmap图片到sd卡中文件夹
- Android将图片保存到data文件夹中
- android保存Bitmap图片到指定文件夹中的方法
- Android保存图片到系统图库 或者指定文件夹
- android代码截屏截切图片并且保存到指定文件夹中
- Android学习笔记进阶18之画图并保存图片到本地
- Android学习笔记进阶18之画图并保存图片到本地
- Android学习笔记进阶18之画图并保存图片到本地
- leetcode[104]:Maximum Depth of Binary Tree
- Java NIO:NIO概述
- 下面宏的作用是什么 结构体
- SAT数学:必背公式之三角函数
- 封装,继承和多态
- 学习Android切割图片并保存到项目文件夹
- 彻底解决Spring MVC 中文乱码 问题
- UVALive 3135--Argus+自定义优先队列的优先规则
- JAVA基础--集合
- 解读XMP-元数据在ALAssetRepresentation
- PL/SQL Developer 连接远程的Oracle 服务器
- Netflix工程总监眼中的分类算法:深度学习优先级最低
- #ifndef、#def、#endif说明
- cocos2d-x3.0 + vs2012环境配置