This repository has been archived by the owner on Apr 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 236
/
MainActivity.java
205 lines (169 loc) · 6.54 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Copyright (C) 2015 Lyft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lyft.android.scissorssample;
import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTouch;
import com.lyft.android.scissors2.CropView;
import com.squareup.leakcanary.RefWatcher;
import java.io.File;
import java.util.List;
import rx.Observable;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;
import static android.graphics.Bitmap.CompressFormat.JPEG;
import static rx.android.schedulers.AndroidSchedulers.mainThread;
import static rx.schedulers.Schedulers.io;
public class MainActivity extends Activity {
private static final float[] ASPECT_RATIOS = { 0f, 1f, 6f/4f, 16f/9f };
private static final String[] ASPECT_LABELS = { "\u00D8", "1:1", "6:4", "16:9" };
@Bind(R.id.crop_view)
CropView cropView;
@Bind({ R.id.crop_fab, R.id.pick_mini_fab, R.id.ratio_fab })
List<View> buttons;
@Bind(R.id.pick_fab)
View pickButton;
CompositeSubscription subscriptions = new CompositeSubscription();
private int selectedRatio = 0;
private AnimatorListener animatorListener = new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
ButterKnife.apply(buttons, VISIBILITY, View.INVISIBLE);
}
@Override
public void onAnimationEnd(Animator animation) {
ButterKnife.apply(buttons, VISIBILITY, View.VISIBLE);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCodes.PICK_IMAGE_FROM_GALLERY
&& resultCode == Activity.RESULT_OK) {
Uri galleryPictureUri = data.getData();
cropView.extensions()
.load(galleryPictureUri);
updateButtons();
}
}
@OnClick(R.id.crop_fab)
public void onCropClicked() {
final File croppedFile = new File(getCacheDir(), "cropped.jpg");
Observable<Void> onSave = Observable.from(cropView.extensions()
.crop()
.quality(100)
.format(JPEG)
.into(croppedFile))
.subscribeOn(io())
.observeOn(mainThread());
subscriptions.add(onSave
.subscribe(new Action1<Void>() {
@Override
public void call(Void nothing) {
CropResultActivity.startUsing(croppedFile, MainActivity.this);
}
}));
}
@OnClick({ R.id.pick_fab, R.id.pick_mini_fab })
public void onPickClicked() {
cropView.extensions()
.pickUsing(this, RequestCodes.PICK_IMAGE_FROM_GALLERY);
}
@OnClick(R.id.ratio_fab)
public void onRatioClicked() {
final float oldRatio = cropView.getImageRatio();
selectedRatio = (selectedRatio + 1) % ASPECT_RATIOS.length;
// Since the animation needs to interpolate to the native
// ratio, we need to get that instead of using 0
float newRatio = ASPECT_RATIOS[selectedRatio];
if (Float.compare(0, newRatio) == 0) {
newRatio = cropView.getImageRatio();
}
ObjectAnimator viewportRatioAnimator = ObjectAnimator.ofFloat(cropView, "viewportRatio", oldRatio, newRatio)
.setDuration(420);
autoCancel(viewportRatioAnimator);
viewportRatioAnimator.addListener(animatorListener);
viewportRatioAnimator.start();
Toast.makeText(this, ASPECT_LABELS[selectedRatio], Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
subscriptions.unsubscribe();
RefWatcher refWatcher = App.getRefWatcher(this);
refWatcher.watch(this, "MainActivity");
refWatcher.watch(cropView, "cropView");
}
@OnTouch(R.id.crop_view)
public boolean onTouchCropView(MotionEvent event) { // GitHub issue #4
if (event.getPointerCount() > 1 || cropView.getImageBitmap() == null) {
return true;
}
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
ButterKnife.apply(buttons, VISIBILITY, View.INVISIBLE);
break;
default:
ButterKnife.apply(buttons, VISIBILITY, View.VISIBLE);
break;
}
return true;
}
private void updateButtons() {
ButterKnife.apply(buttons, VISIBILITY, View.VISIBLE);
pickButton.setVisibility(View.GONE);
}
static final ButterKnife.Setter<View, Integer> VISIBILITY = new ButterKnife.Setter<View, Integer>() {
@Override
public void set(final View view, final Integer visibility, int index) {
view.setVisibility(visibility);
}
};
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
static void autoCancel(ObjectAnimator objectAnimator) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {
objectAnimator.setAutoCancel(true);
}
}
}