PhotoPreview.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. package epson.print.phlayout;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.Paint;
  8. import android.graphics.Path;
  9. import android.graphics.Point;
  10. import android.graphics.Rect;
  11. import android.graphics.RectF;
  12. import android.graphics.Region;
  13. import android.os.Handler;
  14. import android.os.Message;
  15. import android.support.annotation.AnyThread;
  16. import android.support.p000v4.view.GestureDetectorCompat;
  17. import android.util.AttributeSet;
  18. import android.util.Log;
  19. import android.view.GestureDetector;
  20. import android.view.MotionEvent;
  21. import android.view.ScaleGestureDetector;
  22. import android.view.View;
  23. import java.lang.annotation.Retention;
  24. import java.lang.annotation.RetentionPolicy;
  25. import java.util.concurrent.locks.ReentrantLock;
  26. import epson.common.Info_paper;
  27. import epson.print.EPImage;
  28. import epson.print.EPImageCreator;
  29. import epson.print.EPImageList;
  30. import epson.print.IprintApplication;
  31. import epson.print.R;
  32. public class PhotoPreview extends View {
  33. private static final int ALL_REFRESH_DRAW_END = 4;
  34. private static final int CREATE_PREVIEW = 1;
  35. static final int FIT_MODE_CIRCUMSCRIBED = 1;
  36. static final int FIT_MODE_FREE = 0;
  37. static final int FIT_MODE_INSCRIBED = 2;
  38. private static final int INVALID_IMAGE_NO = -1;
  39. private static final String LOG_TAG = "previewView";
  40. private static final float MAX_MAGNIFICATION_FACTOR = 8.0f;
  41. private static final float MINI_MAGNIFICATION_FACTOR = 0.25f;
  42. private static final double MOVEMENT_MARGIN_RATE = 0.2d;
  43. public static final int RESET_PREVIEW = 2;
  44. public static final int RESTORE_PREVIEW = 0;
  45. private static final int ZOOM_CONTROL = 2;
  46. private float baseScaleFactor = 1.0f;
  47. private int color = 0;
  48. private Handler drawEndHandler = null;
  49. private PhotoPreviewImageList imageList = null;
  50. private volatile int imageNo = 0;
  51. private final Object lockObj = new Object();
  52. private Bitmap mBitmap = null;
  53. private final Rect mBitmapDrawRect = new Rect();
  54. private final ReentrantLock mDrawLock = new ReentrantLock();
  55. private final FitLayout mFitLayout = new FitLayout();
  56. private volatile int mFitMode;
  57. private GestureDetectorCompat mGestureDetector;
  58. private volatile ILayoutPosition mLayoutPosition = null;
  59. private int mPaperSizeId = 0;
  60. private final Path mPath = new Path();
  61. private volatile PreviewBitmapMaker mPreviewBitmapMaker;
  62. private ScaleGestureDetector mScaleGestureDetector = null;
  63. private int[] mViewSize = new int[2];
  64. private ViewSizeChangeListener mViewSizeChangeListener;
  65. private boolean maskExpandPreview = true;
  66. private int maxBitmapHeight = -1;
  67. private int maxBitmapWidth = -1;
  68. private final Paint paint = new Paint();
  69. private float scaleFactor = 1.0f;
  70. private final Point trans = new Point();
  71. private Handler zoomControlHandler = null;
  72. @Retention(RetentionPolicy.SOURCE)
  73. @interface FitMode {
  74. }
  75. public interface PreviewBitmapMaker {
  76. Bitmap getPreviewBitmap(EPImage ePImage, int i, int i2, int i3, boolean z);
  77. }
  78. public interface ViewSizeChangeListener {
  79. void onViewSizeChanged();
  80. }
  81. public PhotoPreview(Context context) {
  82. super(context);
  83. initLocal(context);
  84. }
  85. public PhotoPreview(Context context, AttributeSet attributeSet) {
  86. super(context, attributeSet);
  87. initLocal(context);
  88. }
  89. private void initLocal(Context context) {
  90. mGestureDetector = new GestureDetectorCompat(context, new MyGestureListener(this));
  91. mScaleGestureDetector = new ScaleGestureDetector(context, new MyScaledGestureListener(this));
  92. mFitMode = 1;
  93. }
  94. private void startScaleChange() {
  95. maskExpandPreview = false;
  96. baseScaleFactor = scaleFactor;
  97. mFitMode = 0;
  98. invalidate();
  99. }
  100. private void changeScale(float f) {
  101. scaleFactor = f * baseScaleFactor;
  102. float f2 = scaleFactor;
  103. if (f2 < MINI_MAGNIFICATION_FACTOR) {
  104. scaleFactor = MINI_MAGNIFICATION_FACTOR;
  105. } else if (f2 > MAX_MAGNIFICATION_FACTOR) {
  106. scaleFactor = MAX_MAGNIFICATION_FACTOR;
  107. }
  108. Handler handler = zoomControlHandler;
  109. if (handler != null) {
  110. handler.sendEmptyMessage(2);
  111. }
  112. adjustTrans(trans);
  113. invalidate();
  114. }
  115. private void changeFitModeAndRefresh() {
  116. maskExpandPreview = true;
  117. changeFitMode();
  118. invalidate();
  119. Handler handler = zoomControlHandler;
  120. if (handler != null) {
  121. handler.sendEmptyMessage(2);
  122. }
  123. }
  124. private void changeFitMode() {
  125. if (1 == mLayoutPosition.getLayoutId()) {
  126. fitCircumscribed();
  127. } else if (mFitMode == 1) {
  128. fitInscribed();
  129. } else {
  130. fitCircumscribed();
  131. }
  132. }
  133. @WorkerThread
  134. private void fitCircumscribed() {
  135. scaleFactor = 1.0f;
  136. baseScaleFactor = 1.0f;
  137. setPaper(mPaperSizeId);
  138. mFitMode = 1;
  139. }
  140. private void fitInscribed() {
  141. EPImage currentEpImage;
  142. mFitMode = 2;
  143. if (mLayoutPosition != null && (currentEpImage = getCurrentEpImage()) != null) {
  144. RectF previewImageCircumscribedTargetSize = mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  145. double[] fitInscribed = mFitLayout.fitInscribed(new double[]{(double) previewImageCircumscribedTargetSize.left, (double) previewImageCircumscribedTargetSize.top, (double) previewImageCircumscribedTargetSize.right, (double) previewImageCircumscribedTargetSize.bottom}, new double[]{(double) currentEpImage.previewWidth, (double) currentEpImage.previewHeight});
  146. if (fitInscribed != null) {
  147. Point point = trans;
  148. point.x = (int) fitInscribed[0];
  149. point.y = (int) fitInscribed[1];
  150. scaleFactor = (float) fitInscribed[2];
  151. }
  152. }
  153. }
  154. @AnyThread
  155. private EPImage getCurrentEpImage() {
  156. if (imageNo < 0 || imageNo >= imageList.size()) {
  157. return null;
  158. }
  159. return imageList.get(imageNo);
  160. }
  161. private PhotoPreviewImageList.Element getCurrentElement() {
  162. return imageList.getElement(imageNo);
  163. }
  164. @WorkerThread
  165. private static Bitmap getBitmap(EPImage ePImage, int i, int i2, int i3, boolean z) {
  166. new EPImageCreator(IprintApplication.getInstance()).createPreviewImage(ePImage, i, i2, i3, z);
  167. if (ePImage.previewImageFileName != null) {
  168. return BitmapFactory.decodeFile(ePImage.previewImageFileName);
  169. }
  170. return null;
  171. }
  172. public void setViewSizeChangeListener(ViewSizeChangeListener viewSizeChangeListener) {
  173. mViewSizeChangeListener = viewSizeChangeListener;
  174. }
  175. public void setPreviewBitmapMaker(PreviewBitmapMaker previewBitmapMaker) {
  176. mPreviewBitmapMaker = previewBitmapMaker;
  177. }
  178. public EPImageList getImageList() {
  179. EPImageList ePImageList;
  180. synchronized (lockObj) {
  181. saveImage(imageNo);
  182. ePImageList = new EPImageList();
  183. ePImageList.setRenderingMode(1);
  184. for (int i = 0; i < imageList.size(); i++) {
  185. EPImage ePImage = imageList.get(i);
  186. if (ePImage != null) {
  187. RectF calcPreviewImageRect = calcPreviewImageRect(ePImage.previewWidth, ePImage.previewHeight, ePImage.scaleFactor, new Point(ePImage.previewImageRectCenterX, ePImage.previewImageRectCenterY));
  188. EPImage ePImage2 = new EPImage(ePImage);
  189. ePImage2.previewImageRectLeft = calcPreviewImageRect.left;
  190. ePImage2.previewImageRectTop = calcPreviewImageRect.top;
  191. ePImage2.previewImageRectRight = calcPreviewImageRect.right;
  192. ePImage2.previewImageRectBottom = calcPreviewImageRect.bottom;
  193. ePImageList.add(ePImage2);
  194. }
  195. }
  196. }
  197. return ePImageList;
  198. }
  199. public void setImageList(@NonNull PhotoPreviewImageList photoPreviewImageList) {
  200. imageList = photoPreviewImageList;
  201. }
  202. public void setLayout(int i, int i2) {
  203. if (i == 1) {
  204. mLayoutPosition = new BorderlessLayoutPosition();
  205. } else if (i != 4) {
  206. mLayoutPosition = new BorderedLayoutPosition();
  207. } else {
  208. mLayoutPosition = new CdLayoutPosition();
  209. }
  210. mLayoutPosition.setLayoutId(i, i2);
  211. }
  212. public void setColor(int i) {
  213. color = i;
  214. }
  215. public void setZoomControlHandler(Handler handler) {
  216. zoomControlHandler = handler;
  217. }
  218. public void setDrawEndHandler(Handler handler) {
  219. drawEndHandler = handler;
  220. }
  221. public boolean getIsPaperLandscape() {
  222. return mLayoutPosition.getIsPaperLandscape();
  223. }
  224. @WorkerThread
  225. public void setIsPaperLandscape(boolean z, Activity activity) {
  226. synchronized (lockObj) {
  227. mLayoutPosition.setPaperLandscape(z);
  228. if (imageList == null || imageList.size() <= 0) {
  229. setPaper(mPaperSizeId);
  230. } else {
  231. scaleFactor = 1.0f;
  232. baseScaleFactor = 1.0f;
  233. mFitMode = 1;
  234. setImage(imageNo, 1, activity);
  235. }
  236. postInvalidate();
  237. }
  238. }
  239. @AnyThread
  240. public boolean setPaper(int i) {
  241. synchronized (lockObj) {
  242. mPaperSizeId = i;
  243. Info_paper infoPaper = Info_paper.getInfoPaper(IprintApplication.getInstance(), mPaperSizeId);
  244. if (infoPaper == null) {
  245. return false;
  246. }
  247. int[] viewSize = getViewSize();
  248. if (viewSize == null) {
  249. return false;
  250. }
  251. mLayoutPosition.setPaperAndCalcPreviewPosition(infoPaper, viewSize[0], viewSize[1], trans);
  252. return true;
  253. }
  254. }
  255. @Nullable
  256. @AnyThread
  257. private synchronized int[] getViewSize() {
  258. if (mViewSize[0] > 0) {
  259. if (mViewSize[1] > 0) {
  260. return mViewSize;
  261. }
  262. }
  263. return null;
  264. }
  265. @WorkerThread
  266. private void reCalcPaperPosition() {
  267. Info_paper infoPaper;
  268. int[] viewSize = getViewSize();
  269. if (viewSize != null && (infoPaper = Info_paper.getInfoPaper(IprintApplication.getInstance(), mPaperSizeId)) != null) {
  270. mLayoutPosition.setPaperAndCalcPreviewPosition(infoPaper, viewSize[0], viewSize[1], new Point());
  271. }
  272. }
  273. @AnyThread
  274. public void invalidateImageNo() {
  275. if (imageList != null) {
  276. imageNo = -1;
  277. }
  278. }
  279. public void invalidatePreview() {
  280. if (imageList != null) {
  281. synchronized (lockObj) {
  282. for (int i = 0; i < imageList.size(); i++) {
  283. imageList.get(i).previewImageFileName = null;
  284. }
  285. }
  286. }
  287. }
  288. /* JADX WARNING: Code restructure failed: missing block: B:62:0x0099, code lost:
  289. r4.mDrawLock.unlock();
  290. */
  291. /* JADX WARNING: Code restructure failed: missing block: B:63:0x009e, code lost:
  292. return false;
  293. */
  294. /* JADX WARNING: Missing exception handler attribute for start block: B:45:0x007e */
  295. /* JADX WARNING: Missing exception handler attribute for start block: B:50:0x0088 */
  296. @android.support.annotation.WorkerThread
  297. /* Code decompiled incorrectly, please refer to instructions dump. */
  298. public boolean setImage(int r5, int r6, android.app.Activity r7) throws java.lang.OutOfMemoryError {
  299. /*
  300. r4 = this;
  301. java.util.concurrent.locks.ReentrantLock r0 = r4.mDrawLock // Catch:{ all -> 0x00a2 }
  302. r0.lock() // Catch:{ all -> 0x00a2 }
  303. java.lang.Object r0 = r4.lockObj // Catch:{ all -> 0x00a2 }
  304. monitor-enter(r0) // Catch:{ all -> 0x00a2 }
  305. epson.print.phlayout.PhotoPreviewImageList r1 = r4.imageList // Catch:{ all -> 0x009f }
  306. r2 = 0
  307. if (r1 == 0) goto L_0x0098
  308. epson.print.phlayout.ILayoutPosition r1 = r4.mLayoutPosition // Catch:{ all -> 0x009f }
  309. if (r1 != 0) goto L_0x0013
  310. goto L_0x0098
  311. L_0x0013:
  312. epson.print.phlayout.PhotoPreviewImageList r1 = r4.imageList // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  313. int r1 = r1.size() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  314. r3 = 1
  315. if (r1 > 0) goto L_0x003e
  316. r5 = 0
  317. r4.mBitmap = r5 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  318. r5 = 2
  319. if (r6 != r5) goto L_0x0025
  320. r4.fitCircumscribed() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  321. L_0x0025:
  322. if (r6 == r3) goto L_0x002f
  323. epson.print.phlayout.ILayoutPosition r5 = r4.mLayoutPosition // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  324. boolean r5 = r5.isPreviewImageSizeValid() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  325. if (r5 != 0) goto L_0x0034
  326. L_0x002f:
  327. int r5 = r4.mPaperSizeId // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  328. r4.setPaper(r5) // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  329. L_0x0034:
  330. r4.postInvalidate() // Catch:{ all -> 0x009f }
  331. monitor-exit(r0) // Catch:{ all -> 0x009f }
  332. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  333. r5.unlock()
  334. return r2
  335. L_0x003e:
  336. r4.setImageCore(r5, r6) // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  337. android.graphics.Bitmap r5 = r4.mBitmap // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  338. if (r5 == 0) goto L_0x0076
  339. int r5 = r4.maxBitmapWidth // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  340. r6 = -1
  341. if (r5 == r6) goto L_0x0054
  342. android.graphics.Bitmap r5 = r4.mBitmap // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  343. int r5 = r5.getWidth() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  344. int r1 = r4.maxBitmapWidth // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  345. if (r5 > r1) goto L_0x0062
  346. L_0x0054:
  347. int r5 = r4.maxBitmapWidth // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  348. if (r5 == r6) goto L_0x006a
  349. android.graphics.Bitmap r5 = r4.mBitmap // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  350. int r5 = r5.getHeight() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  351. int r6 = r4.maxBitmapHeight // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  352. if (r5 <= r6) goto L_0x006a
  353. L_0x0062:
  354. epson.print.phlayout.PhotoPreview$1 r5 = new epson.print.phlayout.PhotoPreview$1 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  355. r5.<init>() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  356. r7.runOnUiThread(r5) // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  357. L_0x006a:
  358. r4.maskExpandPreview = r3 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  359. r4.postInvalidate() // Catch:{ all -> 0x009f }
  360. monitor-exit(r0) // Catch:{ all -> 0x009f }
  361. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  362. r5.unlock()
  363. return r3
  364. L_0x0076:
  365. java.lang.OutOfMemoryError r5 = new java.lang.OutOfMemoryError // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  366. r5.<init>() // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  367. throw r5 // Catch:{ OutOfMemoryError -> 0x0092, NullPointerException -> 0x0088, Exception -> 0x007e }
  368. L_0x007c:
  369. r5 = move-exception
  370. goto L_0x0094
  371. L_0x007e:
  372. r4.postInvalidate() // Catch:{ all -> 0x009f }
  373. monitor-exit(r0) // Catch:{ all -> 0x009f }
  374. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  375. r5.unlock()
  376. return r2
  377. L_0x0088:
  378. r4.postInvalidate() // Catch:{ all -> 0x009f }
  379. monitor-exit(r0) // Catch:{ all -> 0x009f }
  380. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  381. r5.unlock()
  382. return r2
  383. L_0x0092:
  384. r5 = move-exception
  385. throw r5 // Catch:{ all -> 0x007c }
  386. L_0x0094:
  387. r4.postInvalidate() // Catch:{ all -> 0x009f }
  388. throw r5 // Catch:{ all -> 0x009f }
  389. L_0x0098:
  390. monitor-exit(r0) // Catch:{ all -> 0x009f }
  391. java.util.concurrent.locks.ReentrantLock r5 = r4.mDrawLock
  392. r5.unlock()
  393. return r2
  394. L_0x009f:
  395. r5 = move-exception
  396. monitor-exit(r0) // Catch:{ all -> 0x009f }
  397. throw r5 // Catch:{ all -> 0x00a2 }
  398. L_0x00a2:
  399. r5 = move-exception
  400. java.util.concurrent.locks.ReentrantLock r6 = r4.mDrawLock
  401. r6.unlock()
  402. throw r5
  403. */
  404. throw new UnsupportedOperationException("Method not decompiled: epson.print.phlayout.PhotoPreview.setImage(int, int, android.app.Activity):boolean");
  405. }
  406. @WorkerThread
  407. private void setImageCore(int i, int i2) throws Exception {
  408. if (i < 0 || i >= imageList.size()) {
  409. throw new Exception();
  410. } else if (imageNo < 0 || imageNo >= imageList.size() || saveImage(imageNo)) {
  411. imageNo = i;
  412. PhotoPreviewImageList.Element currentElement = getCurrentElement();
  413. EPImage currentEpImage = getCurrentEpImage();
  414. if (i2 == 0) {
  415. setImageCore_RestorePreview(currentElement);
  416. } else {
  417. setImageCore2(currentEpImage, i2 == 2);
  418. }
  419. } else {
  420. throw new Exception();
  421. }
  422. }
  423. @WorkerThread
  424. private void setImageCore2(EPImage ePImage, boolean z) throws Exception {
  425. if (z) {
  426. mLayoutPosition.setPaperLandscape(ePImage.srcWidth > ePImage.srcHeight);
  427. fitCircumscribed();
  428. } else {
  429. setPaper(mPaperSizeId);
  430. }
  431. if (mLayoutPosition.isPreviewImageSizeValid()) {
  432. RectF previewImageCircumscribedTargetSize = mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  433. mBitmap = localGetPreviewBitmap(ePImage, (int) previewImageCircumscribedTargetSize.width(), (int) previewImageCircumscribedTargetSize.height(), color, true, !z);
  434. return;
  435. }
  436. throw new Exception();
  437. }
  438. @WorkerThread
  439. private void setImageCore_RestorePreview(@NonNull PhotoPreviewImageList.Element element) throws Exception {
  440. restoreImage(element);
  441. reCalcPaperPosition();
  442. if (mLayoutPosition.isPreviewImageSizeValid()) {
  443. RectF previewImageCircumscribedTargetSize = mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  444. mBitmap = localGetPreviewBitmap(element.getEPImage(), (int) previewImageCircumscribedTargetSize.width(), (int) previewImageCircumscribedTargetSize.height(), color, false, true);
  445. return;
  446. }
  447. throw new Exception();
  448. }
  449. @WorkerThread
  450. private Bitmap localGetPreviewBitmap(EPImage ePImage, int i, int i2, int i3, boolean z, boolean z2) {
  451. if (mPreviewBitmapMaker != null && z2) {
  452. mBitmap = mPreviewBitmapMaker.getPreviewBitmap(ePImage, i, i2, i3, z);
  453. Bitmap bitmap = mBitmap;
  454. if (bitmap != null) {
  455. return bitmap;
  456. }
  457. }
  458. mBitmap = getBitmap(ePImage, i, i2, i3, z);
  459. if (mBitmap == null) {
  460. return null;
  461. }
  462. Log.i(LOG_TAG, "bitmap <" + mBitmap.getWidth() + PreferencesConstants.COOKIE_DELIMITER + mBitmap.getHeight() + "> preview WxH <" + ePImage.previewWidth + PreferencesConstants.COOKIE_DELIMITER + ePImage.previewHeight + ">");
  463. return mBitmap;
  464. }
  465. @WorkerThread
  466. private boolean saveImage(int i) {
  467. try {
  468. PhotoPreviewImageList.Element element = imageList.getElement(i);
  469. EPImage ePImage = element.getEPImage();
  470. if (ePImage == null) {
  471. return false;
  472. }
  473. ePImage.previewImageRectCenterX = trans.x;
  474. ePImage.previewImageRectCenterY = trans.y;
  475. mLayoutPosition.copyPreviewPrintAreaToEpImage(ePImage);
  476. RectF calcPreviewImageRect = calcPreviewImageRect(mBitmap, scaleFactor, trans);
  477. ePImage.previewImageRectLeft = calcPreviewImageRect.left;
  478. ePImage.previewImageRectTop = calcPreviewImageRect.top;
  479. ePImage.previewImageRectRight = calcPreviewImageRect.right;
  480. ePImage.previewImageRectBottom = calcPreviewImageRect.bottom;
  481. ePImage.scaleFactor = scaleFactor;
  482. imageList.set(i, ePImage);
  483. element.setFitMode(mFitMode);
  484. return true;
  485. } catch (IndexOutOfBoundsException unused) {
  486. return false;
  487. }
  488. }
  489. @WorkerThread
  490. private void restoreImage(@NonNull PhotoPreviewImageList.Element element) {
  491. EPImage ePImage = element.getEPImage();
  492. trans.x = ePImage.previewImageRectCenterX;
  493. trans.y = ePImage.previewImageRectCenterY;
  494. mLayoutPosition.copyPreviewPrintAreaFromEpImage(ePImage);
  495. scaleFactor = ePImage.scaleFactor;
  496. mFitMode = element.getFitMode();
  497. }
  498. public boolean onTouchEvent(MotionEvent motionEvent) {
  499. boolean z = mGestureDetector.onTouchEvent(motionEvent) || mScaleGestureDetector.onTouchEvent(motionEvent);
  500. if ((motionEvent.getAction() & 255) == 1) {
  501. onMoveEventEnd();
  502. }
  503. if (z || super.onTouchEvent(motionEvent)) {
  504. return true;
  505. }
  506. return false;
  507. }
  508. private void onMoveEventEnd() {
  509. invalidate();
  510. }
  511. private void onMoveEventStarted(float f, float f2) {
  512. maskExpandPreview = false;
  513. invalidate();
  514. }
  515. private void onMoving(float f, float f2) {
  516. mFitMode = 0;
  517. if (mBitmap != null) {
  518. Point point = trans;
  519. point.x = (int) ((-f) + ((float) point.x));
  520. Point point2 = trans;
  521. point2.y = (int) ((-f2) + ((float) point2.y));
  522. adjustTrans(trans);
  523. invalidate();
  524. }
  525. }
  526. private void adjustTrans(@NonNull Point point) {
  527. synchronized (lockObj) {
  528. if (mLayoutPosition != null) {
  529. RectF previewImageCircumscribedTargetSize = mLayoutPosition.getPreviewImageCircumscribedTargetSize();
  530. EPImage currentEpImage = getCurrentEpImage();
  531. if (currentEpImage != null) {
  532. adjustPosition(point, MOVEMENT_MARGIN_RATE, previewImageCircumscribedTargetSize, (double) (((float) currentEpImage.previewWidth) * scaleFactor), (double) (((float) currentEpImage.previewHeight) * scaleFactor));
  533. }
  534. }
  535. }
  536. }
  537. private void adjustPosition(@NonNull Point point, double d, RectF rectF, double d2, double d3) {
  538. Point point2 = point;
  539. RectF rectF2 = rectF;
  540. double d4 = ((double) (rectF2.right - rectF2.left)) * d;
  541. double d5 = ((double) (rectF2.bottom - rectF2.top)) * d;
  542. double d6 = d2 / 2.0d;
  543. if (((double) rectF2.left) + d4 > ((double) point2.x) + d6) {
  544. point2.x = (int) ((((double) rectF2.left) + d4) - d6);
  545. }
  546. double d7 = d3 / 2.0d;
  547. if (((double) rectF2.top) + d5 > ((double) point2.y) + d7) {
  548. point2.y = (int) ((((double) rectF2.top) + d5) - d7);
  549. }
  550. if (((double) rectF2.right) - d4 < ((double) point2.x) - d6) {
  551. point2.x = (int) ((((double) rectF2.right) - d4) + d6);
  552. }
  553. if (((double) rectF2.bottom) - d5 < ((double) point2.y) - d7) {
  554. point2.y = (int) ((((double) rectF2.bottom) - d5) + d7);
  555. }
  556. }
  557. public void onSizeChanged(int i, int i2, int i3, int i4) {
  558. super.onSizeChanged(i, i2, i3, i4);
  559. synchronized (this) {
  560. mViewSize[0] = i;
  561. mViewSize[1] = i2;
  562. }
  563. ViewSizeChangeListener viewSizeChangeListener = mViewSizeChangeListener;
  564. if (viewSizeChangeListener != null) {
  565. viewSizeChangeListener.onViewSizeChanged();
  566. }
  567. }
  568. public void onDraw(Canvas canvas) {
  569. super.onDraw(canvas);
  570. if (mDrawLock.tryLock()) {
  571. try {
  572. synchronized (lockObj) {
  573. if (!canvas.isHardwareAccelerated()) {
  574. if (maxBitmapWidth > 0 && maxBitmapHeight > 0) {
  575. maxBitmapWidth = -1;
  576. maxBitmapHeight = -1;
  577. }
  578. } else if (maxBitmapWidth < 0 || maxBitmapHeight < 0) {
  579. maxBitmapWidth = canvas.getMaximumBitmapWidth();
  580. maxBitmapHeight = canvas.getMaximumBitmapHeight();
  581. }
  582. RectF calcPreviewImageRect = mBitmap != null ? calcPreviewImageRect(mBitmap, scaleFactor, trans) : null;
  583. if (mLayoutPosition.getLayoutId() == 4) {
  584. drawCdPreviewAll(canvas, paint, mBitmap, mLayoutPosition, mBitmapDrawRect, maskExpandPreview, mPath, getResources().getColor(R.color.background_preview), calcPreviewImageRect);
  585. } else {
  586. drawPreview(canvas, paint, mBitmap, mLayoutPosition, mBitmapDrawRect, maskExpandPreview, getResources().getColor(R.color.background_preview), new int[]{getWidth(), getHeight()}, calcPreviewImageRect);
  587. }
  588. if (drawEndHandler != null) {
  589. Message message = new Message();
  590. message.what = 4;
  591. message.arg1 = imageNo;
  592. drawEndHandler.sendMessage(message);
  593. }
  594. }
  595. mDrawLock.unlock();
  596. } catch (Throwable th) {
  597. mDrawLock.unlock();
  598. throw th;
  599. }
  600. }
  601. }
  602. private void drawPreview(@NonNull Canvas canvas, @NonNull Paint paint2, @Nullable Bitmap bitmap, @NonNull ILayoutPosition iLayoutPosition, @NonNull Rect rect, boolean z, int i, int[] iArr, RectF rectF) {
  603. RectF rectF2;
  604. Canvas canvas2 = canvas;
  605. Paint paint3 = paint2;
  606. Bitmap bitmap2 = bitmap;
  607. Rect rect2 = rect;
  608. int layoutId = iLayoutPosition.getLayoutId();
  609. if (layoutId == 2) {
  610. rectF2 = iLayoutPosition.getPreviewImageCircumscribedTargetSize();
  611. } else {
  612. rectF2 = new RectF(iLayoutPosition.getPreviewPaperRect());
  613. }
  614. paint2.setColor(-1);
  615. canvas.drawRect(rectF2, paint2);
  616. if (bitmap2 != null) {
  617. rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
  618. canvas.drawBitmap(bitmap, rect, rectF, (Paint) null);
  619. }
  620. drawBorderedOutsidePrintingArea(canvas, rectF2, layoutId, paint2, z, iLayoutPosition, i, iArr);
  621. }
  622. private static void drawBorderedOutsidePrintingArea(@NonNull Canvas canvas, @NonNull RectF rectF, int i, @NonNull Paint paint2, boolean z, @NonNull ILayoutPosition iLayoutPosition, int i2, @NonNull int[] iArr) {
  623. RectF rectF2 = rectF;
  624. int i3 = i;
  625. Paint paint3 = paint2;
  626. int i4 = i2;
  627. if (rectF2.left != 0.0f && rectF2.top != 0.0f && rectF2.right != 0.0f && rectF2.bottom != 0.0f) {
  628. paint3.setColor(i4);
  629. if (!z) {
  630. paint3.setAlpha(221);
  631. }
  632. int leftMargin = iLayoutPosition.getLeftMargin();
  633. int topMargin = iLayoutPosition.getTopMargin();
  634. int rightMargin = iLayoutPosition.getRightMargin();
  635. int bottomMargin = iLayoutPosition.getBottomMargin();
  636. float f = (float) leftMargin;
  637. Canvas canvas2 = canvas;
  638. Paint paint4 = paint2;
  639. canvas2.drawRect(0.0f, 0.0f, rectF2.left - f, (float) iArr[1], paint4);
  640. float f2 = (float) rightMargin;
  641. float f3 = (float) topMargin;
  642. canvas.drawRect(rectF2.left - f, 0.0f, rectF2.right + f2, rectF2.top - f3, paint4);
  643. canvas.drawRect(rectF2.right + f2, 0.0f, (float) iArr[0], (float) iArr[1], paint4);
  644. float f4 = (float) bottomMargin;
  645. canvas.drawRect(rectF2.left - f, rectF2.bottom + f4, rectF2.right + f2, (float) iArr[1], paint4);
  646. if (i3 == 2) {
  647. paint3.setColor(-1);
  648. } else if (i3 == 1) {
  649. paint3.setColor(i4);
  650. if (!z) {
  651. paint3.setAlpha(221);
  652. }
  653. }
  654. Paint paint5 = paint2;
  655. canvas.drawRect(rectF2.left - f, rectF2.top - f3, rectF2.left, rectF2.bottom + f4, paint5);
  656. canvas.drawRect(rectF2.left, rectF2.top - f3, rectF2.right, rectF2.top, paint5);
  657. canvas.drawRect(rectF2.right, rectF2.top - f3, rectF2.right + f2, rectF2.bottom + f4, paint5);
  658. canvas.drawRect(rectF2.left, rectF2.bottom, rectF2.right, rectF2.bottom + f4, paint2);
  659. }
  660. }
  661. private void drawCdPreviewAll(@NonNull Canvas canvas, @NonNull Paint paint2, @Nullable Bitmap bitmap, @NonNull ILayoutPosition iLayoutPosition, @NonNull Rect rect, boolean z, @NonNull Path path, int i, @NonNull RectF rectF) {
  662. float f;
  663. if (bitmap != null) {
  664. rect.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
  665. canvas.drawBitmap(bitmap, rect, rectF, (Paint) null);
  666. }
  667. RectF previewImageCircumscribedTargetSize = iLayoutPosition.getPreviewImageCircumscribedTargetSize();
  668. paint2.setColor(i);
  669. if (!z) {
  670. paint2.setAlpha(221);
  671. }
  672. if (previewImageCircumscribedTargetSize.height() > previewImageCircumscribedTargetSize.width()) {
  673. f = previewImageCircumscribedTargetSize.width() / 2.0f;
  674. } else {
  675. f = previewImageCircumscribedTargetSize.height() / 2.0f;
  676. }
  677. path.reset();
  678. canvas.drawCircle(previewImageCircumscribedTargetSize.centerX(), previewImageCircumscribedTargetSize.centerY(), (43.0f * f) / 116.0f, paint2);
  679. path.addCircle(previewImageCircumscribedTargetSize.centerX(), previewImageCircumscribedTargetSize.centerY(), f, Path.Direction.CCW);
  680. canvas.clipPath(path, Region.Op.DIFFERENCE);
  681. canvas.drawPaint(paint2);
  682. }
  683. @WorkerThread
  684. @Deprecated
  685. @NonNull
  686. public RectF calcPreviewImageRect(@NonNull Bitmap bitmap, float f, @NonNull Point point) {
  687. return calcPreviewImageRect(bitmap.getWidth(), bitmap.getHeight(), f, point);
  688. }
  689. @WorkerThread
  690. @NonNull
  691. private RectF calcPreviewImageRect(int i, int i2, float f, @NonNull Point point) {
  692. RectF rectF = new RectF();
  693. float f2 = ((float) i) * f;
  694. rectF.left = ((float) point.x) - (f2 / 2.0f);
  695. float f3 = ((float) i2) * f;
  696. rectF.top = ((float) point.y) - (f3 / 2.0f);
  697. rectF.right = rectF.left + f2;
  698. rectF.bottom = rectF.top + f3;
  699. return rectF;
  700. }
  701. static class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
  702. private final PhotoPreview mPreviewView;
  703. public MyGestureListener(PhotoPreview photoPreview) {
  704. mPreviewView = photoPreview;
  705. }
  706. public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float f, float f2) {
  707. mPreviewView.onMoving(f, f2);
  708. return true;
  709. }
  710. public boolean onDown(MotionEvent motionEvent) {
  711. mPreviewView.onMoveEventStarted(motionEvent.getX(), motionEvent.getY());
  712. return true;
  713. }
  714. public boolean onDoubleTap(MotionEvent motionEvent) {
  715. mPreviewView.changeFitModeAndRefresh();
  716. return true;
  717. }
  718. }
  719. static class MyScaledGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
  720. private final PhotoPreview mPreviewView;
  721. public MyScaledGestureListener(PhotoPreview photoPreview) {
  722. mPreviewView = photoPreview;
  723. }
  724. public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
  725. mPreviewView.startScaleChange();
  726. return super.onScaleBegin(scaleGestureDetector);
  727. }
  728. public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
  729. mPreviewView.changeScale(scaleGestureDetector.getScaleFactor());
  730. return super.onScale(scaleGestureDetector);
  731. }
  732. }
  733. }