Use Palette from android.support.v7 to get Feed color.

This gives more vibrant colors than just using the average.
This commit is contained in:
Daniel Schaal 2015-07-10 16:25:35 +02:00
parent 0c9fc6bb54
commit b1b0aa8a94
3 changed files with 6 additions and 92 deletions

View file

@ -51,6 +51,7 @@ dependencies {
compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:palette-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'de.mrmaffen:holocircularprogressbar:1.0.1'
compile 'com.google.code.gson:gson:2.3.1'

View file

@ -1,62 +0,0 @@
/**
* Android ownCloud News
*
* @author David Luhmer
* @copyright 2013 David Luhmer david-dev@live.de
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.luhmer.owncloudnewsreader.helper;
import android.graphics.Bitmap;
import android.graphics.Color;
public class ColourCalculator {
public static int[] averageARGB(Bitmap pic) {
int A, R, G, B;
A = R = G = B = 0;
int pixelColor;
int width = pic.getWidth();
int height = pic.getHeight();
int size = width * height;
if(size > 0) {
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
pixelColor = pic.getPixel(x, y);
A += Color.alpha(pixelColor);
R += Color.red(pixelColor);
G += Color.green(pixelColor);
B += Color.blue(pixelColor);
}
}
A /= size;
R /= size;
G /= size;
B /= size;
}
int[] average = { A, R, G, B };
return average;
}
public static String ColourHexFromBitmap(Bitmap bitmap) {
int[] colorArr = averageARGB(bitmap);
int color = Color.argb(colorArr[0], colorArr[1], colorArr[2], colorArr[3]);
return String.valueOf(color);
}
}

View file

@ -28,6 +28,7 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.support.v7.graphics.Palette;
import android.util.Log;
import android.util.SparseArray;
import android.widget.ImageView;
@ -68,35 +69,6 @@ public class FavIconHandler {
}
}
public static Drawable GetFavIconFromCache(String URL_TO_PAGE, Context context, Long feedID)
{
try
{
File favIconFile = ImageHandler.getFullPathOfCacheFile(URL_TO_PAGE, FileUtils.getPathFavIcons(context));
if(favIconFile.isFile() && favIconFile.length() > 0)
{
if(feedID != null) {
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context);
Feed feed = dbConn.getFeedById(feedID);
Bitmap bitmap = BitmapFactory.decodeFile(favIconFile.getAbsolutePath());
String avg = ColourCalculator.ColourHexFromBitmap(bitmap);
feed.setAvgColour(avg);
dbConn.updateFeed(feed);
//dbConn.setAvgColourOfFeedByDbId(feedID, avg);
}
return Drawable.createFromPath(favIconFile.getPath());
}
}
catch(Exception ex)
{
//Log.d(TAG, ex.getMessage());
ex.printStackTrace();
}
return null;
}
public static int getResourceIdForRightDefaultFeedIcon(Context context)
{
if(ThemeChooser.isDarkTheme(context))
@ -127,7 +99,10 @@ public class FavIconHandler {
if(bitmap != null) {
DatabaseConnectionOrm dbConn = new DatabaseConnectionOrm(context);
Feed feed = dbConn.getFeedById(AsynkTaskId);
String avg = ColourCalculator.ColourHexFromBitmap(bitmap);
Palette palette = Palette.from(bitmap).generate();
String avg = String.valueOf(
palette.getVibrantColor(R.color.material_blue_grey_800)
);
feed.setAvgColour(avg);
dbConn.updateFeed(feed);