Skip to content

Commit

Permalink
[ +] 增加CropImageView
Browse files Browse the repository at this point in the history
  • Loading branch information
yangsanning committed Jan 7, 2020
1 parent 19420a1 commit a59a3d1
Show file tree
Hide file tree
Showing 32 changed files with 3,608 additions and 13 deletions.
13 changes: 13 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation rootProject.ext.supportLibs
implementation "com.android.support:recyclerview-v7:" +rootProject.ext.dependVersion.support_version

implementation 'com.github.tbruyelle:rxpermissions:0.10.2' // 权限控制
implementation "io.reactivex.rxjava2:rxjava:2.1.10"
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.42'

implementation project(':cropimageview')

testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ysn.com.demo.cropimageview">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -16,6 +19,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".CropActivity"/>
</application>

</manifest>
127 changes: 127 additions & 0 deletions app/src/main/java/ysn/com/demo/cropimageview/CropActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package ysn.com.demo.cropimageview;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import java.util.Arrays;

import ysn.com.view.cropimageview.CropImageView;
import ysn.com.view.cropimageview.listener.OnCropMultiListener;
import ysn.com.view.cropimageview.mode.CropMode;
import ysn.com.view.cropimageview.mode.RotateAngle;
import ysn.com.view.cropimageview.utils.FileUtils;
import ysn.com.view.cropimageview.utils.Utils;

/**
* @Author yangsanning
* @ClassName CropActivity
* @Description 一句话概括作用
* @Date 2020/1/7
* @History 2020/1/7 author: description:
*/
public class CropActivity extends AppCompatActivity implements View.OnClickListener, OnCropMultiListener {

public static final int REQUEST_CODE_SELECT = 100;

RecyclerView cropModeRecyclerView;
RecyclerView angleRecyclerView;
CropImageView cropImageView;

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

cropImageView = findViewById(R.id.crop_activity_crop_image_view);
cropImageView.setOnCropMultiListener(this);

initCropModeAdapter();
initAngleAdapter();

findViewById(R.id.crop_activity_select_image).setOnClickListener(this);
findViewById(R.id.crop_activity_crop).setOnClickListener(this);
}

private void initCropModeAdapter() {
StringAdapter cropModeAdapter = new StringAdapter();
cropModeAdapter.setOnItemClickListener((adapter, view, position) ->
cropImageView.setCropMode(CropMode.getValue(position)));
cropModeRecyclerView = findViewById(R.id.crop_activity_crop_mode_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayout.HORIZONTAL);
cropModeRecyclerView.setLayoutManager(layoutManager);
cropModeRecyclerView.setAdapter(cropModeAdapter);
cropModeAdapter.setNewData(Arrays.asList(getResources().getStringArray(R.array.crop_mode)));
}

private void initAngleAdapter() {
int[] angles = getResources().getIntArray(R.array.angle_value);
StringAdapter angleAdapter = new StringAdapter();
angleAdapter.setOnItemClickListener((adapter, view, position) ->
cropImageView.rotateImage(RotateAngle.getValue(angles[position])));
angleRecyclerView = findViewById(R.id.crop_activity_angle_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayout.HORIZONTAL);
angleRecyclerView.setLayoutManager(layoutManager);
angleRecyclerView.setAdapter(angleAdapter);
angleAdapter.setNewData(Arrays.asList(getResources().getStringArray(R.array.angle)));
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.crop_activity_select_image:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_SELECT);
break;
case R.id.crop_activity_crop:
cropImageView.crop(FileUtils.getYsnUri(CropActivity.this, Bitmap.CompressFormat.PNG));
break;
default:
break;
}
}

@Override
public void onLoadSuccess() {

}

@Override
public void onCropSuccess(Bitmap cropBitmap) {

}

@Override
public void onCropSaveSuccess(Uri saveUri) {
Intent intent = new Intent();
intent.setData(saveUri);
setResult(RESULT_OK, intent);
finish();
}

@Override
public void onError(Throwable e) {

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent result) {
super.onActivityResult(requestCode, resultCode, result);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_SELECT) {
cropImageView.load(result.getData(), true);
}
}
}
51 changes: 50 additions & 1 deletion app/src/main/java/ysn/com/demo/cropimageview/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
package ysn.com.demo.cropimageview;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.android.schedulers.AndroidSchedulers;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

public static final int REQUEST_CODE_CROP = 2020;

ImageView cropImageView;

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

cropImageView = findViewById(R.id.main_activity_preview);

new RxPermissions(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(granted -> {
if (granted) {
setViewClickListener();
} else {
Toast.makeText(this, "权限不足", Toast.LENGTH_SHORT).show();
}
});
}

private void setViewClickListener() {
findViewById(R.id.main_activity_preview).setOnClickListener(this);
}

@Override
public void onClick(View v) {
startActivityForResult(new Intent(this, CropActivity.class), REQUEST_CODE_CROP);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_CROP && data != null) {
cropImageView.setImageURI(data.getData());
}
}
}
}
23 changes: 23 additions & 0 deletions app/src/main/java/ysn/com/demo/cropimageview/StringAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ysn.com.demo.cropimageview;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

/**
* @Author yangsanning
* @ClassName StringAdapter
* @Description 一句话概括作用
* @Date 2020/1/7
* @History 2020/1/7 author: description:
*/
public class StringAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

public StringAdapter() {
super(R.layout.item_string);
}

@Override
protected void convert(BaseViewHolder helper, String item) {
helper.setText(R.id.string_item_text, item);
}
}
Binary file added app/src/main/res/drawable/mm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 95 additions & 0 deletions app/src/main/res/layout/activity_crop.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ysn="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ysn.com.view.cropimageview.CropImageView
android:id="@+id/crop_activity_crop_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="15dp"
ysn:civ_anim_duration="100"
ysn:civ_anim_enabled="true"
ysn:civ_bg_color="@android:color/transparent"
ysn:civ_crop_enabled="true"
ysn:civ_crop_mode="fit_image"
ysn:civ_drag_point_padding="8dp"
ysn:civ_drag_point_show="true"
ysn:civ_drag_point_size="12dp"
ysn:civ_gird_column_part="3"
ysn:civ_gird_row_part="3"
ysn:civ_grid_color="@android:color/white"
ysn:civ_grid_line_mode="show_always"
ysn:civ_grid_line_stroke="1dp"
ysn:civ_grid_min_size="100dp"
ysn:civ_grid_stroke="1dp"
ysn:civ_img_src="@drawable/mm"
ysn:civ_make_color="#AA1C1C1C" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="5dp">

<TextView
android:id="@+id/crop_activity_select_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="选择图片"
android:textColor="@color/text"
android:textSize="18sp" />

<TextView
android:id="@+id/crop_activity_crop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="12dp"
android:text="裁剪"
android:textColor="@color/text"
android:textSize="18sp" />
</RelativeLayout>

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/line" />

<android.support.v7.widget.RecyclerView
android:id="@+id/crop_activity_angle_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/line" />

<android.support.v7.widget.RecyclerView
android:id="@+id/crop_activity_crop_mode_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />

<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@color/line" />
</LinearLayout>
24 changes: 19 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
<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"
android:orientation="vertical"
tools:context=".MainActivity">

<ImageView
android:id="@+id/main_activity_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:src="@drawable/mm" />

<TextView
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />

</FrameLayout>
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:padding="10dp"
android:text="点击图片进入裁剪"
android:textColor="@android:color/white"
android:textSize="18sp" />
</RelativeLayout>
Loading

0 comments on commit a59a3d1

Please sign in to comment.