From bc6dadc07c8180e25951a01f01e61c5c856c6b3c Mon Sep 17 00:00:00 2001 From: "moxun.ljf" Date: Sat, 6 Feb 2016 16:43:06 +0800 Subject: [PATCH] fix issue #4 --- app/build.gradle | 3 +- .../com/moxun/tagcloud/TextTagsAdapter.java | 2 +- app/src/main/res/layout/activity_main.xml | 4 +-- tagcloudlib/build.gradle | 2 +- .../java/com/moxun/tagcloudlib/view/Tag.java | 4 ++- .../com/moxun/tagcloudlib/view/TagCloud.java | 21 +++++++------ .../moxun/tagcloudlib/view/TagCloudView.java | 30 +++++++++---------- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7630d95..615fe69 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,5 +22,6 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.+' - compile 'com.moxun:tagcloudlib:1.0.3' + //compile 'com.moxun:tagcloudlib:1.0.3' + compile project(path: ':tagcloudlib') } diff --git a/app/src/main/java/com/moxun/tagcloud/TextTagsAdapter.java b/app/src/main/java/com/moxun/tagcloud/TextTagsAdapter.java index 5a6dfa9..55cf657 100644 --- a/app/src/main/java/com/moxun/tagcloud/TextTagsAdapter.java +++ b/app/src/main/java/com/moxun/tagcloud/TextTagsAdapter.java @@ -48,7 +48,7 @@ public Object getItem(int position) { @Override public int getPopularity(int position) { - return Math.abs(new Random().nextInt(5) + 2); + return position % 7; } @Override diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index c81bc45..eb429f1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -15,8 +15,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" app:autoScrollMode="uniform" - app:lightColor="#0fe291" - app:darkColor="#123456" + app:lightColor="#ffff0000" + app:darkColor="#ff00ff00" app:radiusPercent="0.5" app:scrollSpeed="3"/> diff --git a/tagcloudlib/build.gradle b/tagcloudlib/build.gradle index 6877118..685f8a1 100644 --- a/tagcloudlib/build.gradle +++ b/tagcloudlib/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'com.github.dcendents.android-maven' apply plugin: 'com.jfrog.bintray' -version = "1.0.3" +version = "1.1.0" android { compileSdkVersion 'Google Inc.:Google APIs:23' buildToolsVersion "23.0.2" diff --git a/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/Tag.java b/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/Tag.java index 5c67664..8b63f1c 100755 --- a/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/Tag.java +++ b/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/Tag.java @@ -1,6 +1,7 @@ package com.moxun.tagcloudlib.view; import android.graphics.Color; +import android.util.Log; /** * Copyright © 2016 moxun @@ -132,9 +133,10 @@ public void setColorByArray(float[] rgb) { public int getColor() { int[] result = new int[4]; for (int i = 0; i < 4; i++) { - result[i] = (int) (this.argb[i] * 256); + result[i] = (int) (this.argb[i] * 0xff); } int color = Color.argb(result[0], result[1], result[2], result[3]); + Log.e("TAG","#" + Integer.toHexString(color)); return color; } } diff --git a/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloud.java b/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloud.java index 12485ed..958e373 100755 --- a/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloud.java +++ b/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloud.java @@ -22,6 +22,8 @@ * OR OTHER DEALINGS IN THE SOFTWARE. */ +import android.util.Log; + import java.util.ArrayList; import java.util.List; @@ -132,8 +134,8 @@ public void update() { private void initTag(Tag tag) { float percentage = getPercentage(tag); - float[] tempColor = getColorFromGradient(percentage); //(rgb Alpha) - tag.setColorByArray(tempColor); + float[] argb = getColorFromGradient(percentage); + tag.setColorByArray(argb); } private float getPercentage(Tag tag) { @@ -225,13 +227,14 @@ private void updateAll() { } } - private float[] getColorFromGradient(float perc) { - float[] tempRGB = new float[4]; - tempRGB[0] = (perc * (tagColorLight[0])) + ((1 - perc) * (tagColorDark[0])); - tempRGB[1] = (perc * (tagColorLight[1])) + ((1 - perc) * (tagColorDark[1])); - tempRGB[2] = (perc * (tagColorLight[2])) + ((1 - perc) * (tagColorDark[2])); - tempRGB[3] = 1; - return tempRGB; + private float[] getColorFromGradient(float percentage) { + //perc: 1.0 full dark; 0.0 full light + float[] rgba = new float[4]; + rgba[0] = 1f; //Alpha is 1.0 when init. + rgba[1] = (percentage * (tagColorDark[0])) + ((1f - percentage) * (tagColorLight[0])); + rgba[2] = (percentage * (tagColorDark[1])) + ((1f - percentage) * (tagColorLight[1])); + rgba[3] = (percentage * (tagColorDark[2])) + ((1f - percentage) * (tagColorLight[2])); + return rgba; } private void sineCosine(float mAngleX, float mAngleY, float mAngleZ) { diff --git a/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloudView.java b/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloudView.java index f69d8d3..3f81cb4 100755 --- a/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloudView.java +++ b/tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloudView.java @@ -49,8 +49,8 @@ public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnD private float radius; private float radiusPercent = 0.9f; - private float[] darkColor = new float[]{1f, 0f, 0f, 1}; - private float[] lightColor = new float[]{0.9412f, 0.7686f, 0.2f, 1}; + private float[] darkColor = new float[]{1f, 0f, 0f, 1f};//rgba + private float[] lightColor = new float[]{0.9412f, 0.7686f, 0.2f, 1f};//rgba public static final int MODE_DISABLE = 0; public static final int MODE_DECELERATE = 1; @@ -68,23 +68,21 @@ public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnD public TagCloudView(Context context) { super(context); - setFocusableInTouchMode(true); init(context,null); } public TagCloudView(Context context, AttributeSet attrs) { super(context, attrs); - setFocusableInTouchMode(true); init(context,attrs); } public TagCloudView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - setFocusableInTouchMode(true); init(context,attrs); } private void init(Context context, AttributeSet attrs) { + setFocusableInTouchMode(true); mTagCloud = new TagCloud(); if (attrs != null) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TagCloudView); @@ -92,10 +90,10 @@ private void init(Context context, AttributeSet attrs) { String m = typedArray.getString(R.styleable.TagCloudView_autoScrollMode); mode = Integer.valueOf(m); - int light = typedArray.getColor(R.styleable.TagCloudView_lightColor,Color.GREEN); + int light = typedArray.getColor(R.styleable.TagCloudView_lightColor,Color.WHITE); setLightColor(light); - int dark = typedArray.getColor(R.styleable.TagCloudView_darkColor,Color.GRAY); + int dark = typedArray.getColor(R.styleable.TagCloudView_darkColor,Color.BLACK); setDarkColor(dark); float p = typedArray.getFloat(R.styleable.TagCloudView_radiusPercent,radiusPercent); @@ -103,6 +101,8 @@ private void init(Context context, AttributeSet attrs) { float s = typedArray.getFloat(R.styleable.TagCloudView_scrollSpeed,2f); setScrollSpeed(s); + + typedArray.recycle(); } WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE); @@ -130,10 +130,10 @@ public final void setAdapter(TagsAdapter adapter) { public void setLightColor(int color) { float[] argb = new float[4]; - argb[0] = Color.alpha(color) /1.0f / 255; - argb[1] = Color.red(color) /1.0f / 255; - argb[2] = Color.green(color) /1.0f / 255; - argb[3] = Color.blue(color) /1.0f / 255; + argb[3] = Color.alpha(color) /1.0f / 0xff; + argb[0] = Color.red(color) /1.0f / 0xff; + argb[1] = Color.green(color) /1.0f / 0xff; + argb[2] = Color.blue(color) /1.0f / 0xff; lightColor = argb.clone(); onChange(); @@ -141,10 +141,10 @@ public void setLightColor(int color) { public void setDarkColor(int color) { float[] argb = new float[4]; - argb[0] = Color.alpha(color) /1.0f / 255; - argb[1] = Color.red(color) /1.0f / 255; - argb[2] = Color.green(color) /1.0f / 255; - argb[3] = Color.blue(color) /1.0f / 255; + argb[3] = Color.alpha(color) /1.0f / 0xff; + argb[0] = Color.red(color) /1.0f / 0xff; + argb[1] = Color.green(color) /1.0f / 0xff; + argb[2] = Color.blue(color) /1.0f / 0xff; darkColor = argb.clone(); onChange();