Fixed kabouzeid/phonograph-issue-tracker#4, when setting an album cover in the tag editor, the image will be cropped instead of stretched now.
This commit is contained in:
parent
55f8800f89
commit
9b2aaea2c9
3 changed files with 62 additions and 48 deletions
|
|
@ -169,9 +169,9 @@ public abstract class AbsTagEditorActivity extends AbsBaseActivity {
|
|||
}
|
||||
|
||||
private void startImagePicker() {
|
||||
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
|
||||
photoPickerIntent.setType("image/*");
|
||||
startActivityForResult(photoPickerIntent, REQUEST_CODE_SELECT_IMAGE);
|
||||
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||
intent.setType("image/*");
|
||||
startActivityForResult(Intent.createChooser(intent, getString(R.string.pick_from_local_storage)), REQUEST_CODE_SELECT_IMAGE);
|
||||
}
|
||||
|
||||
protected abstract void loadCurrentImage();
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@ import com.kabouzeid.gramophone.lastfm.rest.LastFMRestClient;
|
|||
import com.kabouzeid.gramophone.lastfm.rest.model.LastFmAlbum;
|
||||
import com.kabouzeid.gramophone.loader.AlbumSongLoader;
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
import com.kabouzeid.gramophone.util.ImageUtil;
|
||||
import com.kabouzeid.gramophone.util.LastFMUtil;
|
||||
import com.kabouzeid.gramophone.util.MusicUtil;
|
||||
import com.kabouzeid.gramophone.util.Util;
|
||||
|
|
@ -102,25 +101,14 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
|||
}
|
||||
lastFMRestClient.getApiService().getAlbumInfo(albumTitleStr, albumArtistNameStr).enqueue(new Callback<LastFmAlbum>() {
|
||||
@Override
|
||||
public void onResponse(Response<LastFmAlbum> response) {
|
||||
public void onResponse(final Response<LastFmAlbum> response) {
|
||||
LastFmAlbum lastFmAlbum = response.body();
|
||||
if (lastFmAlbum.getAlbum() != null) {
|
||||
final int smallerScreenSize = Util.getSmallerScreenSize(AlbumTagEditorActivity.this);
|
||||
ImageLoader.getInstance().loadImage(LastFMUtil.getLargestAlbumImageUrl(lastFmAlbum.getAlbum().getImage()),
|
||||
new DisplayImageOptions.Builder()
|
||||
.preProcessor(new BitmapProcessor() {
|
||||
@Override
|
||||
public Bitmap process(Bitmap bitmap) {
|
||||
return ImageUtil.getResizedBitmap(bitmap, smallerScreenSize, smallerScreenSize, true);
|
||||
}
|
||||
})
|
||||
.preProcessor(albumCoverProcessor)
|
||||
.build(),
|
||||
new SimpleImageLoadingListener() {
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
toastLoadingFailed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
if (loadedImage == null) {
|
||||
|
|
@ -133,6 +121,11 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
|||
dataChanged();
|
||||
setResult(RESULT_OK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
handleFailReason(failReason);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
toastLoadingFailed();
|
||||
|
|
@ -206,23 +199,11 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
|||
|
||||
@Override
|
||||
protected void loadImageFromFile(@NonNull final Uri selectedFileUri) {
|
||||
final int smallerScreenSize = Util.getSmallerScreenSize(AlbumTagEditorActivity.this);
|
||||
ImageLoader.getInstance().loadImage(selectedFileUri.toString(),
|
||||
new DisplayImageOptions.Builder()
|
||||
.preProcessor(new BitmapProcessor() {
|
||||
@Override
|
||||
public Bitmap process(Bitmap bitmap) {
|
||||
return ImageUtil.getResizedBitmap(bitmap, smallerScreenSize, smallerScreenSize, true);
|
||||
}
|
||||
})
|
||||
.preProcessor(albumCoverProcessor)
|
||||
.build(),
|
||||
new SimpleImageLoadingListener() {
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
Toast.makeText(AlbumTagEditorActivity.this,
|
||||
R.string.could_not_download_album_cover, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
albumArtBitmap = loadedImage;
|
||||
|
|
@ -231,6 +212,11 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
|||
dataChanged();
|
||||
setResult(RESULT_OK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||
handleFailReason(failReason);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -248,4 +234,50 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
|||
public void afterTextChanged(Editable s) {
|
||||
dataChanged();
|
||||
}
|
||||
|
||||
private BitmapProcessor albumCoverProcessor = new BitmapProcessor() {
|
||||
@Override
|
||||
public Bitmap process(Bitmap bitmap) {
|
||||
return getResizedAlbumCover(bitmap, Util.getSmallerScreenSize(AlbumTagEditorActivity.this));
|
||||
}
|
||||
};
|
||||
|
||||
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
|
||||
private void handleFailReason(FailReason failReason) {
|
||||
Throwable cause = failReason.getCause();
|
||||
if (cause != null) {
|
||||
cause.printStackTrace();
|
||||
Toast.makeText(AlbumTagEditorActivity.this, cause.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
private static Bitmap getResizedAlbumCover(@NonNull Bitmap src, int maxForSmallerSize) {
|
||||
int width = src.getWidth();
|
||||
int height = src.getHeight();
|
||||
|
||||
final int dstWidth;
|
||||
final int dstHeight;
|
||||
|
||||
if (width < height) {
|
||||
if (maxForSmallerSize >= width) {
|
||||
return src;
|
||||
}
|
||||
float ratio = (float) height / width;
|
||||
dstWidth = maxForSmallerSize;
|
||||
dstHeight = Math.round(maxForSmallerSize * ratio);
|
||||
} else {
|
||||
if (maxForSmallerSize >= height) {
|
||||
return src;
|
||||
}
|
||||
float ratio = (float) width / height;
|
||||
dstWidth = Math.round(maxForSmallerSize * ratio);
|
||||
dstHeight = maxForSmallerSize;
|
||||
}
|
||||
|
||||
Bitmap scaledBitmap = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, false);
|
||||
if (scaledBitmap != src) {
|
||||
src.recycle();
|
||||
}
|
||||
return scaledBitmap;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,28 +1,10 @@
|
|||
package com.kabouzeid.gramophone.util;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Matrix;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
/**
|
||||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class ImageUtil {
|
||||
|
||||
public static Bitmap getResizedBitmap(@NonNull Bitmap bm, int newHeight, int newWidth, boolean recycleOld) {
|
||||
int width = bm.getWidth();
|
||||
int height = bm.getHeight();
|
||||
float scaleWidth = ((float) newWidth) / width;
|
||||
float scaleHeight = ((float) newHeight) / height;
|
||||
Matrix matrix = new Matrix();
|
||||
matrix.postScale(scaleWidth, scaleHeight);
|
||||
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
|
||||
if (recycleOld && resizedBitmap != bm) {
|
||||
bm.recycle();
|
||||
}
|
||||
return resizedBitmap;
|
||||
}
|
||||
|
||||
public static int calculateInSampleSize(int width, int height, int reqWidth) {
|
||||
// setting reqWidth matching to desired 1:1 ratio and screen-size
|
||||
if (width < height) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue