如何在点击时调整从instagram检索到的位图图像的大小

时间:2014-09-05 16:06:34

标签: android image bitmap resize instagram

所以我想在这里做的是我目前有一个位图图像数组,我从一个instagram厨房检索,我想要做的是将这些图像放大到全屏,然后返回到原始数组再次点击(我明白这可能意味着检索" standard_resolution"以及" low_resolution"

主要活动

package net.londatiga.android.example;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import net.londatiga.android.instagram.Instagram;
import net.londatiga.android.instagram.InstagramRequest;
import net.londatiga.android.instagram.InstagramSession;
import net.londatiga.android.instagram.InstagramUser;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;


public class MainActivity extends Activity {
private InstagramSession mInstagramSession;
private Instagram mInstagram;

private ProgressBar mLoadingPb;
private GridView mGridView;

private static final String CLIENT_ID = "83549f9eb76f4a5b90daf6e4e14da107";
private static final String CLIENT_SECRET = "6df26b0c8f664323a07126bfe8511651";
private static final String REDIRECT_URI = "http://www.yahoo.com";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mInstagram          = new Instagram(this, CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);

    mInstagramSession   = mInstagram.getSession();

    if (mInstagramSession.isActive()) {
        setContentView(R.layout.activity_user);

        InstagramUser instagramUser = mInstagramSession.getUser();

        mLoadingPb  = (ProgressBar) findViewById(R.id.pb_loading);
        mGridView   = (GridView) findViewById(R.id.gridView);

        ((TextView) findViewById(R.id.tv_name)).setText(instagramUser.fullName);
        ((TextView) findViewById(R.id.tv_username)).setText(instagramUser.username);

        ((Button) findViewById(R.id.btn_logout)).setOnClickListener(new View.OnClickListener() {                
            @Override
            public void onClick(View arg0) {
                mInstagramSession.reset();

                startActivity(new Intent(MainActivity.this, MainActivity.class));

                finish();
            }
        });

        ImageView userIv = (ImageView) findViewById(R.id.iv_user);

        DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_user)
                .showImageForEmptyUri(R.drawable.ic_user)
                .showImageOnFail(R.drawable.ic_user)
                .cacheInMemory(true)
                .cacheOnDisc(false)
                .considerExifParams(true)
                .build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)                                               
                .writeDebugLogs()
                .defaultDisplayImageOptions(displayOptions)             
                .build();

        ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);

        AnimateFirstDisplayListener animate  = new AnimateFirstDisplayListener();

        imageLoader.displayImage(instagramUser.profilPicture, userIv, animate);

        new DownloadTask().execute();

    } else {
        setContentView(R.layout.activity_main);

        ((Button) findViewById(R.id.btn_connect)).setOnClickListener(new View.OnClickListener() {           
            @Override
            public void onClick(View arg0) {                    
                mInstagram.authorize(mAuthListener);    
            }
        });
    }
}

private void showToast(String text) {
    Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}

private Instagram.InstagramAuthListener mAuthListener = new Instagram.InstagramAuthListener() {         
    @Override
    public void onSuccess(InstagramUser user) {
        finish();

        startActivity(new Intent(MainActivity.this, MainActivity.class));
    }

    @Override
    public void onError(String error) {     
        showToast(error);
    }

    @Override
    public void onCancel() {
        showToast("OK. Maybe later?");

    }
};

public static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

    static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        if (loadedImage != null) {
            ImageView imageView = (ImageView) view;
            boolean firstDisplay = !displayedImages.contains(imageUri);
            if (firstDisplay) {
                FadeInBitmapDisplayer.animate(imageView, 500);
                displayedImages.add(imageUri);
            }
        }
    }
}

public class DownloadTask extends AsyncTask<URL, Integer, Long> {
    ArrayList<String> photoList;

    protected void onCancelled() {

    }

    protected void onPreExecute() {

    }

    protected Long doInBackground(URL... urls) {         
        long result = 0;

        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>(1);

            params.add(new BasicNameValuePair("count", "20"));

            InstagramRequest request = new InstagramRequest(mInstagramSession.getAccessToken());
            String response          = request.createRequest("GET", "/users/21846697/media/recent", params);

            if (!response.equals("")) {
                JSONObject jsonObj  = (JSONObject) new JSONTokener(response).nextValue();                   
                JSONArray jsonData  = jsonObj.getJSONArray("data");

                int length = jsonData.length();

                if (length > 0) {
                    photoList = new ArrayList<String>();

                    for (int i = 0; i < length; i++) {
                        JSONObject jsonPhoto = jsonData.getJSONObject(i).getJSONObject("images").getJSONObject("low_resolution");

                        photoList.add(jsonPhoto.getString("url"));
                    }
                }
            }
        } catch (Exception e) { 
            e.printStackTrace();
        }

        return result;
    }

    protected void onProgressUpdate(Integer... progress) {                  
    }

    protected void onPostExecute(Long result) {
        mLoadingPb.setVisibility(View.GONE);

        if (photoList == null) {
            Toast.makeText(getApplicationContext(), "No Photos Available", Toast.LENGTH_LONG).show();
        } else {
            DisplayMetrics dm = new DisplayMetrics();

            getWindowManager().getDefaultDisplay().getMetrics(dm);

            int width   = (int) Math.ceil((double) dm.widthPixels / 2);
            width=width-50;
            int height  = width;

            PhotoListAdapter adapter = new PhotoListAdapter(MainActivity.this);

            adapter.setData(photoList);
            adapter.setLayoutParam(width, height);

            mGridView.setAdapter(adapter);
        }
    }                
}
}

PhotoListAdapter

package net.londatiga.android.example;

import net.londatiga.android.example.R;
import net.londatiga.android.example.MainActivity.AnimateFirstDisplayListener;

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import android.content.Context;

public class PhotoListAdapter extends BaseAdapter {
private Context mContext;

private ImageLoader mImageLoader;
private AnimateFirstDisplayListener mAnimator;

private ArrayList<String> mPhotoList;

private int mWidth;
private int mHeight;

public PhotoListAdapter(Context context) {
    mContext = context;

    DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.instagram_logo)
            .showImageForEmptyUri(R.drawable.instagram_logo)
            .showImageOnFail(R.drawable.instagram_logo)
            .cacheInMemory(true)
            .cacheOnDisc(false)
            .considerExifParams(true)
            .build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)                                            
            .writeDebugLogs()
            .defaultDisplayImageOptions(displayOptions)             
            .build();

    mImageLoader = ImageLoader.getInstance();
    mImageLoader.init(config);

    mAnimator  = new AnimateFirstDisplayListener();
}

public void setData(ArrayList<String> data) {
    mPhotoList = data;
}

public void setLayoutParam(int width, int height) {
    mWidth  = width;
    mHeight = height;
}

@Override
public int getCount() {
    return (mPhotoList == null) ? 0 : mPhotoList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageIv;

    if (convertView == null) {
        imageIv = new ImageView(mContext);

        imageIv.setLayoutParams(new GridView.LayoutParams(mWidth, mHeight));
        imageIv.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageIv.setPadding(0, 0, 0, 0); 
    } else {
        imageIv = (ImageView) convertView;
    }

    mImageLoader.displayImage(mPhotoList.get(position), imageIv, mAnimator);

    return imageIv;
}
}

0 个答案:

没有答案