Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.stfo.app.breakingeven;
- import android.content.res.AssetManager;
- import android.content.res.Resources;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Typeface;
- import android.media.ThumbnailUtils;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.lang.ref.WeakReference;
- /**
- * Created by kartik on 28/02/15.
- */
- public class HomeScreen extends ActionBarActivity{
- ImageView iV;
- int height,width;
- TextView tv;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.adventure_fragment);
- iV = (ImageView) findViewById(R.id.imageView_adventure_home_screen_fragment);
- tv = (TextView) findViewById(R.id.textView_adventure_name_home_screen_fragment);
- Typeface typeface = Typeface.createFromAsset(getAssets(),"RobotoCondensed-Bold.ttf");
- tv.setTypeface(typeface);
- tv.setText("demooo");
- DisplayMetrics disp = getApplicationContext().getResources().getDisplayMetrics();
- int density = disp.densityDpi;
- width = disp.widthPixels;
- height = (int)((float)(200.0/160.0) * (float)density);
- Bitmap b =decodeSampledBitmapFromResource(getResources(),R.drawable.shake,height);
- loadBitmap(R.drawable.shake,iV);
- }
- public void loadBitmap(int resId, ImageView imageView) {
- BitmapWorkerTask task = new BitmapWorkerTask(imageView);
- task.execute(resId);
- }
- public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
- int reqHeight) {
- // First decode with inJustDecodeBounds=true to check dimensions
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeResource(res, resId, options);
- // Calculate inSampleSize
- options.inSampleSize = calculateInSampleSize(options , reqHeight);
- // Decode bitmap with inSampleSize set
- options.inJustDecodeBounds = false;
- return BitmapFactory.decodeResource(res, resId, options);
- }
- public static int calculateInSampleSize(BitmapFactory.Options options, int reqHeight)
- {
- final int height = options.outHeight;
- int inSampleSize = 1;
- if (height > reqHeight ) {
- final int halfHeight = height / 2;
- // Calculate the largest inSampleSize value that is a power of 2 and keeps both
- // height and width larger than the requested height and width.
- while ((halfHeight / inSampleSize) > reqHeight) {
- inSampleSize *= 2;
- }
- }
- return inSampleSize;
- }
- class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
- private final WeakReference<ImageView> imageViewReference;
- private int data = 0;
- public BitmapWorkerTask(ImageView imageView) {
- // Use a WeakReference to ensure the ImageView can be garbage collected
- imageViewReference = new WeakReference<ImageView>(imageView);
- }
- // Decode image in background.
- @Override
- protected Bitmap doInBackground(Integer... params) {
- data = params[0];
- return decodeSampledBitmapFromResource(getResources(), data, height);
- }
- // Once complete, see if ImageView is still around and set bitmap.
- @Override
- protected void onPostExecute(Bitmap bitmap) {
- if (imageViewReference != null && bitmap != null) {
- final ImageView imageView = imageViewReference.get();
- if (imageView != null) {
- imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,width,height));
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement