Thursday, February 16, 2017

android - Picasso image resize example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#bed3da"
    >
    <Button
        android:id="@+id/btn_do"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Load Resized Assets Image"
        android:layout_margin="25dp"
        android:layout_gravity="top|center_horizontal"
        />
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/btn_do"
        app:layout_anchorGravity="bottom"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_margin="15dp"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private static final String TAG = MainActivity.class.getSimpleName();

    private CoordinatorLayout mCLayout;
    private Button mButtonDo;
    private ImageView mImageView;
    String imageFilePath = "file:///android_asset/images/image_mushroom.jpeg";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mButtonDo = (Button) findViewById(R.id.btn_do);
        mImageView = (ImageView) findViewById(R.id.iv);

        // Set a click listener for button widget
        mButtonDo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Initialize a new Picasso Builder instance
                Picasso picasso = new Picasso.Builder(mContext).listener(new Picasso.Listener() {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                        // Do something here
                        Snackbar.make(mCLayout,"Load failed.",Snackbar.LENGTH_LONG).show();
                    }
                }).build();

                // Load the image into image view from assets folder
                picasso.load(imageFilePath)
                        .error(R.drawable.ic_error_black_36dp) // On error image
                        .placeholder(R.drawable.ic_photo_black_36dp) // Place holder image
                        .resize(800,400) // Width and Height
                        .centerCrop() // Image scaling type
                        .onlyScaleDown() // Only resize image when it is larger than provided dimension
                        //.fit() // fil cannot be use with resize and onlyScaleDown
                        .into(mImageView, new Callback() {
                            @Override
                            public void onSuccess() {
                                // Do something
                                Log.d(TAG,"Success image loading...");
                                Snackbar.make(mCLayout,"Success.",Snackbar.LENGTH_LONG).show();
                            }

                            @Override
                            public void onError() {
                                // Do something
                                Log.d(TAG,"Error!!!");
                                Snackbar.make(mCLayout,"Error.",Snackbar.LENGTH_LONG).show();
                            }
                        });
            }
        });
    }
}
build.gradle(app) dependencies

compile 'com.squareup.picasso:picasso:2.5.2'