首页 > 系统 > Android > 正文

Android开发之图片切割工具类定义与用法示例

2019-10-22 18:24:00
字体:
来源:转载
供稿:网友

本文实例讲述了Android开发之图片切割工具类定义与用法。分享给大家供大家参考,具体如下:

该工具类比较常见于拼图游戏中使用。这里演示了类基本的定义与使用方法。

图片切割工具类定义:

public class ImageSplitter{  /**   * 将图片切成 , piece *piece   *   * @param bitmap   * @param piece   * @return   */  public static List<ImagePiece> split(Bitmap bitmap, int piece)  {    List<ImagePiece> pieces = new ArrayList<ImagePiece>(piece * piece);    int width = bitmap.getWidth();    int height = bitmap.getHeight();    Log.e("TAG", "bitmap Width = " + width + " , height = " + height);    int pieceWidth = Math.min(width, height) / piece;    for (int i = 0; i < piece; i++)    {      for (int j = 0; j < piece; j++)      {        ImagePiece imagePiece = new ImagePiece();        imagePiece.index = j + i * piece;        int xValue = j * pieceWidth;        int yValue = i * pieceWidth;        imagePiece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,            pieceWidth, pieceWidth);        pieces.add(imagePiece);      }    }    return pieces;  }}

图片切割实体类:

public class ImagePiece{  public int index = 0;  public Bitmap bitmap = null;}

使用方法:

private void initBitmap(){    if (mBitmap == null)      mBitmap = BitmapFactory.decodeResource(getResources(),          R.drawable.aa);    List<ImagePiece> mItemBitmaps = ImageSplitter.split(mBitmap, mColumn);    Collections.sort(mItemBitmaps, new Comparator<ImagePiece>()    {      @Override      public int compare(ImagePiece lhs, ImagePiece rhs)      {        return Math.random() > 0.5 ? 1 : -1;      }    });}

希望本文所述对大家Android程序设计有所帮助。


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表