用开源项目cropper实现对图片中任意部分进行裁剪
红色区域为截图控件的区域。 
开源项目地址: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.CropImageView
xmlns: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"/> <ImageView
android: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" /> <Button
android: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; @Override
protected 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() { @Override
public 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实现对图片中任意部分进行裁剪的更多相关文章
- Android 开源项目
StickerCamera 一个完整的开源项目.贴纸标签相机(类似nice,in),拍照,裁剪,贴贴纸打标签功能. MD-BiliBili 基于 Material Design 的 BiliBili ...
- Android 开源项目分类汇总(转)
Android 开源项目分类汇总(转) ## 第一部分 个性化控件(View)主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Galler ...
- Android 开源项目分类汇总
Android 开源项目分类汇总 Android 开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView ...
- Android常用酷炫控件(开源项目)github地址汇总
转载一个很牛逼的控件收集帖... 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.Gri ...
- GitHub上优秀的开源项目(转载)
转载出处:https://github.com/Trinea/android-open-project 第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.A ...
- Android 常用炫酷控件(开源项目)git地址汇总
第一部分 个性化控件(View) 主要介绍那些不错个性化的 View,包括 ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.P ...
- Android开源项目分类汇总
目前包括: Android开源项目第一篇——个性化控件(View)篇 包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...
- Android开源项目(二)
第二部分 工具库 主要包括那些不错的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多媒体相关及其他. 一.依赖注入DI 通过 ...
- GitHub上史上最全的Android开源项目分类汇总 (转)
GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...
随机推荐
- JS两种事件的触发方式
一.入侵式触发方式 <input type="button" id="one" onclick="事件" /> 二.非入侵式触发 ...
- 事务ACID特性
所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.例如,银行转帐工作:从一个帐号扣款并使另一个帐号增款,这两个操作要么都执行,要么都不执行. 数据库事务必须具备 ...
- word2vec 中的数学原理三 背景知识 语言模型
主要参考: word2vec 中的数学原理详解 自己动手写 word2vec
- ASP.NET OAuth、jwt、OpenID Connect
ASP.NET OAuth.jwt.OpenID Connect 上一篇文章介绍了OAuth2.0以及如何使用.Net来实现基于OAuth的身份验证,本文是对上一篇文章的补充,主要是介绍OAuth与J ...
- Axios 是一个基于 promise 的 HTTP 库
Axios 是一个基于 promise 的 HTTP 库 vue项目中关于axios的简单使用 axios介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.j ...
- 加载JS代码
玩转JS系列之代码加载篇 一开始我们这样写js <script type="text/javascript"> function a(){ console.log( ...
- js跨越请求的2中实现 JSONP /后端接口设置运行跨越header
由于浏览器同源策略,a域名的js向b域名ajax请求会被禁止.JS实现跨越访问接口有2中办法. 1.后端接口设置允许跨越的header头. //header('Access-Control-Allow ...
- CSS------如何让ul中的li分为两列甚至多列
转载: http://blog.sina.com.cn/s/blog_7f13f92a0100rkfg.html 只需要复制ul和li中的style样式即可 如果需要自定义多少列,只需要修改li中的w ...
- 023 Hadoop的生态系统
1.数据来源 RDBM:sqoop 日志文件:flume 2.zookeeper 多台机器保持同步数据. 3.hive sql语句的查询 HQL转换成mapreduce SQL On Hadoop 4 ...
- ApiPost自动化测试基础之:流程测试
我们在<ApiPost自动化测试基础之:接口参数依赖的情景处理>和<ApiPost自动化测试基础之:如何使用测试校验(测试用例)?>分别讲解了ApiPost自动化测试的基础知识 ...