Commit 15effdb4 authored by Alexander Inglessi's avatar Alexander Inglessi
Browse files

Increase gradient wallpaper size, add progress dialog, add plasma effect.

parent c7322eed
......@@ -19,5 +19,4 @@
</profile>
</annotationProcessing>
</component>
</project>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
......@@ -5,6 +5,7 @@
<GradleProjectSettings>
<option name="distributionType" value="DEFAULT_WRAPPED" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleJvm" value="1.8" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
......@@ -14,5 +15,4 @@
</GradleProjectSettings>
</option>
</component>
</project>
</project>
\ No newline at end of file
......@@ -3,8 +3,60 @@
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<component name="NullableNotNullManager">
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
<option name="myNullables">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
</list>
</value>
</option>
<option name="myNotNulls">
<value>
<list size="4">
<item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.NotNull" />
<item index="1" class="java.lang.String" itemvalue="javax.annotation.Nonnull" />
<item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.NonNull" />
<item index="3" class="java.lang.String" itemvalue="android.support.annotation.NonNull" />
</list>
</value>
</option>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
<OptionsSetting value="true" id="Checkout" />
<OptionsSetting value="true" id="Update" />
<OptionsSetting value="true" id="Status" />
<OptionsSetting value="true" id="Edit" />
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="Android API 23 Platform" project-jdk-type="Android SDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
</project>
<component name="ProjectType">
<option name="id" value="Android" />
</component>
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.7</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>
\ No newline at end of file
......@@ -6,5 +6,4 @@
<module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
</modules>
</component>
</project>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunConfigurationProducerService">
<option name="ignoredProducers">
<set>
<option value="org.jetbrains.plugins.gradle.execution.test.runner.AllInPackageGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestClassGradleConfigurationProducer" />
<option value="org.jetbrains.plugins.gradle.execution.test.runner.TestMethodGradleConfigurationProducer" />
</set>
</option>
</component>
</project>
\ No newline at end of file
<component name="DependencyValidationManager">
<state>
<option name="SKIP_IMPORT_STATEMENTS" value="false" />
</state>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<module external.linked.project.id="WPGen" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
<option name="BUILDABLE" value="false" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
......@@ -17,5 +16,4 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
</module>
\ No newline at end of file
......@@ -9,11 +9,15 @@
<facet type="android" name="Android">
<configuration>
<option name="SELECTED_BUILD_VARIANT" value="debug" />
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugTest" />
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
<option name="TEST_SOURCE_GEN_TASK_NAME" value="generateDebugTestSources" />
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugTestSources" />
<afterSyncTasks>
<task>generateDebugSources</task>
<task>generateDebugTestSources</task>
</afterSyncTasks>
<option name="ALLOW_USER_CONFIGURATION" value="false" />
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
<option name="RES_FOLDER_RELATIVE_PATH" value="/src/main/res" />
......@@ -22,8 +26,9 @@
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/intermediates/classes/debug" />
<output-test url="file://$MODULE_DIR$/build/intermediates/classes/test/debug" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build/generated/source/r/debug" isTestSource="false" generated="true" />
......@@ -81,11 +86,10 @@
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="appcompat-v7-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
</component>
</module>
</module>
\ No newline at end of file
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "net.glsk.wpgen"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
targetSdkVersion 23
versionCode 11
versionName "1.1"
}
buildTypes {
release {
......@@ -21,5 +21,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:appcompat-v7:23.1.0' // see Android Support Library in SDK manager
}
......@@ -18,19 +18,24 @@
package net.glsk.wpgen;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.ActionMode;
import android.view.Gravity;
import android.view.LayoutInflater;
......@@ -47,8 +52,13 @@ import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class ColorsActivity extends ActionBarActivity {
......@@ -58,6 +68,12 @@ public class ColorsActivity extends ActionBarActivity {
ArrayList<String> allList = new ArrayList<>();
ArrayList<String> selectedList = new ArrayList<>();
static final int NOISE_SPREAD = 6;
static final int NOISE_BITMAP_SIZE = 64;
static final int SET_GRADIENT = 1;
static final int SET_PLASMA = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -94,7 +110,7 @@ public class ColorsActivity extends ActionBarActivity {
return true;
case R.id.action_add_color: // Show add color dialog.
AlertDialog.Builder addColorDialog = new AlertDialog.Builder(this);
addColorDialog.setTitle(R.string.dialog_add_color_title);
//addColorDialog.setTitle(R.string.dialog_add_color_title);
addColorDialog.setMessage(R.string.dialog_add_color_msg);
final EditText input = new EditText(this);
addColorDialog.setView(input);
......@@ -145,11 +161,11 @@ public class ColorsActivity extends ActionBarActivity {
}
}
// Set wallpaper to blurred image.
protected void setBlurWallpaper(ArrayList<String> colors) {
// Set wallpaper to gradient image.
protected void setGradientWallpaper(ArrayList<String> colors) {
WallpaperManager wpManager = WallpaperManager.getInstance(this.getApplicationContext());
// Use half screen size to reduce memory usage.
int height = (int) (wpManager.getDesiredMinimumHeight() / 2);
// Use full screen size so wallpaper is movable.
int height = (int) (wpManager.getDesiredMinimumHeight());
// Create square bitmap for wallpaper.
Bitmap wallpaperBitmap = Bitmap.createBitmap(height, height, Bitmap.Config.ARGB_8888);
// Prepare colors for gradient.
......@@ -164,13 +180,82 @@ public class ColorsActivity extends ActionBarActivity {
paint.setShader(gradientShader);
// Draw gradient on bitmap.
c.drawRect(0, 0, height, height, paint);
// Add noise.
//addNoise(wallpaperBitmap);
try {
// Set wallpaper.
wpManager.setBitmap(wallpaperBitmap);
} catch (IOException e) {
e.printStackTrace();
}
// Cleanup.
wallpaperBitmap.recycle();
}
// Set wallpaper to plasma image.
protected void setPlasmaWallpaper(ArrayList<String> colors) {
WallpaperManager wpManager = WallpaperManager.getInstance(this.getApplicationContext());
// Use half screen size for speed.
int height = wpManager.getDesiredMinimumHeight()/2;
int width = height;
// Create wallpaper bitmap.
Bitmap wallpaperBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
// Prepare colors for gradient.
int[] colorsInt = new int[colors.size()];
for (int i = 0; i < colors.size(); i++) {
colorsInt[i] = Color.parseColor(colors.get(i));
}
// Create gradient to construct palette.
Paint paint = new Paint();
Bitmap gradientBitmap = Bitmap.createBitmap(256, 1, Bitmap.Config.ARGB_8888);
Shader gradientShader = new LinearGradient(0, 0, 255, 0, colorsInt, null, Shader.TileMode.MIRROR);
Canvas c = new Canvas(gradientBitmap);
paint.setShader(gradientShader);
// Draw gradient on bitmap.
c.drawRect(0, 0, 256, 1, paint);
int[] palette = new int[256];
//
for(int x = 0; x < 256; x++) {
palette[x] = gradientBitmap.getPixel(x, 0);
}
// Cleanup.
gradientBitmap.recycle();
// Generate plasma.
int[][] plasma = new int[width][height];
Random random = new Random();
// TODO: add n (and maybe spread) as parameter in Settings.
double n = 1.3; // Number of periods per wallpaper width.
double period = width / (n * 2 * 3.14);
double spread = period * 0.3;
double period1 = period - spread + spread*random.nextFloat();
double period2 = period - spread + spread*random.nextFloat();
double period3 = period - spread + spread*random.nextFloat();
for(int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
{
// Adding sines to get plasma value.
int value = (int)
(
128.0 + (128.0 * Math.sin(x / period1))
+ 128.0 + (128.0 * Math.sin(y / period2))
+ 128.0 + (128.0 * Math.sin((x + y) / period1))
+ 128.0 + (128.0 * Math.sin(Math.sqrt((double) (x * x + y * y)) / period3))
) / 4;
plasma[x][y] = value;
}
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++) {
int color = palette[plasma[x][y] % 256];
wallpaperBitmap.setPixel(x, y, color);
}
// TODO: Add noise as option in Settings.
//addNoise(wallpaperBitmap);
wallpaperBitmap = Bitmap.createScaledBitmap(wallpaperBitmap, wpManager.getDesiredMinimumHeight(), wpManager.getDesiredMinimumHeight(), true);
try {
// Set wallpaper.
wpManager.setBitmap(wallpaperBitmap);
String text = getString(R.string.toast_wallpaper_set_to_blur);
Toast toast = Toast.makeText(this, text, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} catch (IOException e) {
e.printStackTrace();
}
......@@ -225,18 +310,35 @@ public class ColorsActivity extends ActionBarActivity {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
if (selectedList.size() > 1)
menu.findItem(R.id.action_create_blur).setVisible(true);
else
menu.findItem(R.id.action_create_blur).setVisible(false);
if (selectedList.size() > 1) {
menu.findItem(R.id.action_create_gradient).setVisible(true);
menu.findItem(R.id.action_create_plasma).setVisible(true);
} else {
menu.findItem(R.id.action_create_gradient).setVisible(false);
menu.findItem(R.id.action_create_plasma).setVisible(false);
}
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
AsyncTaskParams params;
SetWallpaperTask asyncTask;
String[] selectedArray = selectedList.toArray(new String[selectedList.size()]);
switch (item.getItemId()) {
case R.id.action_create_blur:
setBlurWallpaper(selectedList);
case R.id.action_create_gradient:
//setGradientWallpaper(selectedList);
params = new AsyncTaskParams(selectedArray, SET_GRADIENT);
asyncTask = new SetWallpaperTask(ColorsActivity.this);
Log.d("onClick", "colors: " + params.colors);
asyncTask.execute(params);
mode.finish();
return true;
case R.id.action_create_plasma:
params = new AsyncTaskParams(selectedArray, SET_PLASMA);
asyncTask = new SetWallpaperTask(ColorsActivity.this);
Log.d("onClick", "colors: " + params.colors);
asyncTask.execute(params);
mode.finish();
return true;
default:
......@@ -274,6 +376,64 @@ public class ColorsActivity extends ActionBarActivity {
}
}
private static class AsyncTaskParams {
String[] colors;
int operation;
AsyncTaskParams(String[] colors, int operation) {
this.colors = colors;
this.operation = operation;
}
}
private class SetWallpaperTask extends AsyncTask<AsyncTaskParams, Void, String> {
private ProgressDialog progressDialog;
Context context;
public SetWallpaperTask(Context context) {
this.context=context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(context);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setIndeterminate(true);
progressDialog.setMessage(getString(R.string.setting_wp));
progressDialog.show();
}
@Override
protected String doInBackground(AsyncTaskParams... params) {
String result = null;
String[] colors = params[0].colors;
ArrayList<String> colorsList = new ArrayList(Arrays.asList(colors));
int operation = params[0].operation;
if (operation == SET_GRADIENT) {
Log.d("doInBg", "colors: " + params[0].colors + ", op: " + params[0].operation);
setGradientWallpaper(colorsList);
result = getString(R.string.toast_wallpaper_set_to_gradient);
}
if (operation == SET_PLASMA) {
Log.d("doInBg", "colors: " + params[0].colors + ", op: " + params[0].operation);
setPlasmaWallpaper(colorsList);
result = getString(R.string.toast_wallpaper_set_to_plasma);
}
return result;
}
protected void onProgressUpdate() {
//setProgressPercent(progress[0]);
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
Toast toast = Toast.makeText(ColorsActivity.this, result, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
// Adapter for GridView.
public class ImageAdapter extends BaseAdapter {
public
......@@ -369,4 +529,58 @@ public class ColorsActivity extends ActionBarActivity {
}
return Bitmap.createBitmap(bitmapArray, size, size, Bitmap.Config.ARGB_8888);
}
public Bitmap generateNoise() {
Bitmap resultBitmap;
String fileName = "noise.png";
String filePath = this.getFilesDir().getAbsolutePath() + File.separator + fileName;
File file = new File(filePath);
if (file.exists()) {
// File exists, use it.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
resultBitmap = BitmapFactory.decodeFile(filePath, options);
Log.d("generateNoise", "bitmap exists, loading");
} else {
// File does not exist, generate new one.
int width = NOISE_BITMAP_SIZE;
int height = NOISE_BITMAP_SIZE;
Log.d("generateNoise", "generating new noise bitmap (" + width + "x" + height + ")");
int[] pixels = new int[width * height];
Random random = new Random();
for (int i = 0; i < width * height; i++) {
// Fill with black pixels with random opacity ranging from 0 to NOISE_SPREAD.
int noise = random.nextInt(NOISE_SPREAD);
pixels[i] = Color.argb(noise, 0, 0, 0);
}
resultBitmap = Bitmap.createBitmap(pixels, width, height, Bitmap.Config.ARGB_8888);
// Save noise bitmap for future use.
FileOutputStream out = null;
try {
out = new FileOutputStream(filePath);
resultBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resultBitmap;
}
public Bitmap addNoise(Bitmap source) {
Canvas canvas = new Canvas(source);
Bitmap noise = generateNoise();
Shader noiseShader = new BitmapShader(noise, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Paint paint = new Paint();
paint.setShader(noiseShader);
canvas.drawRect(0, 0, source.getWidth(), source.getHeight(), paint);
return source;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment