Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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
Android 我想在中心裁剪后将图像缩放到底部_Android_Image - Fatal编程技术网

Android 我想在中心裁剪后将图像缩放到底部

Android 我想在中心裁剪后将图像缩放到底部,android,image,Android,Image,我使用图像视图显示客户的图片,并使用此命令在图像视图中缩放图像 iv.setScaleType(ImageView.ScaleType.CENTER_CROP); 但在执行此命令后,客户的头部已被部分切割,因此我希望在执行“中心裁剪”命令后将图像缩放为按钮,以便图像底部将被切割没有现成的解决方案满足您的要求。 您需要自己进行缩放。以下是您的操作方法: @Override protected void onCreate(Bundle savedInstanceState) {

我使用图像视图显示客户的图片,并使用此命令在图像视图中缩放图像

 iv.setScaleType(ImageView.ScaleType.CENTER_CROP);

但在执行此命令后,客户的头部已被部分切割,因此我希望在执行“中心裁剪”命令后将图像缩放为按钮,以便图像底部将被切割

没有现成的解决方案满足您的要求。 您需要自己进行缩放。以下是您的操作方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // we need to wait till the layout has been inflated to get the size of the ImageView
        final ImageView iv = (ImageView) findViewById(R.id.image);
        iv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
                public void onGlobalLayout() {
                    scaleBitmap(iv);
                }
            }
        );
    }

    private void scaleBitmap(ImageView iv) {
        // get ImageView and determine width & height
        int imageWidth = iv.getWidth();
        int imageHeight = iv.getHeight();
        Rect imageRect = new Rect(0, 0, imageWidth, imageHeight);

        // determine destination rectangle
        BitmapDrawable drawable = (BitmapDrawable)iv.getDrawable();
        Bitmap sourceBitmap = drawable.getBitmap();
        Rect bitmapRect = new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        // determine source rectangle
        Rect sourceRect = computeSourceRect(imageRect, bitmapRect);

        // here's where we do the magic
        Bitmap scaledBitmap = Bitmap.createBitmap(imageRect.width(), imageRect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(scaledBitmap);
        canvas.drawBitmap(sourceBitmap, sourceRect, imageRect, new Paint());
        iv.setImageBitmap(scaledBitmap);
    }

    private Rect computeSourceRect(Rect imageRect, Rect bitmapRect) {
        float imageWidth = (float) imageRect.width();
        float imageHeight = (float) imageRect.height();
        float bitmapWidth = (float) bitmapRect.width();
        float bitmapHeight = (float) bitmapRect.height();
        float aspectRatioImage = imageWidth / imageHeight;
        float aspectRatioBitmap = bitmapWidth / bitmapHeight;

        if (aspectRatioImage<aspectRatioBitmap) {
            float newWidth = bitmapHeight * aspectRatioImage;
            float widthOffset = (bitmapWidth - newWidth) / 2f;
            return new Rect((int) widthOffset, 0, (int) (widthOffset + newWidth), (int) bitmapHeight);
        }
        else {
            float newHeight = bitmapWidth / aspectRatioImage;
            float heightOffset = (bitmapHeight - newHeight) / 2f;
            // return this for center_crop
            //return new Rect(0, (int) heightOffset, (int) bitmapWidth, (int) (heightOffset + newHeight));
            // return this for bottom align
            return new Rect(0, 0, (int) bitmapWidth, (int) newHeight);
        }
    }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//我们需要等到布局膨胀后才能得到ImageView的大小
最终ImageView iv=(ImageView)findViewById(R.id.image);
iv.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener()){
@凌驾
公共图书馆{
scaleBitmap(iv);
}
}
);
}
私有void scaleBitmap(ImageView iv){
//获取ImageView并确定宽度和高度
int imageWidth=iv.getWidth();
int imageHeight=iv.getHeight();
Rect-imageRect=新的Rect(0,0,imageWidth,imageHeight);
//确定目标矩形
BitmapDrawable drawable=(BitmapDrawable)iv.getDrawable();
位图sourceBitmap=drawable.getBitmap();
Rect bitmapRect=new Rect(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
//确定源矩形
Rect sourceRect=computeSourceRect(imageRect,bitmapRect);
//这是我们施展魔法的地方
位图缩放位图=Bitmap.createBitmap(imageRect.width()、imageRect.height()、Bitmap.Config.ARGB_8888);
画布画布=新画布(缩放位图);
drawBitmap(sourceBitmap、sourceRect、imageRect、new Paint());
iv.设置图像位图(缩放位图);
}
私有Rect computeSourceRect(Rect imageRect,Rect bitmapRect){
float imageWidth=(float)imageRect.width();
float imageHeight=(float)imageRect.height();
float bitmapWidth=(float)bitmapRect.width();
float bitmapHeight=(float)bitmapRect.height();
float aspectRatioImage=图像宽度/图像高度;
float aspectRatioBitmap=位图宽度/位图高度;

如果(aspectRatioImage没有现成的解决方案满足您的需求。 您需要自己进行缩放。以下是您的操作方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // we need to wait till the layout has been inflated to get the size of the ImageView
        final ImageView iv = (ImageView) findViewById(R.id.image);
        iv.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
                public void onGlobalLayout() {
                    scaleBitmap(iv);
                }
            }
        );
    }

    private void scaleBitmap(ImageView iv) {
        // get ImageView and determine width & height
        int imageWidth = iv.getWidth();
        int imageHeight = iv.getHeight();
        Rect imageRect = new Rect(0, 0, imageWidth, imageHeight);

        // determine destination rectangle
        BitmapDrawable drawable = (BitmapDrawable)iv.getDrawable();
        Bitmap sourceBitmap = drawable.getBitmap();
        Rect bitmapRect = new Rect(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

        // determine source rectangle
        Rect sourceRect = computeSourceRect(imageRect, bitmapRect);

        // here's where we do the magic
        Bitmap scaledBitmap = Bitmap.createBitmap(imageRect.width(), imageRect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(scaledBitmap);
        canvas.drawBitmap(sourceBitmap, sourceRect, imageRect, new Paint());
        iv.setImageBitmap(scaledBitmap);
    }

    private Rect computeSourceRect(Rect imageRect, Rect bitmapRect) {
        float imageWidth = (float) imageRect.width();
        float imageHeight = (float) imageRect.height();
        float bitmapWidth = (float) bitmapRect.width();
        float bitmapHeight = (float) bitmapRect.height();
        float aspectRatioImage = imageWidth / imageHeight;
        float aspectRatioBitmap = bitmapWidth / bitmapHeight;

        if (aspectRatioImage<aspectRatioBitmap) {
            float newWidth = bitmapHeight * aspectRatioImage;
            float widthOffset = (bitmapWidth - newWidth) / 2f;
            return new Rect((int) widthOffset, 0, (int) (widthOffset + newWidth), (int) bitmapHeight);
        }
        else {
            float newHeight = bitmapWidth / aspectRatioImage;
            float heightOffset = (bitmapHeight - newHeight) / 2f;
            // return this for center_crop
            //return new Rect(0, (int) heightOffset, (int) bitmapWidth, (int) (heightOffset + newHeight));
            // return this for bottom align
            return new Rect(0, 0, (int) bitmapWidth, (int) newHeight);
        }
    }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//我们需要等到布局膨胀后才能得到ImageView的大小
最终ImageView iv=(ImageView)findViewById(R.id.image);
iv.getViewTreeObserver().addOnGlobalLayoutListener(新OnGlobalLayoutListener()){
@凌驾
公共图书馆{
scaleBitmap(iv);
}
}
);
}
私有void scaleBitmap(ImageView iv){
//获取ImageView并确定宽度和高度
int imageWidth=iv.getWidth();
int imageHeight=iv.getHeight();
Rect-imageRect=新的Rect(0,0,imageWidth,imageHeight);
//确定目标矩形
BitmapDrawable drawable=(BitmapDrawable)iv.getDrawable();
位图sourceBitmap=drawable.getBitmap();
Rect bitmapRect=new Rect(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
//确定源矩形
Rect sourceRect=computeSourceRect(imageRect,bitmapRect);
//这是我们施展魔法的地方
位图缩放位图=Bitmap.createBitmap(imageRect.width()、imageRect.height()、Bitmap.Config.ARGB_8888);
画布画布=新画布(缩放位图);
drawBitmap(sourceBitmap、sourceRect、imageRect、new Paint());
iv.设置图像位图(缩放位图);
}
私有Rect computeSourceRect(Rect imageRect,Rect bitmapRect){
float imageWidth=(float)imageRect.width();
float imageHeight=(float)imageRect.height();
float bitmapWidth=(float)bitmapRect.width();
float bitmapHeight=(float)bitmapRect.height();
float aspectRatioImage=图像宽度/图像高度;
float aspectRatioBitmap=位图宽度/位图高度;
如果(aspectRatioImage