Skip to content

Commit

Permalink
Fix crop size bug, update sample code, update gradle, add screenshots.
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyOrr committed Apr 29, 2016
1 parent 18a1282 commit aca0d3a
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 156 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.android.tools.build:gradle:2.1.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -14,7 +14,7 @@ buildscript {

allprojects {
group 'com.github.crazyorr'
version '0.1.0'
version '0.1.1'
repositories {
jcenter()
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 10 15:27:10 PDT 2013
#Wed Apr 27 10:50:19 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
10 changes: 5 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ apply plugin: 'signing'
archivesBaseName = 'zoom-crop-image'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
minSdkVersion 14
targetSdkVersion 22
versionCode 1
targetSdkVersion 23
versionCode 2
versionName version
}

Expand All @@ -35,7 +35,7 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:appcompat-v7:23.3.0'
}

task androidJavadocs(type: Javadoc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,81 +93,6 @@ public CropImageLayout(Context context, AttributeSet attrs, int defStyleAttr) {
this.addView(mCropImageBorderView, lp);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
initCropSize();
}

/**
* 裁切图片
*
* @return
*/
public Bitmap crop() {
return mZoomCropImageView.crop(mOutputWidth, mOutputHeight);
}

/**
* 必须提供Uri,否则无法获取到旋转信息
* @param uri
*/
public void setImageURI(Uri uri){
String path = FileUtils.getPath(getContext(), uri);

WindowManager windowManager = (WindowManager)getContext()
.getSystemService(Context.WINDOW_SERVICE);
Display windowDisplay = windowManager.getDefaultDisplay();
Point size = new Point();
windowDisplay.getSize(size);
Log.i(TAG, "Screen Width = " + size.x);
Log.i(TAG, "Screen Height = " + size.y);

int degrees = readImageRotationDegree(path);
Bitmap rotatedBitmap = rotaingBitmap(degrees,
sampleBitmap(getContext(), uri, size.x, size.y));
mZoomCropImageView.setImageBitmap(rotatedBitmap);
}

/**
* 设置输出图片尺寸
* @param outputWidth
* @param outputHeight
*/
public void setOutputSize(int outputWidth, int outputHeight){
mOutputWidth = outputWidth;
mOutputHeight = outputHeight;
initCropSize();
}

/**
* 设置切割形状
* @param cropShape
*/
public void setCropShape(int cropShape){
mCropImageBorderView.setCropShape(cropShape);
mZoomCropImageView.setCropShape(cropShape);
}

private void initCropSize(){
if(mWidth != 0 && mHeight != 0
&& mOutputWidth != 0 && mOutputHeight != 0){
final double CROP_SIZE_RATIO = 0.9;
double scaleWidth = Math.floor(mWidth * CROP_SIZE_RATIO / mOutputWidth);
double scaleHeight = Math.floor(mHeight * CROP_SIZE_RATIO / mOutputHeight);
//放大倍数取较小值
double scale = Math.min(scaleWidth, scaleHeight);

int mCropWidth = (int)(mOutputWidth * scale);
int mCropHeight = (int)(mOutputHeight * scale);

mZoomCropImageView.setCropSize(mCropWidth, mCropHeight);
mCropImageBorderView.setCropSize(mCropWidth, mCropHeight);
}
}

/**
* 读取图片属性:旋转的角度
* @param path 图片绝对路径
Expand Down Expand Up @@ -196,7 +121,7 @@ public static int readImageRotationDegree(String path) {
}
return degree;
}

/**
* 旋转图片
* @param degrees
Expand All @@ -205,14 +130,14 @@ public static int readImageRotationDegree(String path) {
*/
public static Bitmap rotaingBitmap(int degrees , Bitmap bitmap) {
// 旋转图片 动作
Matrix matrix = new Matrix();;
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}

/**
* 压缩图片尺寸
* @return
Expand Down Expand Up @@ -267,4 +192,79 @@ public static Bitmap sampleBitmap(Context context, Uri uri,
}
return bitmap;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = w;
mHeight = h;
initCropSize();
}

/**
* 裁切图片
*
* @return
*/
public Bitmap crop() {
return mZoomCropImageView.crop(mOutputWidth, mOutputHeight);
}

/**
* 必须提供Uri,否则无法获取到旋转信息
* @param uri
*/
public void setImageURI(Uri uri){
String path = FileUtils.getPath(getContext(), uri);

WindowManager windowManager = (WindowManager)getContext()
.getSystemService(Context.WINDOW_SERVICE);
Display windowDisplay = windowManager.getDefaultDisplay();
Point size = new Point();
windowDisplay.getSize(size);
Log.i(TAG, "Screen Width = " + size.x);
Log.i(TAG, "Screen Height = " + size.y);

int degrees = readImageRotationDegree(path);
Bitmap rotatedBitmap = rotaingBitmap(degrees,
sampleBitmap(getContext(), uri, size.x, size.y));
mZoomCropImageView.setImageBitmap(rotatedBitmap);
}

/**
* 设置输出图片尺寸
* @param outputWidth
* @param outputHeight
*/
public void setOutputSize(int outputWidth, int outputHeight){
mOutputWidth = outputWidth;
mOutputHeight = outputHeight;
initCropSize();
}

/**
* 设置切割形状
* @param cropShape
*/
public void setCropShape(int cropShape){
mCropImageBorderView.setCropShape(cropShape);
mZoomCropImageView.setCropShape(cropShape);
}

private void initCropSize(){
if(mWidth != 0 && mHeight != 0
&& mOutputWidth != 0 && mOutputHeight != 0){
final double CROP_SIZE_RATIO = 0.9;
double scaleWidth = mWidth * CROP_SIZE_RATIO / mOutputWidth;
double scaleHeight = mHeight * CROP_SIZE_RATIO / mOutputHeight;
//缩放倍数取较小值
double scale = Math.min(scaleWidth, scaleHeight);

int mCropWidth = (int)(mOutputWidth * scale);
int mCropHeight = (int)(mOutputHeight * scale);

mZoomCropImageView.setCropSize(mCropWidth, mCropHeight);
mCropImageBorderView.setCropSize(mCropWidth, mCropHeight);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,27 @@
import android.provider.DocumentsContract;
import android.provider.MediaStore;

import java.io.File;

/**
* http://stackoverflow.com/questions/20067508/get-real-path-from-uri-android-kitkat-new-storage-access-framework
*/
public class FileUtils {

public static File createFile(String dir, String name) {
File file = new File(dir, name);
File dirFile = file.getParentFile();
if (!dirFile.exists()) {
dirFile.mkdirs();
}
return file;
}

public static boolean isSdCardMounted() {
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);
}

/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
Expand Down
Loading

0 comments on commit aca0d3a

Please sign in to comment.