当前位置:操作系统 > 安卓/Android >>

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

有种场景,我们想将一个图片切割成多个图片。比如我们在开发一个拼图的游戏,就首先要对图片进行切割。
以下是封装好的两个类,可以实现图片的切割。仅供参考和学习。
一个是ImagePiece类,此类保存了一个Bitmap对象和一个标识图片的顺序索引的int变量。
[java]  import android.graphics.Bitmap;  
public class ImagePiece {    
    public int index = 0;         
    public Bitmap bitmap = null;   

import android.graphics.Bitmap;
public class ImagePiece {  
    public int index = 0;       
    public Bitmap bitmap = null; 
}

一个是ImageSplitter类,有一个静态方法split,传入的参数是要切割的Bitmap对象,和横向和竖向的切割片数。比如传入的是3、3,则横竖向都切割成3片,最终会将整个图片切割成3X3=9片。
[java] import java.util.ArrayList;   
import java.util.List;   
   
import android.graphics.Bitmap;   
   
public class ImageSplitter {   
   
    public static List<ImagePiece> split(Bitmap bitmap, int xPiece, int yPiece) {   
   
        List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece);   
        int width = bitmap.getWidth();   
        int height = bitmap.getHeight();   
        int pieceWidth = width / 3;   
        int pieceHeight = height / 3;   
        for (int i = 0; i < yPiece; i++) {   
            for (int j = 0; j < xPiece; j++) {   
                ImagePiece piece = new ImagePiece();   
                piece.index = j + i * xPiece;   
                int xValue = j * pieceWidth;   
                int yValue = i * pieceHeight;   
                piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,   
                        pieceWidth, pieceHeight);   
                pieces.add(piece);   
            }   
        }   
   
        return pieces;   
    }   
   

import java.util.ArrayList; 
import java.util.List; 
 
import android.graphics.Bitmap; 
 
public class ImageSplitter { 
 
    public static List<ImagePiece> split(Bitmap bitmap, int xPiece, int yPiece) { 
 
        List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece); 
        int width = bitmap.getWidth(); 
        int height = bitmap.getHeight(); 
        int pieceWidth = width / 3; 
        int pieceHeight = height / 3; 
        for (int i = 0; i < yPiece; i++) { 
            for (int j = 0; j < xPiece; j++) { 
                ImagePiece piece = new ImagePiece(); 
                piece.index = j + i * xPiece; 
                int xValue = j * pieceWidth; 
                int yValue = i * pieceHeight; 
                piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue, 
                        pieceWidth, pieceHeight); 
                pieces.add(piece); 
            } 
        } 
 
        return pieces; 
    } 
 
}


1、图标加灰色过滤;
2、android的图片资源默认是静态的,单实例;如果两个IM好友的头像一样,最简单的都是用的软件自带头像,有一个在线,一个离线,直接改变头像的灰度,则两个用户的头像都会变灰或者在线,答案是:Drawable.mutate()。
[java] Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon);   
//Make this drawable mutable.    
//A mutable drawable is guaranteed to not share its state with any other drawable.    
mDrawable.mutate();   
ColorMatrix cm = new ColorMatrix();   
cm.setSaturation(0);   
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);   
mDrawable.setColorFilter(cf); 
Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon); 
//Make this drawable mutable. 
//A mutable drawable is guaranteed to not share its state with any other drawable. 
mDrawable.mutate(); 
ColorMatrix cm = new ColorMatrix(); 
cm.setSaturation(0); 
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm); 
mDrawable.setColorFilter(cf);


生成缩略图,抠自android launcher源码:
[java] /*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
&n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,