Skip to content

Commit

Permalink
fixed issue #4
Browse files Browse the repository at this point in the history
  • Loading branch information
misakuo committed Feb 17, 2016
1 parent b37ccd0 commit fc4fc04
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 46 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.+'
//compile 'com.moxun:tagcloudlib:1.1.0'
compile project(path: ':tagcloudlib')
}
9 changes: 8 additions & 1 deletion app/src/main/java/com/moxun/tagcloud/TextTagsAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -32,12 +33,18 @@ public int getCount() {
}

@Override
public View getView(Context context, int position, ViewGroup parent) {
public View getView(Context context, final int position, ViewGroup parent) {
TextView tv = new TextView(context);
ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(100, 100);
tv.setLayoutParams(lp);
tv.setText("No." + position);
tv.setGravity(Gravity.CENTER);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("Click","Tag " + position + " clicked.");
}
});
return tv;
}

Expand Down
2 changes: 1 addition & 1 deletion tagcloudlib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'

version = "1.1.0"
version = "1.1.1"
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
Expand Down
101 changes: 58 additions & 43 deletions tagcloudlib/src/main/java/com/moxun/tagcloudlib/view/TagCloudView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

/**
* Copyright © 2016 moxun
*
* <p/>
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the “Software”),
* to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* <p/>
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* <p/>
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Expand All @@ -26,15 +26,16 @@
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.graphics.Point;

import com.moxun.tagcloudlib.R;

Expand All @@ -49,7 +50,7 @@ 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, 1f};//rgba
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;
Expand All @@ -59,7 +60,7 @@ public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnD
private int left, right, top, bottom;

private MarginLayoutParams layoutParams;
private int minSize;
private int minSize;

private boolean isOnTouch = false;
private Handler handler = new Handler(Looper.getMainLooper());
Expand All @@ -68,17 +69,17 @@ public class TagCloudView extends ViewGroup implements Runnable, TagsAdapter.OnD

public TagCloudView(Context context) {
super(context);
init(context,null);
init(context, null);
}

public TagCloudView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs);
init(context, attrs);
}

public TagCloudView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context,attrs);
init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
Expand All @@ -90,22 +91,22 @@ 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.WHITE);
int light = typedArray.getColor(R.styleable.TagCloudView_lightColor, Color.WHITE);
setLightColor(light);

int dark = typedArray.getColor(R.styleable.TagCloudView_darkColor,Color.BLACK);
int dark = typedArray.getColor(R.styleable.TagCloudView_darkColor, Color.BLACK);
setDarkColor(dark);

float p = typedArray.getFloat(R.styleable.TagCloudView_radiusPercent,radiusPercent);
float p = typedArray.getFloat(R.styleable.TagCloudView_radiusPercent, radiusPercent);
setRadiusPercent(p);

float s = typedArray.getFloat(R.styleable.TagCloudView_scrollSpeed,2f);
float s = typedArray.getFloat(R.styleable.TagCloudView_scrollSpeed, 2f);
setScrollSpeed(s);

typedArray.recycle();
}

WindowManager wm = (WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Point point = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
wm.getDefaultDisplay().getSize(point);
Expand All @@ -115,7 +116,7 @@ private void init(Context context, AttributeSet attrs) {
}
int screenWidth = point.x;
int screenHeight = point.y;
minSize = screenHeight < screenWidth ? screenHeight : screenWidth;
minSize = screenHeight < screenWidth ? screenHeight : screenWidth;
}

public void setAutoScrollMode(int mode) {
Expand All @@ -130,21 +131,21 @@ public final void setAdapter(TagsAdapter adapter) {

public void setLightColor(int color) {
float[] argb = new float[4];
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;
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();
}

public void setDarkColor(int color) {
float[] argb = new float[4];
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;
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();
Expand Down Expand Up @@ -202,12 +203,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

measureChildren(widthMode, heightMode);

if(layoutParams == null) {
if (layoutParams == null) {
layoutParams = (MarginLayoutParams) getLayoutParams();
}

int dimensionX = widthMode == MeasureSpec.EXACTLY ? contentWidth : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
int dimensionY = heightMode == MeasureSpec.EXACTLY ? contentHeight : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
int dimensionX = widthMode == MeasureSpec.EXACTLY ? contentWidth : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
int dimensionY = heightMode == MeasureSpec.EXACTLY ? contentHeight : minSize - layoutParams.leftMargin - layoutParams.rightMargin;
setMeasuredDimension(dimensionX, dimensionY);
}

Expand All @@ -225,21 +226,21 @@ protected void onDetachedFromWindow() {

@SuppressLint("WrongCall")
private void updateChild() {
onLayout(false, left,top,right,bottom);
onLayout(false, left, top, right, bottom);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
left = l;
right = r;
top = t;
left = l;
right = r;
top = t;
bottom = b;

for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child.getVisibility() != GONE) {
Tag tag = mTagCloud.get(i);
tagsAdapter.onThemeColorChanged(child,tag.getColor());
tagsAdapter.onThemeColorChanged(child, tag.getColor());
child.setScaleX(tag.getScale());
child.setScaleY(tag.getScale());
int left, top;
Expand Down Expand Up @@ -272,32 +273,46 @@ public boolean onTrackballEvent(MotionEvent e) {
return true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
handleTouchEvent(ev);
return false;
}

@Override
public boolean onTouchEvent(MotionEvent e) {
float x = e.getX();
float y = e.getY();
handleTouchEvent(e);
return true;
}

private float downX, downY;

private void handleTouchEvent(MotionEvent e) {
switch (e.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = e.getX();
downY = e.getY();
isOnTouch = true;
break;
case MotionEvent.ACTION_MOVE:
//rotate elements depending on how far the selection point is from center of cloud
float dx = x - centerX;
float dy = y - centerY;
mAngleX = (dy / radius) * tspeed * TOUCH_SCALE_FACTOR;
mAngleY = (-dx / radius) * tspeed * TOUCH_SCALE_FACTOR;

processTouch();

float dx = e.getX() - downX;
float dy = e.getY() - downY;
if (isValidMove(dx, dy)) {
mAngleX = (dy / radius) * tspeed * TOUCH_SCALE_FACTOR;
mAngleY = (-dx / radius) * tspeed * TOUCH_SCALE_FACTOR;
processTouch();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
isOnTouch = false;
break;
}
}

return true;
private boolean isValidMove(float dx, float dy) {
int minDistance = ViewConfiguration.get(getContext()).getScaledTouchSlop();
return (Math.abs(dx) > minDistance || Math.abs(dy) > minDistance);
}

private void processTouch() {
Expand Down

1 comment on commit fc4fc04

@misakuo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed issue #5

Please sign in to comment.