【问题标题】:Android how to crop image use xml like cssAndroid如何裁剪图像使用像css这样的xml
【发布时间】:2012-06-23 13:22:57
【问题描述】:

正如你所看到的image,我们可以通过为图像设置负边距并将overflow:hidden 设置为其父元素来裁剪它。那么我该如何在android 中使用它呢? 我相信强大的 Bitmap 类可以裁剪它并创建一个新图像。有没有更简单的方法,比如做一些宽度的边距或布局?


这是我的解决方案: 我刚刚找到了一个更好的方法并解决了我的大部分问题:

imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);

可选代码(设置一点负边距(实际上是填充))

imageview.setPadding(-5, -10, -5, -10);

将调整图像大小以适应其父级的宽度或高度并自动使其居中

【问题讨论】:

    标签: android imageview margin crop


    【解决方案1】:

    您可以使用com.android.camera.action.CROP as 裁剪图像

    File tempFile = new File("mnt/sdcard/Images/beijingtesttemp.jpeg");//Image path which u want to crop
    Intent intent = new Intent("com.android.camera.action.CROP");  
    intent.setDataAndType(Uri.fromFile(tempFile), "image/*");
    intent.putExtra("crop", "true"); 
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("outputX", 96); 
    intent.putExtra("outputY", 96);
    intent.putExtra("output", Uri.fromFile(tempFile)); 
    intent.putExtra("outputFormat", "JPEG");
    startActivityForResult(intent,PHOTOR);  
    

    onActivityResult 中使用data.getExtras() 获取裁剪图像

     @Override 
         protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
                if (resultCode == NONE)  
                    return;  
                if (requestCode == PHOTOR) {  
                    Bundle extras = data.getExtras();  
                    if (extras != null) {  
                         // get cropped image as bitmap here
                        Bitmap photo = extras.getParcelable("data");  
    
                    }  
                }  
                super.onActivityResult(requestCode, resultCode, data);  
            }
    

    【讨论】:

    • 哦~~谢谢,我刚才找到了更好的方法:imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);将调整图像大小以适应其父级的宽度或高度并自动使其居中
    【解决方案2】:
    android:scaleType="centerCrop" 
    

    很棒,但它并没有真正裁剪图像。要正确裁剪它,您还应该使用:

    android:cropToPadding="true"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      • 2012-09-29
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      相关资源
      最近更新 更多