安卓:从位图作物的位图,并设置为ImageView的SRC [英] Android: Crop Bitmap from Bitmap and set as ImageView src

查看:320
本文介绍了安卓:从位图作物的位图,并设置为ImageView的SRC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个spritesheet裁剪精灵,但是裁剪后的图像是不是我指定的图像的一部分。

的spritesheet包含一个大小为32×32像素,因此在第一子画面子画面应在像素0到32(水平方向)和0至32(垂直地),假定最左上角像素为[0,0]

 位图spritesheet = BitmapFactory.de codeResource(getResources(),R.drawable.spritesheet_32x32);
位图精灵= Bitmap.createBitmap(spritesheet,0,0,32,32);

该ImageView的只是没有一个src一个普通的ImageView,宽度或高度设置。

  ImageView的观点=(ImageView的)findViewById(R.id.player1);
view.setImageBitmap(精灵);

但所显示的图像只是第一个精灵的要小得多。
我怎么监督?为 view.setImageBitmap(精灵); 这个正确的方法?是否ImageView的设置位之前需要一个修复的宽度和高度?

编辑:也许我应该对我的布局更加清晰。有布置在的LinearLayout 2 ImageViews。这里的XML:

 <的LinearLayout
        机器人:方向=横向
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:baselineAligned =假>
    < ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:将scaleX = - 1
        机器人:ID =@ + ID / PLAYER1
        机器人:layout_weight =1
        机器人:layout_gravity =center_vertical/>
    < ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:ID =@ + ID / player2
        机器人:layout_weight =1
        机器人:layout_gravity =center_vertical/>
< / LinearLayout中>


解决方案

 进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;公共类ImageResizer {公共静态位图德codeSampledBitmapFromFile(字符串的文件名,
    INT reqWidth,诠释reqHeight){
//首先去code。与inJustDe codeBounds = true来检查尺寸
最后BitmapFactory.Options
        选项​​=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(文件名,期权);
//计算inSampleSize
options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);
//德code位与inSampleSize集
options.inJustDe codeBounds = FALSE;
返回BitmapFactory.de codeFILE(文件名,期权);
}公共静态INT calculateInSampleSize(BitmapFactory.Options选项,
    INT reqWidth,诠释reqHeight){
// BEGIN_INCLUDE(calculate_sample_size)
//原始高度和图像宽度
最终诠释身高= options.outHeight;
最终诠释宽度= options.outWidth;
INT inSampleSize = 1;如果(高度> reqHeight ||宽度GT; reqWidth){    最终诠释halfHeight =身高/ 2;
    最终诠释半宽度=宽度/ 2;    //计算最大inSampleSize值是2的幂,并保持这两个
    //高度和宽度大于所请求的高度和宽度。
    而((halfHeight / inSampleSize)GT; reqHeight
            &功放;&安培; (半角/ inSampleSize)GT; reqWidth){
        inSampleSize * = 2;
    }    //这提供的情况下,图像有一个奇怪的一些额外的逻辑
    //宽高比。例如,全景可以具有大得多的
    //宽度大于高度。在这种情况下,总像素仍可能
    //最终会被过大,以适应在舒适的记忆,所以我们应该
    //与样本缩小图像(=较大inSampleSize)更具侵略性。    长totalPixels =宽*高/ inSampleSize;    //任何超过2倍的像素要求,我们将进一步下降采样
    最终长totalReqPixelsCap = reqWidth * reqHeight * 2;    而(totalPixels> totalReqPixelsCap){
        inSampleSize * = 2;
        totalPixels / = 2;
    }
}
返回inSampleSize;
// END_INCLUDE(calculate_sample_size)
 }}

调用方法

  BMP位图= ImageResizer.de codeSampledBitmapFromFile(新文件(文件路径).getAbsolutePath(),512,342);

我还没有试过,但如下可能工作传似结果的方法参数
            imageView.getWidth(),imageView.getHeight()

I'm trying to crop a sprite from a spritesheet, however the cropped image is not the part of the image I specified.

The spritesheet contains sprites with a size of 32 x 32 px and therefore the first sprite should be at pixels 0 to 32 (horizontally) and 0 to 32 (vertically), supposed the most upper left pixel is [0, 0].

Bitmap spritesheet = BitmapFactory.decodeResource(getResources(), R.drawable.spritesheet_32x32);
Bitmap sprite = Bitmap.createBitmap(spritesheet, 0, 0, 32, 32);

The ImageView is just a plain ImageView without a src, width or height set.

ImageView view = (ImageView)findViewById(R.id.player1);
view.setImageBitmap(sprite);

But the displayed image is just a much smaller part of the first sprite. What do I oversee? Is view.setImageBitmap(sprite); the right method for this? Does the ImageView need a fix width and height before setting the bitmap?

EDIT: Maybe I should get clearer about my layout. There are two ImageViews arranged on a LinearLayout. Here's the XML:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:baselineAligned="false">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleX="-1"
        android:id="@+id/player1"
        android:layout_weight="1"
        android:layout_gravity="center_vertical" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/player2"
        android:layout_weight="1"
        android:layout_gravity="center_vertical" />
</LinearLayout>

解决方案

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageResizer {

public static Bitmap decodeSampledBitmapFromFile(String filename,
    int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options
        options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filename, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filename, options);
}

public static int calculateInSampleSize(BitmapFactory.Options options,
    int reqWidth, int reqHeight) {
// BEGIN_INCLUDE (calculate_sample_size)
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    final int halfHeight = height / 2;
    final int halfWidth = width / 2;

    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
    while ((halfHeight / inSampleSize) > reqHeight
            && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;
    }

    // This offers some additional logic in case the image has a strange
    // aspect ratio. For example, a panorama may have a much larger
    // width than height. In these cases the total pixels might still
    // end up being too large to fit comfortably in memory, so we should
    // be more aggressive with sample down the image (=larger inSampleSize).

    long totalPixels = width * height / inSampleSize;

    // Anything more than 2x the requested pixels we'll sample down further
    final long totalReqPixelsCap = reqWidth * reqHeight * 2;

    while (totalPixels > totalReqPixelsCap) {
        inSampleSize *= 2;
        totalPixels /= 2;
    }
}
return inSampleSize;
// END_INCLUDE (calculate_sample_size)
 }

}

Call the method

Bitmap bmp = ImageResizer.decodeSampledBitmapFromFile(new  File(filePath).getAbsolutePath(), 512, 342);               

I haven't tried but the follows might work pass the method parameter like
imageView.getWidth() , imageView.getHeight()

这篇关于安卓:从位图作物的位图,并设置为ImageView的SRC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆