Android-根据目的宽高和原宽高进行图片裁剪的代码

根据目的宽高和原始宽高,并比较一一比较它们,最后根据目的宽高和原始宽高的大小确定裁剪方案:

 1:

宽高都不大于目的宽高(保持图片原宽高,在目的宽高的画布上剧中画上原图片)

2:

/**宽不大于目的宽,高大于目的高(以高的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)snapshot*/
float scale = (float)widHig[1] / (float)bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

3:

/**宽大于目的宽,高不大于目的高(以宽的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)123 marry*/
float scale = (float)widHig[0] / (float)bitmap.getWidth();
Matrix matrix = new Matrix();
matrix.setScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

4: /**宽大于目的宽,高大于目的高*/

    4.1:

      /**宽的差值大于高(以宽的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)1111111*/
      float scale = (float)widHig[0] / (float)bitmap.getWidth();
      Matrix matrix = new Matrix();
      matrix.setScale(scale, scale);
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

    4.2

      /**高的差值大于宽(以高的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)wolf*/
      float scale = (float)widHig[1] / (float)bitmap.getHeight();
      Matrix matrix = new Matrix();
      matrix.setScale(scale, scale);
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

 

代码如下:

public class MainActivity extends AppCompatActivity
{
    private AppCompatImageView imageView;
    private int[] widHig = new int[]{1080, 1920};
    private String srcPath = "/sdcard/DCIM/Camera/marry.jpg";
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = findViewById(R.id.imageView);
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath);
        if(bitmap.getWidth() <= widHig[0])
        {
            if(bitmap.getHeight() <= widHig[1])
            {
                /**宽高都不大于目的宽高(保持图片原宽高,在目的宽高的画布上剧中画上原图片)vsim*/
            }
            else
            {
                /**宽不大于目的宽,高大于目的高(以高的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)snapshot*/
                float scale = (float)widHig[1] / (float)bitmap.getHeight();
                Matrix matrix = new Matrix();
                matrix.setScale(scale, scale);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            }
        }
        else
        {
            if(bitmap.getHeight() <= widHig[1])
            {
                /**宽大于目的宽,高不大于目的高(以宽的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)123 marry*/
                float scale = (float)widHig[0] / (float)bitmap.getWidth();
                Matrix matrix = new Matrix();
                matrix.setScale(scale, scale);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            }
            else
            {
                /**宽大于目的宽,高大于目的高*/
                int surplusWid = bitmap.getWidth() - widHig[0];
                int surplusHig = bitmap.getHeight() - widHig[1];
                if(surplusWid >= surplusHig)
                {
                    /**宽的差值大于高(以宽的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)1111111*/
                    float scale = (float)widHig[0] / (float)bitmap.getWidth();
                    Matrix matrix = new Matrix();
                    matrix.setScale(scale, scale);
                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                }
                else;
                {
                    /**高的差值大于宽(以高的比例为标准缩放图片,然后在目的宽高的画布上剧中画上图片)wolf*/
                    float scale = (float)widHig[1] / (float)bitmap.getHeight();
                    Matrix matrix = new Matrix();
                    matrix.setScale(scale, scale);
                    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

                }
            }
        }
        /**ARGB_8888:四个通道都是8位,每个像素占用4个字节,图片质量是最高的,但是占用的内存也是最大的;
         *
         *
         ARGB_4444:四个通道都是4位,每个像素占用2个字节,图片的失真比较严重;
         RGB_565:没有A通道,每个像素占用2个字节,图片失真小,但是没有透明度;
         ALPHA_8:只有A通道,每个像素占用1个字节大大小,只有透明度,没有颜色值。
         使用场景总结:ARGB_4444失真严重,基本不用;ALPHA_8使用场景特殊,比如设置遮盖效果等;不需要设置透明度,RGB_565是个不错的选择;既要设置透明度,对图片质量要求又高,就用ARGB_8888。*/
        Bitmap baseBitmap = Bitmap.createBitmap(widHig[0], widHig[1], Bitmap.Config.RGB_565);
        baseBitmap.eraseColor(Color.rgb(72, 98, 124));
        int left = (widHig[0] - bitmap.getWidth()) / 2;
        int top = (widHig[1] - bitmap.getHeight()) / 2;
        Canvas canvas = new Canvas(baseBitmap);
        canvas.drawBitmap(bitmap, left, top, new Paint());
        canvas.save();
        canvas.restore();
        imageView.setImageBitmap(baseBitmap);
    }
}

 

你可能感兴趣的:(android)