首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android:如何不使用画布来分割图像

Android:如何不使用画布来分割图像
EN

Stack Overflow用户
提问于 2015-04-21 21:18:36
回答 3查看 1.9K关注 0票数 1

我正在制作一个滑动型益智游戏(拼图)。我希望用户能够选择一个图像,然后显示该图像。

然而,在我做到这一点之前,我需要将他们选择的图像分割成9个不同的方格,组成原始图像。

关于如何在没有画布的情况下做到这一点有什么想法吗?或者我应该用画布代替。

谢谢:)

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-21 21:27:06

您可以使用Bitmap API。看一下这个例子:http://androidattop.blogspot.de/2012/05/splitting-image-into-smaller-chunks-in.html

在上面的链接上提供的示例中,提供了一个ImageView,您将从中获得图像的位图。然后,您可以暂时将拆分的部分保存到位图数组( arraylist )中。唯一剩下的就是将这些位图提供到GridView中。下面是您需要在代码中添加的方法:

代码语言:javascript
运行
复制
private void splitImage(ImageView image, int chunkNumbers) { 

    //For the number of rows and columns of the grid to be displayed
    int rows,cols;

    //For height and width of the small image chunks 
    int chunkHeight,chunkWidth;

    //To store all the small image chunks in bitmap format in this list 
    ArrayList<bitmap> chunkedImages = new ArrayList<bitmap>(chunkNumbers);

    //Getting the scaled bitmap of the source image
    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);

    rows = cols = (int) Math.sqrt(chunkNumbers);
    chunkHeight = bitmap.getHeight()/rows;
    chunkWidth = bitmap.getWidth()/cols;

    //xCoord and yCoord are the pixel positions of the image chunks
    int yCoord = 0;
    for(int x=0; x<rows; x++){
        int xCoord = 0;
        for(int y=0; y<cols; y++){
            chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
            xCoord += chunkWidth;
        }
        yCoord += chunkHeight;
    }

    /* Now the chunkedImages has all the small image chunks in the form of Bitmap class. 
     * You can do what ever you want with this chunkedImages as per your requirement.
     * I pass it to a new Activity to show all small chunks in a grid for demo.
     * You can get the source code of this activity from my Google Drive Account.
     */

    //Start a new activity to show these chunks into a grid 
    Intent intent = new Intent(ImageActivity.this, ChunkedImageActivity.class);
    intent.putParcelableArrayListExtra("image chunks", chunkedImages);
    startActivity(intent);
}

请注意,在上面的示例中使用了一个意图和一个putParcelableArrayListExtra来将它们传递到一个新的活动中,但是您并不需要这样做。因为您可以在布局中声明一个GridView。然后,您可以创建一个类,它扩展BaseAdapter类并设置图像。最后,调用GridView的GridView方法。

票数 3
EN

Stack Overflow用户

发布于 2015-04-21 21:31:32

这可能是您的解决方案:在android中切割拼图形状的图像

下面是是一个完整的例子,我特别要看一下createPieces方法:

票数 0
EN

Stack Overflow用户

发布于 2015-04-21 21:36:34

下面的代码片段显示了如何将图像分割/分割成较小的块/块。

代码语言:javascript
运行
复制
package com.android.imagesplitter;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class ImageActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b1 = (Button) findViewById(R.id.three);
        Button b2 = (Button) findViewById(R.id.four);
        Button b3 = (Button) findViewById(R.id.five);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
    }

    @Override
    public void onClick(View view){

        int chunkNumbers = 0;

        switch (view.getId()) {
            case R.id.three:
                chunkNumbers = 9 ; 
                break;
            case R.id.four:
                chunkNumbers = 16 ;
                break;
            case R.id.five:
                chunkNumbers = 25 ;
        }

        //Getting the source image to split
        ImageView image = (ImageView) findViewById(R.id.source_image);

        //invoking method to split the source image
        splitImage(image, chunkNumbers);
    }

    private void splitImage(ImageView image, int chunkNumbers) { 

        //For the number of rows and columns of the grid to be displayed
        int rows,cols;

        //For height and width of the small image chunks 
        int chunkHeight,chunkWidth;

        //To store all the small image chunks in bitmap format in this list 
        ArrayList<bitmap> chunkedImages = new ArrayList<bitmap>(chunkNumbers);

        //Getting the scaled bitmap of the source image
        BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);

        rows = cols = (int) Math.sqrt(chunkNumbers);
        chunkHeight = bitmap.getHeight()/rows;
        chunkWidth = bitmap.getWidth()/cols;

        //xCoord and yCoord are the pixel positions of the image chunks
        int yCoord = 0;
        for(int x=0; x<rows; x++){
            int xCoord = 0;
            for(int y=0; y<cols; y++){
                chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
                xCoord += chunkWidth;
            }
            yCoord += chunkHeight;
        }

        /* Now the chunkedImages has all the small image chunks in the form of Bitmap class. 
         * You can do what ever you want with this chunkedImages as per your requirement.
         * I pass it to a new Activity to show all small chunks in a grid for demo.
         * You can get the source code of this activity from my Google Drive Account.
         */

        //Start a new activity to show these chunks into a grid 
        Intent intent = new Intent(ImageActivity.this, ChunkedImageActivity.class);
        intent.putParcelableArrayListExtra("image chunks", chunkedImages);
        startActivity(intent);
    }
}

http://androidattop.blogspot.com/2012/05/splitting-image-into-smaller-chunks-in.html

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29783308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档