Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 点击按钮将图像分成4等份_Java_Android - Fatal编程技术网

Java 点击按钮将图像分成4等份

Java 点击按钮将图像分成4等份,java,android,Java,Android,如何在用户单击按钮时将单个图像分成4个相等的部分。您正在查找 您只需将整个位图的宽度和高度除以块数,然后循环遍历每个块。您正在寻找的 您只需将整个位图的宽度和高度除以块数,然后循环遍历每个块。您可以使用下面的方法裁剪图像的一部分,其中图像是源图像,r是要裁剪的矩形部分。您可以使用以下矩形执行4次: x:0,y:0,宽度=image.width/2,高度=image.height/2 x:image.Width/2,y:0,Width=image.Width/2,height=image.heig

如何在用户单击按钮时将单个图像分成4个相等的部分。

您正在查找 您只需将整个位图的宽度和高度除以块数,然后循环遍历每个块。

您正在寻找的
您只需将整个位图的宽度和高度除以块数,然后循环遍历每个块。

您可以使用下面的方法裁剪图像的一部分,其中图像是源图像,r是要裁剪的矩形部分。您可以使用以下矩形执行4次:

  • x:0,y:0,宽度=image.width/2,高度=image.height/2
  • x:image.Width/2,y:0,Width=image.Width/2,height=image.height/2
  • x:0,y:image.Height/2,width=image.width/2,Height=image.Height/2
  • x:image.Width/2,y:image.Height/2,Width=image.Width/2,Height=image.Height/2
  • 请注意,下面的方法是在C#中实现的,使用Mono for Android,但在Java中应该几乎相同

    private Android.Graphics.Bitmap Crop(Android.Graphics.Bitmap image, Rectangle r)
    {
      return Android.Graphics.Bitmap.CreateBitmap(image, r.X, r.Y, r.Width, r.Height);
    }
    

    您可以使用下面的方法裁剪图像的一部分,其中image是源图像,r是要裁剪的矩形部分。您可以使用以下矩形执行4次:

  • x:0,y:0,宽度=image.width/2,高度=image.height/2
  • x:image.Width/2,y:0,Width=image.Width/2,height=image.height/2
  • x:0,y:image.Height/2,width=image.width/2,Height=image.Height/2
  • x:image.Width/2,y:image.Height/2,Width=image.Width/2,Height=image.Height/2
  • 请注意,下面的方法是在C#中实现的,使用Mono for Android,但在Java中应该几乎相同

    private Android.Graphics.Bitmap Crop(Android.Graphics.Bitmap image, Rectangle r)
    {
      return Android.Graphics.Bitmap.CreateBitmap(image, r.X, r.Y, r.Width, r.Height);
    }
    

    使用此方法分割图像

    public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)
    

    使用此方法分割图像

    public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height)
    

    我发现下面的代码非常有效。它将图像分为9个部分。通过使用它,您还可以将图像分为4个部分

    public Bitmap[] splitBitmap(Bitmap picture)
    {
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(picture, 240, 240, true);
    Bitmap[] imgs = new Bitmap[9];
    imgs[0] = Bitmap.createBitmap(scaledBitmap, 0, 0, 80 , 80);
    imgs[1] = Bitmap.createBitmap(scaledBitmap, 80, 0, 80, 80);
    imgs[2] = Bitmap.createBitmap(scaledBitmap,160, 0, 80,80);
    imgs[3] = Bitmap.createBitmap(scaledBitmap, 0, 80, 80, 80);
    imgs[4] = Bitmap.createBitmap(scaledBitmap, 80, 80, 80,80);
    imgs[5] = Bitmap.createBitmap(scaledBitmap, 160, 80,80,80);
    imgs[6] = Bitmap.createBitmap(scaledBitmap, 0, 160, 80,80);
    imgs[7] = Bitmap.createBitmap(scaledBitmap, 80, 160,80,80);
    imgs[8] = Bitmap.createBitmap(scaledBitmap, 160,160,80,80);
    return imgs;
    
    
    }
    
    该函数将原始位图作为参数,然后使用bitmap.createScaledBitmap(picture,240240,true);我创建了一个大小为240 x 240的缩放图像以将图像分割为相等的部分,我创建了一个3 x 3的网格,其中每个图像的大小为80 x 80。这可以根据您的需要进行更改,但宽度应保持在240,因为所有普通android手机屏幕的宽度都是240


    所有位图都存储在位图数组中,最后函数将数组返回给调用函数。

    我发现下面的代码非常有效。它将图像分为9个部分。通过使用它,您还可以将图像分为4个部分

    public Bitmap[] splitBitmap(Bitmap picture)
    {
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(picture, 240, 240, true);
    Bitmap[] imgs = new Bitmap[9];
    imgs[0] = Bitmap.createBitmap(scaledBitmap, 0, 0, 80 , 80);
    imgs[1] = Bitmap.createBitmap(scaledBitmap, 80, 0, 80, 80);
    imgs[2] = Bitmap.createBitmap(scaledBitmap,160, 0, 80,80);
    imgs[3] = Bitmap.createBitmap(scaledBitmap, 0, 80, 80, 80);
    imgs[4] = Bitmap.createBitmap(scaledBitmap, 80, 80, 80,80);
    imgs[5] = Bitmap.createBitmap(scaledBitmap, 160, 80,80,80);
    imgs[6] = Bitmap.createBitmap(scaledBitmap, 0, 160, 80,80);
    imgs[7] = Bitmap.createBitmap(scaledBitmap, 80, 160,80,80);
    imgs[8] = Bitmap.createBitmap(scaledBitmap, 160,160,80,80);
    return imgs;
    
    
    }
    
    该函数将原始位图作为参数,然后使用bitmap.createScaledBitmap(picture,240240,true);我创建了一个大小为240 x 240的缩放图像以将图像分割为相等的部分,我创建了一个3 x 3的网格,其中每个图像的大小为80 x 80。这可以根据您的需要进行更改,但宽度应保持在240,因为所有普通android手机屏幕的宽度都是240


    所有位图都存储在位图数组中,最后函数将数组返回给调用函数。

    这是将位图分成4等份的方法。 只需将位图作为参数传递给此方法

     public Bitmap[] splitBitmap(Bitmap picture)
    {
    
    Bitmap[] imgs = new Bitmap[4];
     imgs[0] = Bitmap.createBitmap(picture, 0, 0, picture.getWidth()/2 , picture.getHeight()/2);
     imgs[1] = Bitmap.createBitmap(picture, picture.getWidth()/2, 0, picture.getWidth()/2, picture.getHeight()/2);
     imgs[2] = Bitmap.createBitmap(picture,0, picture.getHeight()/2, picture.getWidth()/2,picture.getHeight()/2);
     imgs[3] = Bitmap.createBitmap(picture, picture.getWidth()/2, picture.getHeight()/2, picture.getWidth()/2, picture.getHeight()/2);
    
    return imgs;
    
    
    }
    

    这是将位图分成4个相等部分的方法。 只需将位图作为参数传递给此方法

     public Bitmap[] splitBitmap(Bitmap picture)
    {
    
    Bitmap[] imgs = new Bitmap[4];
     imgs[0] = Bitmap.createBitmap(picture, 0, 0, picture.getWidth()/2 , picture.getHeight()/2);
     imgs[1] = Bitmap.createBitmap(picture, picture.getWidth()/2, 0, picture.getWidth()/2, picture.getHeight()/2);
     imgs[2] = Bitmap.createBitmap(picture,0, picture.getHeight()/2, picture.getWidth()/2,picture.getHeight()/2);
     imgs[3] = Bitmap.createBitmap(picture, picture.getWidth()/2, picture.getHeight()/2, picture.getWidth()/2, picture.getHeight()/2);
    
    return imgs;
    
    
    }