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() {
|
private void startImagePicker() {
|
||||||
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
|
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
|
||||||
photoPickerIntent.setType("image/*");
|
intent.setType("image/*");
|
||||||
startActivityForResult(photoPickerIntent, REQUEST_CODE_SELECT_IMAGE);
|
startActivityForResult(Intent.createChooser(intent, getString(R.string.pick_from_local_storage)), REQUEST_CODE_SELECT_IMAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void loadCurrentImage();
|
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.lastfm.rest.model.LastFmAlbum;
|
||||||
import com.kabouzeid.gramophone.loader.AlbumSongLoader;
|
import com.kabouzeid.gramophone.loader.AlbumSongLoader;
|
||||||
import com.kabouzeid.gramophone.model.Song;
|
import com.kabouzeid.gramophone.model.Song;
|
||||||
import com.kabouzeid.gramophone.util.ImageUtil;
|
|
||||||
import com.kabouzeid.gramophone.util.LastFMUtil;
|
import com.kabouzeid.gramophone.util.LastFMUtil;
|
||||||
import com.kabouzeid.gramophone.util.MusicUtil;
|
import com.kabouzeid.gramophone.util.MusicUtil;
|
||||||
import com.kabouzeid.gramophone.util.Util;
|
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>() {
|
lastFMRestClient.getApiService().getAlbumInfo(albumTitleStr, albumArtistNameStr).enqueue(new Callback<LastFmAlbum>() {
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(Response<LastFmAlbum> response) {
|
public void onResponse(final Response<LastFmAlbum> response) {
|
||||||
LastFmAlbum lastFmAlbum = response.body();
|
LastFmAlbum lastFmAlbum = response.body();
|
||||||
if (lastFmAlbum.getAlbum() != null) {
|
if (lastFmAlbum.getAlbum() != null) {
|
||||||
final int smallerScreenSize = Util.getSmallerScreenSize(AlbumTagEditorActivity.this);
|
|
||||||
ImageLoader.getInstance().loadImage(LastFMUtil.getLargestAlbumImageUrl(lastFmAlbum.getAlbum().getImage()),
|
ImageLoader.getInstance().loadImage(LastFMUtil.getLargestAlbumImageUrl(lastFmAlbum.getAlbum().getImage()),
|
||||||
new DisplayImageOptions.Builder()
|
new DisplayImageOptions.Builder()
|
||||||
.preProcessor(new BitmapProcessor() {
|
.preProcessor(albumCoverProcessor)
|
||||||
@Override
|
|
||||||
public Bitmap process(Bitmap bitmap) {
|
|
||||||
return ImageUtil.getResizedBitmap(bitmap, smallerScreenSize, smallerScreenSize, true);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.build(),
|
.build(),
|
||||||
new SimpleImageLoadingListener() {
|
new SimpleImageLoadingListener() {
|
||||||
@Override
|
|
||||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
|
||||||
toastLoadingFailed();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||||
if (loadedImage == null) {
|
if (loadedImage == null) {
|
||||||
|
|
@ -133,6 +121,11 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
||||||
dataChanged();
|
dataChanged();
|
||||||
setResult(RESULT_OK);
|
setResult(RESULT_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
|
||||||
|
handleFailReason(failReason);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
toastLoadingFailed();
|
toastLoadingFailed();
|
||||||
|
|
@ -206,23 +199,11 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void loadImageFromFile(@NonNull final Uri selectedFileUri) {
|
protected void loadImageFromFile(@NonNull final Uri selectedFileUri) {
|
||||||
final int smallerScreenSize = Util.getSmallerScreenSize(AlbumTagEditorActivity.this);
|
|
||||||
ImageLoader.getInstance().loadImage(selectedFileUri.toString(),
|
ImageLoader.getInstance().loadImage(selectedFileUri.toString(),
|
||||||
new DisplayImageOptions.Builder()
|
new DisplayImageOptions.Builder()
|
||||||
.preProcessor(new BitmapProcessor() {
|
.preProcessor(albumCoverProcessor)
|
||||||
@Override
|
|
||||||
public Bitmap process(Bitmap bitmap) {
|
|
||||||
return ImageUtil.getResizedBitmap(bitmap, smallerScreenSize, smallerScreenSize, true);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.build(),
|
.build(),
|
||||||
new SimpleImageLoadingListener() {
|
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
|
@Override
|
||||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||||
albumArtBitmap = loadedImage;
|
albumArtBitmap = loadedImage;
|
||||||
|
|
@ -231,6 +212,11 @@ public class AlbumTagEditorActivity extends AbsTagEditorActivity implements Text
|
||||||
dataChanged();
|
dataChanged();
|
||||||
setResult(RESULT_OK);
|
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) {
|
public void afterTextChanged(Editable s) {
|
||||||
dataChanged();
|
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;
|
package com.kabouzeid.gramophone.util;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.Matrix;
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Karim Abou Zeid (kabouzeid)
|
* @author Karim Abou Zeid (kabouzeid)
|
||||||
*/
|
*/
|
||||||
public class ImageUtil {
|
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) {
|
public static int calculateInSampleSize(int width, int height, int reqWidth) {
|
||||||
// setting reqWidth matching to desired 1:1 ratio and screen-size
|
// setting reqWidth matching to desired 1:1 ratio and screen-size
|
||||||
if (width < height) {
|
if (width < height) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue