Advertisement
Guest User

Untitled

a guest
Jul 17th, 2023
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.82 KB | None | 0 0
  1. package lv.marmog.androidpuzzlegame;
  2.  
  3. import androidx.appcompat.app.AlertDialog;
  4. import androidx.appcompat.app.AppCompatActivity;
  5. import android.content.Intent;
  6. import android.content.res.AssetManager;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.graphics.Canvas;
  10. import android.graphics.Matrix;
  11. import android.graphics.Paint;
  12. import android.graphics.Path;
  13. import android.graphics.PorterDuff;
  14. import android.graphics.PorterDuffXfermode;
  15. import android.graphics.Rect;
  16. import android.graphics.drawable.BitmapDrawable;
  17. import android.graphics.drawable.Drawable;
  18. //import androidx.exifinterface.media.ExifInterface;
  19.  
  20. import android.media.ExifInterface;
  21. import android.media.MediaPlayer;
  22. import android.net.Uri;
  23. import android.os.Bundle;
  24. import android.os.CountDownTimer;
  25. import android.util.Log;
  26. import android.view.View;
  27. import android.widget.Button;
  28. import android.widget.ImageView;
  29. import android.widget.RelativeLayout;
  30. import android.widget.TextView;
  31. import android.widget.Toast;
  32. import java.io.IOException;
  33. import java.io.InputStream;
  34. import java.util.ArrayList;
  35. import java.util.Collections;
  36. import java.util.Random;
  37.  
  38. import static java.lang.Math.abs;
  39. import com.google.android.material.floatingactionbutton.FloatingActionButton;
  40.  
  41. /**
  42. * In this activity user completes the puzzle
  43. *
  44. */
  45. public class PuzzleActivity extends AppCompatActivity {
  46. public static final int TIMER_SEC = 1500;
  47. public static final int WAITING_TIME_MILLISECONDS = 3000;
  48. ArrayList<PuzzlePiece> pieces;
  49.  
  50. //floating button for going to StartActivity
  51. private FloatingActionButton goHome;
  52.  
  53. //picture from camera and gallery------------------------------------------------------
  54. private String mCurrentPhotoPath;
  55. private String mCurrentPhotoUri;
  56. //-------------------------------------------------------------------picture from camera
  57. //timer-----------------------------------
  58. private TextView countTimer;
  59. //------------------------------------timer
  60.  
  61. //popup-----------------------------------------------------
  62. private AlertDialog.Builder dialogBuilder;
  63. private AlertDialog dialog;
  64. private TextView newTimeIsUpText;
  65. private Button newTimeIsUpNext;
  66. //-----------------------------------------------------popup
  67.  
  68. // received results from intent extras
  69. private int piecesNumber;
  70. private int cols;
  71. private int rows;
  72. private int userId;
  73. private String username;
  74.  
  75. //timer----------------------------------------------------------------
  76. int secondsRemaining = TIMER_SEC;//how many seconds left in timer
  77. int time;
  78. //will tick with intervals of 1 sec
  79. final CountDownTimer timer = new CountDownTimer(TIMER_SEC * 1000, 1000) {
  80. /**
  81. * on every timer tick will be displayed the number of seconds passed in TextView
  82. */
  83. @Override
  84. public void onTick(long millisUntilFinished) { //every time the clock ticks
  85. secondsRemaining--;
  86. time = TIMER_SEC - 1 - secondsRemaining;
  87. countTimer.setText(Integer.toString(time) + " secs"); //textView- xml
  88. }
  89.  
  90. /**
  91. * after TIMER_SEC seconds the popup will appear
  92. */
  93. @Override
  94. public void onFinish() {
  95.  
  96. createNewContentDialog(); //creates popup window-------------popup-----------
  97. timer.cancel();
  98. }
  99. };
  100.  
  101. static void shuffleArray(int[] pieces)
  102. {
  103. Random rnd = new Random();
  104. for (int i = pieces.length - 1; i > 0; i--)
  105. {
  106. int index = rnd.nextInt(i + 1);
  107. // Swap
  108. int a = pieces[index];
  109. pieces[index] = pieces[i];
  110. pieces[i] = a;
  111. }
  112. }
  113.  
  114. @Override
  115. protected void onCreate(Bundle savedInstanceState) {
  116. super.onCreate(savedInstanceState);
  117. setContentView(R.layout.activity_puzzle);
  118. setName();
  119.  
  120. //Button to go to the StartActivity
  121. goHome = findViewById(R.id.goHome);
  122. goHome.setOnClickListener(new View.OnClickListener() {
  123. @Override
  124. public void onClick(View v) {
  125. goHome();
  126. }
  127. });
  128.  
  129. //timer-------------------------------------------------------
  130. //counter initializing
  131. countTimer = findViewById(R.id.count_timer);
  132. countTimer.setText("OSecs");
  133. timer.start();//starting timer
  134. //-------------------------------------------------timer
  135.  
  136.  
  137. RelativeLayout layout = findViewById(R.id.layout);
  138. ImageView imageView = findViewById(R.id.imageView);
  139.  
  140. Intent intent = getIntent();
  141. String assetName = intent.getStringExtra("assetName");
  142. //picture from camera and gallery ------------------------------------------------
  143. mCurrentPhotoPath = intent.getStringExtra("mCurrentPhotoPath");
  144. mCurrentPhotoUri = intent.getStringExtra("mCurrentPhotoUri");
  145. //-------------------------------------------------picture from camera
  146.  
  147. // run image related code after the view was laid out
  148. // to have all dimensions calculated
  149. imageView.post(new Runnable() {
  150. @Override
  151. public void run() {
  152. if (assetName != null) {
  153. setPicFromAsset(assetName, imageView);
  154. } else if (mCurrentPhotoPath != null) { //added else if picture from camera
  155. setPicFromPath(mCurrentPhotoPath, imageView);
  156. } else if (mCurrentPhotoUri != null) {
  157. imageView.setImageURI(Uri.parse(mCurrentPhotoUri));
  158. }
  159. pieces = splitImage();
  160. TouchListener touchListener = new TouchListener(PuzzleActivity.this);
  161.  
  162. // shuffle pieces order
  163. //Collections.shuffle(pieces);
  164.  
  165. // --- SWITCH FOR COMPLEXITY LEVELS ---
  166. // positioning and size of pieces-------------------------------------------
  167. int border = 28; //boarder size from xml
  168. double ratio = (double) ((layout.getBottom() - imageView.getBottom()) - 2 * border) / imageView.getHeight();
  169. if (ratio > 1) {
  170. ratio = 1;
  171. }
  172. positioningOfPuzzlePieces(touchListener, border, getRows(), getCols(), ratio, layout, imageView);
  173. }
  174. });
  175. }
  176.  
  177. /**
  178. * positions the resized puzzle pieces in the bottom of the layout
  179. * @param border - taken from xml
  180. * @param getRows - number of rows in the puzzle
  181. * @param getCols - number of columns in the puzzle
  182. * @param ratio - ratio of resizing the draggable puzzle piece relative to puzzle piece
  183. * @param layout - layout of the PuzzleActivity
  184. * @param imageView - imageView of the puzzle picture
  185. */
  186. private void positioningOfPuzzlePieces(TouchListener touchListener, int border, int getRows, int getCols, double ratio, RelativeLayout layout, ImageView imageView) {
  187. int marginLeft;
  188. for (int i = 0; i < getRows; i++) {
  189. marginLeft = border;
  190. for (int j = 0; j < getCols; j++) {
  191. PuzzlePiece piece = pieces.get((i * getCols) + j);
  192. piece.setOnTouchListener(touchListener);
  193. layout.addView(piece);
  194.  
  195. RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) piece.getLayoutParams();
  196.  
  197. int percentageHeight = (int) (piece.pieceHeight * 3 / 4 * ratio);
  198. int percentageWidth = (int) (piece.pieceWidth * 3 / 4 * ratio);
  199. lParams.height = percentageHeight;
  200. lParams.width = percentageWidth;
  201. lParams.leftMargin = marginLeft;
  202. lParams.topMargin = imageView.getBottom() + i * (int) ((layout.getHeight() - imageView.getBottom() - border) / getRows) + border;
  203. lParams.bottomMargin = border;
  204. piece.setLayoutParams(lParams);
  205.  
  206. marginLeft += (int) (imageView.getWidth() / getCols) + border / (getCols - 1);
  207. }
  208. }
  209. }
  210. //--------------------------------------------------------------------positioning and size of pieces
  211.  
  212.  
  213. // --- methods to get number of pieces, columns, rows and id from intent extras
  214. private int getPiecesNumber() {
  215. Intent getComplexityFromGridView = getIntent();
  216. piecesNumber = getComplexityFromGridView.getIntExtra("numOfPiecesToPuz", 0);
  217. return piecesNumber;
  218. }
  219.  
  220. private int getCols() {
  221. Intent getComplexityFromGridView = getIntent();
  222. cols = getComplexityFromGridView.getIntExtra("numOfColumnsToPuz", 0);
  223. return cols;
  224. }
  225.  
  226. private int getRows() {
  227. Intent getComplexityFromGridView = getIntent();
  228. rows = getComplexityFromGridView.getIntExtra("numOfRowsToPuz", 0);
  229. return rows;
  230. }
  231.  
  232. private int getUserId() {
  233. Intent getComplexityFromGridView = getIntent();
  234. userId = getComplexityFromGridView.getIntExtra("userId", 0);
  235. return userId;
  236. }
  237.  
  238. private String getUsername() {
  239. Intent getComplexityFromGridView = getIntent();
  240. username = getComplexityFromGridView.getStringExtra("username");
  241. return username;
  242. }
  243.  
  244.  
  245. /**
  246. * when the game is over timer stops, the cheer sound is played
  247. * and the screen freezes for 3 seconds
  248. */
  249. public void checkGameOver() {
  250. if (isGameOver()) {
  251. timer.cancel(); //stops the timer
  252. // --- sound on finish
  253. MediaPlayer sound = MediaPlayer.create(this, R.raw.cheer);
  254. sound.start();
  255. stopOnCompletion(sound);
  256. // --- /sound
  257.  
  258. //3 sec waiting timer to freeze the screen--------------------------------------------
  259. CountDownTimer pauseTimer = new CountDownTimer(WAITING_TIME_MILLISECONDS, 1000) {
  260. @Override
  261. public void onTick(long millisUntilFinished) {
  262.  
  263. }
  264.  
  265. /**
  266. * when time is up, passing extras to the next activity - Score activity
  267. */
  268. @Override
  269. public void onFinish() {
  270.  
  271. piecesNumber = getPiecesNumber();
  272. userId = getUserId();
  273. username = getUsername();
  274. Log.w(PuzzleActivity.class.getName(), "Received level is " + piecesNumber);
  275. Log.w(PuzzleActivity.class.getName(), "Received id is " + userId);
  276.  
  277. Intent countIntent = new Intent(getApplicationContext(), ScoreActivity.class);
  278. countIntent.putExtra("time", time);//want to transfer final textview with seconds
  279. Log.i(PuzzleActivity.class.getName(),"Timer result sent from puzzle activity is " + time);
  280. countIntent.putExtra("userId", userId);
  281. countIntent.putExtra("level", piecesNumber);
  282. countIntent.putExtra("username", username);
  283. Log.i(PuzzleActivity.class.getName(), "Sent level is " + piecesNumber);
  284. Log.i(PuzzleActivity.class.getName(), "Sent id is " + userId);
  285. Log.i(PuzzleActivity.class.getName(), "Sent username is " + username);
  286. startActivity(countIntent);//transfers to ScoreActivity
  287.  
  288. }
  289. };
  290. pauseTimer.start();
  291. //---------------------------------------------------------------3 sec waiting timer
  292.  
  293. }
  294. }
  295.  
  296.  
  297. private boolean isGameOver() {
  298. for (PuzzlePiece piece : pieces) {
  299. if (piece.canMove) {
  300. return false;
  301. }
  302. }
  303.  
  304. return true;
  305. }
  306.  
  307. private void setPicFromAsset(String assetName, ImageView imageView) {
  308. // Get the dimensions of the View
  309. int targetW = imageView.getWidth();
  310. int targetH = imageView.getHeight();
  311.  
  312. AssetManager am = getAssets();
  313. try {
  314. InputStream is = am.open("img/" + assetName);
  315. // Get the dimensions of the bitmap
  316. BitmapFactory.Options bmOptions = new BitmapFactory.Options();
  317. bmOptions.inJustDecodeBounds = true;
  318. BitmapFactory.decodeStream(is, new Rect(-1, -1, -1, -1), bmOptions);
  319. int photoW = bmOptions.outWidth;
  320. int photoH = bmOptions.outHeight;
  321.  
  322. // Determine how much to scale down the image
  323. int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
  324.  
  325. is.reset();
  326.  
  327. // Decode the image file into a Bitmap sized to fill the View
  328. bmOptions.inJustDecodeBounds = false;
  329. bmOptions.inSampleSize = scaleFactor;
  330.  
  331. Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(-1, -1, -1, -1), bmOptions);
  332. imageView.setImageBitmap(bitmap);
  333. } catch (IOException e) {
  334. e.printStackTrace();
  335. Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
  336. }
  337. }
  338.  
  339. private ArrayList<PuzzlePiece> splitImage() {
  340.  
  341. // --- using separate methods
  342. piecesNumber = getPiecesNumber();
  343. cols = getCols();
  344. rows = getRows();
  345. // ---
  346.  
  347. ImageView imageView = findViewById(R.id.imageView);
  348.  
  349. ArrayList<PuzzlePiece> pieces = new ArrayList<>(piecesNumber);
  350.  
  351. // Get the scaled bitmap of the source image
  352. BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
  353. Bitmap bitmap = drawable.getBitmap();
  354.  
  355. int[] dimensions = getBitmapPositionInsideImageView(imageView);
  356. int scaledBitmapLeft = dimensions[0];
  357. int scaledBitmapTop = dimensions[1];
  358. int scaledBitmapWidth = dimensions[2];
  359. int scaledBitmapHeight = dimensions[3];
  360.  
  361. int croppedImageWidth = scaledBitmapWidth - 2 * abs(scaledBitmapLeft);
  362. int croppedImageHeight = scaledBitmapHeight - 2 * abs(scaledBitmapTop);
  363.  
  364. Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledBitmapWidth, scaledBitmapHeight, true);//calls createBitmap(Bitmap source, int 0, int 0, int width, int height), matrix- m, filter , The result will be the same as bitmap but with sizes scaledBitmapWidth, scaledBitmapHeight
  365. Bitmap croppedBitmap = Bitmap.createBitmap(scaledBitmap, abs(scaledBitmapLeft), abs(scaledBitmapTop), croppedImageWidth, croppedImageHeight);//creates bitmap from existing bitmap- scaledbitmap, x, y, width, height
  366.  
  367. // Calculate the with and height of the pieces
  368. int pieceWidth = croppedImageWidth / cols;
  369. int pieceHeight = croppedImageHeight / rows;
  370.  
  371. // Create each bitmap piece and add it to the resulting array
  372. int yCoord = 0;
  373. for (int row = 0; row < rows; row++) {
  374. int xCoord = 0;
  375. for (int col = 0; col < cols; col++) {
  376. // calculate offset for each piece
  377. int offsetX = 0;
  378. int offsetY = 0;
  379. if (col > 0) {
  380. offsetX = pieceWidth / 3;
  381. }
  382. if (row > 0) {
  383. offsetY = pieceHeight / 3;
  384. }
  385.  
  386. // apply the offset to each piece
  387. Bitmap pieceBitmap = Bitmap.createBitmap(croppedBitmap, xCoord - offsetX, yCoord - offsetY, pieceWidth + offsetX, pieceHeight + offsetY);
  388. PuzzlePiece piece = new PuzzlePiece(getApplicationContext());
  389. piece.setImageBitmap(pieceBitmap);
  390. piece.xCoord = xCoord - offsetX + imageView.getLeft();
  391. piece.yCoord = yCoord - offsetY + imageView.getTop();
  392. piece.pieceWidth = pieceWidth + offsetX;
  393. piece.pieceHeight = pieceHeight + offsetY;
  394.  
  395. // this bitmap will hold our final puzzle piece image
  396. Bitmap puzzlePiece = Bitmap.createBitmap(pieceWidth + offsetX, pieceHeight + offsetY, Bitmap.Config.ARGB_8888);
  397.  
  398. // draw path
  399. int bumpSize = pieceHeight / 4;
  400. Canvas canvas = new Canvas(puzzlePiece);
  401. Path path = new Path();
  402. path.moveTo(offsetX, offsetY);
  403. if (row == 0) {
  404. // top side piece
  405. path.lineTo(pieceBitmap.getWidth(), offsetY);
  406. } else {
  407. // top bump
  408. path.lineTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 3.0f, offsetY);
  409. path.cubicTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 6.0f, offsetY - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 6.0f * 5, offsetY - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 3.0f * 2, offsetY);
  410. path.lineTo(pieceBitmap.getWidth(), offsetY);
  411. }
  412.  
  413. if (col == cols - 1) {
  414. // right side piece
  415. path.lineTo(pieceBitmap.getWidth(), pieceBitmap.getHeight());
  416. } else {
  417. // right bump
  418. path.lineTo(pieceBitmap.getWidth(), offsetY + (pieceBitmap.getHeight() - offsetY) / 3.0f);
  419. path.cubicTo(pieceBitmap.getWidth() - bumpSize, offsetY + (pieceBitmap.getHeight() - offsetY) / 6.0f, pieceBitmap.getWidth() - bumpSize, offsetY + (pieceBitmap.getHeight() - offsetY) / 6.0f * 5, pieceBitmap.getWidth(), offsetY + (pieceBitmap.getHeight() - offsetY) / 3.0f * 2);
  420. path.lineTo(pieceBitmap.getWidth(), pieceBitmap.getHeight());
  421. }
  422.  
  423. if (row == rows - 1) {
  424. // bottom side piece
  425. path.lineTo(offsetX, pieceBitmap.getHeight());
  426. } else {
  427. // bottom bump
  428. path.lineTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 3.0f * 2, pieceBitmap.getHeight());
  429. path.cubicTo(offsetX + (pieceBitmap.getWidth() - offsetX) / 6.0f * 5, pieceBitmap.getHeight() - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 6.0f, pieceBitmap.getHeight() - bumpSize, offsetX + (pieceBitmap.getWidth() - offsetX) / 3.0f, pieceBitmap.getHeight());
  430. path.lineTo(offsetX, pieceBitmap.getHeight());
  431. }
  432.  
  433. if (col == 0) {
  434. // left side piece
  435. path.close();
  436. } else {
  437. // left bump
  438. path.lineTo(offsetX, offsetY + (pieceBitmap.getHeight() - offsetY) / 3.0f * 2);
  439. path.cubicTo(offsetX - bumpSize, offsetY + (pieceBitmap.getHeight() - offsetY) / 6.0f * 5, offsetX - bumpSize, offsetY + (pieceBitmap.getHeight() - offsetY) / 6.0f, offsetX, offsetY + (pieceBitmap.getHeight() - offsetY) / 3.0f);
  440. path.close();
  441. }
  442.  
  443. // mask the piece
  444. Paint paint = new Paint();
  445. paint.setColor(0XFF000000);
  446. paint.setStyle(Paint.Style.FILL);
  447.  
  448. canvas.drawPath(path, paint);
  449. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  450. canvas.drawBitmap(pieceBitmap, 0, 0, paint);
  451.  
  452. // draw a white border
  453. Paint border = new Paint();
  454. border.setColor(0X80FFFFFF);
  455. border.setStyle(Paint.Style.STROKE);
  456. border.setStrokeWidth(8.0f);
  457. canvas.drawPath(path, border);
  458.  
  459. // draw a black border
  460. border = new Paint();
  461. border.setColor(0X80000000);
  462. border.setStyle(Paint.Style.STROKE);
  463. border.setStrokeWidth(3.0f);
  464. canvas.drawPath(path, border);
  465.  
  466. // set the resulting bitmap to the piece
  467. piece.setImageBitmap(puzzlePiece);
  468.  
  469. pieces.add(piece);
  470. xCoord += pieceWidth;
  471. }
  472. yCoord += pieceHeight;
  473. }
  474.  
  475. return pieces;
  476. }
  477.  
  478. private int[] getBitmapPositionInsideImageView(ImageView imageView) {
  479. int[] ret = new int[4];
  480.  
  481. if (imageView == null || imageView.getDrawable() == null)
  482. return ret;
  483.  
  484. // Get image dimensions
  485. // Get image matrix values and place them in an array
  486. float[] f = new float[9];
  487. imageView.getImageMatrix().getValues(f);
  488.  
  489. // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
  490. final float scaleX = f[Matrix.MSCALE_X];
  491. final float scaleY = f[Matrix.MSCALE_Y];
  492.  
  493. // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
  494. final Drawable d = imageView.getDrawable();
  495. final int origW = d.getIntrinsicWidth();
  496. final int origH = d.getIntrinsicHeight();
  497.  
  498. // Calculate the actual dimensions
  499. final int actW = Math.round(origW * scaleX);
  500. final int actH = Math.round(origH * scaleY);
  501.  
  502. ret[2] = actW;
  503. ret[3] = actH;
  504.  
  505. // Get image position
  506. // We assume that the image is centered into ImageView
  507. int imgViewW = imageView.getWidth();
  508. int imgViewH = imageView.getHeight();
  509.  
  510. int top = (int) (imgViewH - actH) / 2;
  511. int left = (int) (imgViewW - actW) / 2;
  512.  
  513. ret[0] = left;
  514. ret[1] = top;
  515.  
  516. return ret;
  517. }
  518.  
  519. //popup------------------------
  520. public void createNewContentDialog() {
  521. dialogBuilder = new AlertDialog.Builder(this);
  522. final View timeIsUpPopupView = getLayoutInflater().inflate(R.layout.activity_time_is_up, null);
  523. newTimeIsUpText = (TextView) timeIsUpPopupView.findViewById(R.id.timeIsUpText);
  524. newTimeIsUpNext = (Button) timeIsUpPopupView.findViewById(R.id.timeIsUpNext);
  525.  
  526. dialogBuilder.setView(timeIsUpPopupView);
  527. dialog = dialogBuilder.create();
  528. dialog.show();
  529.  
  530. //pushing Next Button from pop-Up
  531. newTimeIsUpNext.setOnClickListener(new View.OnClickListener() {
  532.  
  533. @Override
  534. public void onClick(View v) {
  535. //define next button
  536. int userIdFromPopup = getUserId();
  537. String usernameFromPopup = getUsername();
  538. Intent intent = new Intent(getApplicationContext(), ComplexityActivity.class);
  539. intent.putExtra("userIdFromPopup", userIdFromPopup);
  540. intent.putExtra("usernameFromPopup", usernameFromPopup);
  541. Log.i(PuzzleActivity.class.getName(), "User id " + userIdFromPopup + " from time is up popup was sent to complexity");
  542. startActivity(intent);
  543. }
  544. });
  545. }
  546. //------------------------popup
  547.  
  548. //picture from camera---------------------------------------------------------------------------
  549. private void setPicFromPath(String mCurrentPhotoPath, ImageView imageView) {
  550. // Get the dimensions of the View
  551. int targetW = imageView.getWidth();
  552. int targetH = imageView.getHeight();
  553.  
  554. // Get the dimensions of the bitmap
  555. BitmapFactory.Options bmOptions = new BitmapFactory.Options();
  556. bmOptions.inJustDecodeBounds = true;
  557. BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); //gets file from file, using path
  558. int photoW = bmOptions.outWidth;
  559. int photoH = bmOptions.outHeight;
  560.  
  561. // Determine how much to scale down the image
  562. int scaleFactor = Math.min(photoW / targetW, photoH / targetH);
  563.  
  564. // Decode the image file into a Bitmap sized to fill the View
  565. bmOptions.inJustDecodeBounds = false;
  566. bmOptions.inSampleSize = scaleFactor;
  567.  
  568.  
  569. Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
  570. Bitmap rotatedBitmap = bitmap;//just for rotating
  571.  
  572. // rotate bitmap if needed
  573. try {
  574. ExifInterface ei = new ExifInterface(mCurrentPhotoPath);
  575. int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
  576. switch (orientation) {
  577. case ExifInterface.ORIENTATION_ROTATE_90:
  578. rotatedBitmap = rotateImage(bitmap, 90);
  579. break;
  580. case ExifInterface.ORIENTATION_ROTATE_180:
  581. rotatedBitmap = rotateImage(bitmap, 180);
  582. break;
  583. case ExifInterface.ORIENTATION_ROTATE_270:
  584. rotatedBitmap = rotateImage(bitmap, 270);
  585. break;
  586. }
  587. } catch (IOException e) {
  588. Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
  589. }
  590.  
  591. imageView.setImageBitmap(rotatedBitmap);
  592. }
  593.  
  594. //rotates bitmap to angle degrees
  595. public static Bitmap rotateImage(Bitmap source, float angle) {
  596. Matrix matrix = new Matrix();
  597. matrix.postRotate(angle);
  598. return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
  599. matrix, true);
  600. }
  601.  
  602.  
  603. //---------------------------------------------------------------------------picture from camera
  604.  
  605. //Method to go to the StartActivity
  606. public void goHome() {
  607. Intent intent = new Intent(this, StartActivity.class);
  608. startActivity(intent);
  609. }
  610.  
  611. // show username on screen
  612. public void setName() {
  613. TextView name = (TextView)findViewById(R.id.username_puzzle);
  614. name.setText(getUsername());
  615. }
  616.  
  617. // stop media player after playing sound for finishing puzzle
  618. public void stopOnCompletion(MediaPlayer mp) {
  619. mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
  620. @Override
  621. public void onCompletion(MediaPlayer mp) {
  622. mp.release();
  623. mp = null;
  624. }
  625. });
  626. }
  627. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement