码迷,mamicode.com
首页 > 移动开发 > 详细

Android中将一个图片切割成多个图片

时间:2019-01-23 18:13:57      阅读:295      评论:0      收藏:0      [点我收藏+]

标签:get   参数   使用   idt   imp   好的   实体类   com   graph   

有种场景,我们想将一个图片切割成多个图片。比如我们在开发一个拼图的游戏,就首先要对图片进行切割。

以下是封装好的两个类,可以实现图片的切割。仅供参考和学习。

一个是ImagePiece类,此类保存了一个Bitmap对象和一个标识图片的顺序索引的int变量。

 1 package com.example.imagesplitter;
 2 
 3 import android.graphics.Bitmap;
 4 
 5 /**
 6  * 图片切割实体类
 7  */
 8 public class ImagePiece {
 9     public int index = 0;
10     public Bitmap bitmap = null;
11 }

一个是ImageSplitter类,有一个静态方法split,传入的参数是要切割的Bitmap对象,和横向和竖向的切割片数

 1 package com.example.imagesplitter;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.graphics.Bitmap;
 7 
 8 /**
 9  * 图片切割工具类
10  */
11 public class ImageSplitter {
12 
13     /**
14      * 图片切割
15      * @param bitmap 导入图片
16      * @param x x轴切割
17      * @param y y轴切割
18      * @return
19      */
20     public static List<ImagePiece> split(Bitmap bitmap, int x, int y) {
21         List<ImagePiece> pieces = new ArrayList<ImagePiece>();
22         int width = bitmap.getWidth();
23         int height = bitmap.getHeight();
24         int pieceWidth = width / x;
25         int pieceHeight = height / y;
26         for (int i = 0; i < y; i++) {
27             for (int j = 0; j < x; j++) {
28                 ImagePiece image = new ImagePiece();
29                 image.index = j + i * x;
30                 int xValue = j * pieceWidth;
31                 int yValue = i * pieceHeight;
32                 image.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,
33                         pieceWidth, pieceHeight);
34                 pieces.add(image);
35             }
36         }
37         return pieces;
38     }
39 }

这里的切割,主要使用的是Bitmap对象的createBitmap方法,不再做具体描述。

 

Android中将一个图片切割成多个图片

标签:get   参数   使用   idt   imp   好的   实体类   com   graph   

原文地址:https://www.cnblogs.com/MrChen-/p/10310422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!