Android第三方开源图片裁剪截取:cropper



Android第三方开源图片裁剪截取:cropper

很多app都需要裁剪截取图片作为头像、logo之类,而cropper是github上的一个针对Android平台的、第三方开源图片裁剪截取项目,其项目主页是:https://github.com/edmodo/cropper

cropper项目给出的一个例子以一张蝴蝶图作为目标图片进行裁剪截取,如图:


cropper用法简单,给出一个例子,测试的MainActivity.java:

package zhangphil.demo;

import com.edmodo.cropper.CropImageView;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

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

		final CropImageView cropImageView = (CropImageView) findViewById(R.id.CropImageView);

		// 当触摸时候才显示网格线
		cropImageView.setGuidelines(CropImageView.GUIDELINES_ON_TOUCH);

		findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// 获取裁剪成的图片
				Bitmap croppedImage = cropImageView.getCroppedImage();

				cropImageView.setImageBitmap(croppedImage);
			}
		});
	}
}



需要的布局文件:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="zhangphil.demo.MainActivity" >

    <com.edmodo.cropper.CropImageView
        xmlns:custom="http://schemas.android.com/apk/res-auto"
        android:id="@+id/CropImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/girl" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="裁剪" >
    </Button>

</LinearLayout>


附录文章:
1,《Android设置头像,手机拍照或从本地相册选取图片作为头像》链接地址:http://blog.csdn.net/zhangphil/article/details/44829747

posted on 2016-06-12 10:35  疯子123  阅读(599)  评论(0编辑  收藏  举报

导航