Advertisement
Guest User

Untitled

a guest
Mar 15th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. package com.stfo.app.breakingeven;
  2.  
  3. import android.content.res.AssetManager;
  4. import android.content.res.Resources;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.graphics.Typeface;
  8. import android.media.ThumbnailUtils;
  9. import android.os.AsyncTask;
  10. import android.os.Bundle;
  11. import android.support.v7.app.ActionBarActivity;
  12. import android.util.DisplayMetrics;
  13. import android.util.Log;
  14. import android.widget.ImageView;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import java.lang.ref.WeakReference;
  19.  
  20. /**
  21. * Created by kartik on 28/02/15.
  22. */
  23. public class HomeScreen extends ActionBarActivity{
  24.  
  25. ImageView iV;
  26. int height,width;
  27. TextView tv;
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.adventure_fragment);
  33. iV = (ImageView) findViewById(R.id.imageView_adventure_home_screen_fragment);
  34. tv = (TextView) findViewById(R.id.textView_adventure_name_home_screen_fragment);
  35. Typeface typeface = Typeface.createFromAsset(getAssets(),"RobotoCondensed-Bold.ttf");
  36. tv.setTypeface(typeface);
  37. tv.setText("demooo");
  38.  
  39. DisplayMetrics disp = getApplicationContext().getResources().getDisplayMetrics();
  40. int density = disp.densityDpi;
  41. width = disp.widthPixels;
  42. height = (int)((float)(200.0/160.0) * (float)density);
  43.  
  44. Bitmap b =decodeSampledBitmapFromResource(getResources(),R.drawable.shake,height);
  45. loadBitmap(R.drawable.shake,iV);
  46.  
  47. }
  48.  
  49. public void loadBitmap(int resId, ImageView imageView) {
  50. BitmapWorkerTask task = new BitmapWorkerTask(imageView);
  51. task.execute(resId);
  52. }
  53.  
  54. public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
  55. int reqHeight) {
  56.  
  57. // First decode with inJustDecodeBounds=true to check dimensions
  58. final BitmapFactory.Options options = new BitmapFactory.Options();
  59. options.inJustDecodeBounds = true;
  60. BitmapFactory.decodeResource(res, resId, options);
  61.  
  62. // Calculate inSampleSize
  63. options.inSampleSize = calculateInSampleSize(options , reqHeight);
  64.  
  65. // Decode bitmap with inSampleSize set
  66. options.inJustDecodeBounds = false;
  67. return BitmapFactory.decodeResource(res, resId, options);
  68. }
  69.  
  70. public static int calculateInSampleSize(BitmapFactory.Options options, int reqHeight)
  71. {
  72. final int height = options.outHeight;
  73. int inSampleSize = 1;
  74.  
  75. if (height > reqHeight ) {
  76.  
  77. final int halfHeight = height / 2;
  78.  
  79. // Calculate the largest inSampleSize value that is a power of 2 and keeps both
  80. // height and width larger than the requested height and width.
  81. while ((halfHeight / inSampleSize) > reqHeight) {
  82. inSampleSize *= 2;
  83. }
  84. }
  85.  
  86. return inSampleSize;
  87. }
  88.  
  89. class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
  90. private final WeakReference<ImageView> imageViewReference;
  91. private int data = 0;
  92.  
  93. public BitmapWorkerTask(ImageView imageView) {
  94. // Use a WeakReference to ensure the ImageView can be garbage collected
  95. imageViewReference = new WeakReference<ImageView>(imageView);
  96. }
  97.  
  98. // Decode image in background.
  99. @Override
  100. protected Bitmap doInBackground(Integer... params) {
  101. data = params[0];
  102. return decodeSampledBitmapFromResource(getResources(), data, height);
  103. }
  104.  
  105. // Once complete, see if ImageView is still around and set bitmap.
  106. @Override
  107. protected void onPostExecute(Bitmap bitmap) {
  108. if (imageViewReference != null && bitmap != null) {
  109. final ImageView imageView = imageViewReference.get();
  110. if (imageView != null) {
  111. imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,width,height));
  112. }
  113. }
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement