红色区域为截图控件的区域。   

  

  开源项目地址:https://github.com/edmodo/cropper

  croper这个开源项目可以对一个图片进行任意区域的街区,并且可以设置图片的旋转角度。但它忽视了小图片的存在,如果要截图的图片过于小,那么显示效果是极其不好的。于是我写了个图片拉伸的方法来解决这个问题,当然也可以再它的源码中进行修改。我尝试过在源码中修改,但发现它多方法都是针对的大图来做的,修改起来十分困难。所以姑且偷懒先把小图片拉伸后在进行显示。

使用的步骤很简单:

1.在布局文件定义这个控件

2.在代码中找到他,并进行下设置(可选)

3.使用截图的方法

布局文件(里面的imageview用于显示截取好的图片,button是用来截图的按钮)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="${relativePackage}.${activityClass}" ><com.edmodo.cropper.CropImageViewxmlns:custom="http://schemas.android.com/apk/res-auto"android:id="@+id/CropImageView"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@+id/imageView"android:layout_centerHorizontal="true"android:background="#ff0000"/><ImageViewandroid:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/button1"android:layout_centerHorizontal="true"android:src="@drawable/abc_ab_bottom_solid_dark_holo" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:text="裁剪" /></RelativeLayout>

MainActivity

package com.kale.croppertest;import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;import com.edmodo.cropper.CropImageView;public class MainActivity extends Activity {CropImageView cImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);cImageView = (CropImageView) findViewById(R.id.CropImageView);cImageView.setImageBitmap(getBitmap(R.drawable.right));//为了兼容小图片,必须在代码中加载图片cImageView.rotateImage(30);//设定图片的旋转角度cImageView.setFixedAspectRatio(true);//设置允许按比例截图,如果不设置就是默认的任意大小截图cImageView.setAspectRatio(1, 1);//设置比例为一比一cImageView.setGuidelines(CropImageView.ON);//设置显示网格的时机,默认为on_touch
        Button btn = (Button)findViewById(R.id.button1);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO 自动生成的方法存根Bitmap bitmap = cImageView.getCroppedImage();//得到裁剪好的图片ImageView croppedImageView = (ImageView) findViewById(R.id.imageView);croppedImageView.setImageBitmap(bitmap);//设置到imageview中
            }});}/*** @param resId* @return 如果图片太小,那么就拉伸*/public Bitmap getBitmap(int resId) {WindowManager wm = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);int width = wm.getDefaultDisplay().getWidth();Bitmap bitmap=BitmapFactory.decodeResource(getResources(), resId);float scaleWidth = 1,scaleHeight = 1;if(bitmap.getWidth() < width) {scaleWidth = width / bitmap.getWidth();scaleHeight = scaleWidth;}Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),bitmap.getHeight(), matrix, true);return bitmap;}}

源码下载:http://download.csdn.net/detail/shark0017/7732283

用开源项目cropper实现对图片中任意部分进行裁剪相关推荐

  1. github开源项目大集合(1)

    github开源项目大集合 目前包括: Android 开源项目第一篇--个性化控件(View)篇  包括ListView.ActionBar.Menu.ViewPager.Gallery.GridV ...

  2. Android 开源项目分类汇总 APP功能汇总

    Android 开源项目第一篇--个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Progr ...

  3. Android github上优秀开源项目分类汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

  4. Android 开源项目分类汇总(很全)

    目录 前言 目前包括: 第一部分 个性化控件(View) 一.ListView 二.ActionBar 三.Menu 四.ViewPager .Gallery 五.GridView 六.ImageVi ...

  5. Android系统(168)---Android 开源项目分类汇总

    Android开源项目分类汇总 原网址:https://blog.csdn.net/prince77qiqiqq/article/details/51338330 Android 开源项目第一篇--个 ...

  6. github上Android开源项目汇总(带效果图)

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

  7. [转]Android 开源项目分类汇总

    Android 开源项目分类汇总,更全更新可见 codekk.com Other: English Version, 繁體版, Website Version. 我们的微信公众号:codekk.二维码 ...

  8. github上的优秀android开源项目 大全 真是太他妈的全了!!!!!!

    酷炫不需要理由 http://blog.csdn.net/a774057695/article/details/49889437 https://github.com/XinRan5312/andro ...

  9. [转]Android github上优秀开源项目分类汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...

最新文章

  1. 我的商汤实习年末总结
  2. javascript内置顶层函数
  3. 国外开源网站网页翻译
  4. Intel Realsense D435 pyrealsense2 get_option_range() 获取rs.option中参数值取值范围 获取默认值
  5. 百度牵头,全球首个面向商业化运营的Robotaxi技术标准正式发布
  6. 在html中 标题字体标记的最大取值,西南大学网络教育2020年春0834]《网页设计》作业标准答案...
  7. 竞赛准备篇---(一)抽签问题
  8. python语言及其应用电子版翁正秋_Python语言及其应用pdf
  9. php生成图形验证码的几种方法
  10. 福特dms系统服务器,长安福特DMS使用规范.pdf
  11. 动态苹果屏保壁纸软件Brooklyn Mac免费版
  12. 使用 VBRichClient 库
  13. 520C语言表白神器
  14. 高淇python400集课堂笔记_魔力耳朵重磅推出1对1课堂“全班型”满足更多中国家庭教育需求...
  15. (1.4.10)SXF笔试题汇总
  16. Shawn,别让我们失望
  17. Windows使用批处理文件更改指定目录下的文件名(含序号)
  18. python-pcl简易文档(不包含自建函数与pcl_grabber包)
  19. CentOS 7 下的软件安装方法及策略
  20. 《从一到无穷大:科学中的事实和臆测》小记

热门文章

  1. 文思海辉口碑很差_文思海辉·金融打造全自动、100%话务覆盖的智能质检系统
  2. suse docker 安装mysql_Docker(三):Docker安装MySQL
  3. (五)操作系统安全概念和设计思想
  4. 【项目管理】采购管理
  5. 全职宝妈跨专业备考信息系统项目管理师【52,52,51】
  6. kettle中job给转换配置相对路径
  7. UltraVNC反向连接方式的使用
  8. ElementUI中el-upload中怎样限制上传文件的格式
  9. PowerDesigner逆向工程从SqlServer数据库生成PDM(图文教程)
  10. VS中的C#项目怎样引入另一个项目