学习Android切割图片并保存到项目文件夹

学习如何在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;
	}

}



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值