这个适合用于选择 用户的一些兴趣标签,个性名片等。
代码:
Activity
package com.dyl.cloudtags;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private KeywordsFlow keywordsFlow;
private String[] keywords;
public static final String SEARCH_HISTORY = “search_history”;
private ArrayList searchItem;
private String longhistory;
private SharedPreferences sp;
private ArrayList history;
private EditText world_shopping_search_input;
private TextView world_city_refresh, clear_history;
private ImageView toSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initSearchHistory();
refreshTags();
}
private void initView() {
world_shopping_search_input = (EditText) findViewById(R.id.world_shopping_search_input);
keywordsFlow = (KeywordsFlow) findViewById(R.id.keywordsflow);
world_city_refresh = (TextView) findViewById(R.id.world_city_refresh);
world_city_refresh.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
refreshTags();
}
});
clear_history = (TextView) findViewById(R.id.clear_history);
clear_history.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
clearSearchHistory();
}
});
toSearch = (ImageView) findViewById(R.id.toSearch);
toSearch.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
saveSearchHistory();
refreshTags();
}
});
}
private void refreshTags() {
initSearchHistory();
keywordsFlow.setDuration(800l);
keywordsFlow.setOnItemClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String keyword = ((TextView) v).getText().toString();// 获得点击的标签
world_shopping_search_input.setText(keyword);
}
});
// 添加
feedKeywordsFlow(keywordsFlow, keywords);
keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
}
private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
Random random = new Random();
for (int i = 0; i < KeywordsFlow.MAX; i++) {
int ran = random.nextInt(arr.length);
String tmp = arr[ran];
keywordsFlow.feedKeyword(tmp);
}
}
/**
- 读取历史搜索记录
*/
private void initSearchHistory() {
sp = getSharedPreferences(MainActivity.SEARCH_HISTORY, 0);
longhistory = sp.getString(MainActivity.SEARCH_HISTORY, “”);
if (!longhistory.equals(“”)) {
keywords = longhistory.split(“,”);
searchItem = new ArrayList();
for (int i = 0; i < keywords.length; i++) {
searchItem.add(new SearchDataPojo().setContent(keywords[i]));
}
} else {// 如果SharedPreferences没有值得话,就显示默认的数据
keywords = new String[] { “口味虾”, “牛蛙”, “火锅”, “真功夫”, “料理”,
“密室逃”, “天成房”, “波比艾” };
}
}
/*
- 保存搜索记录
*/
private void saveSearchHistory() {
String text = world_shopping_search_input.getText().toString().trim();
Toast.makeText(this, text, Toast.LENGTH_LONG).show();
if (!text.equals(“”) && text != null) {
if (text.length() < 1) {
return;
}
sp = getSharedPreferences(SEARCH_HISTORY, 0);
String longhistory = sp.getString(SEARCH_HISTORY, “”);
String[] tmpHistory = longhistory.split(“,”);
history = new ArrayList(Arrays.asList(tmpHistory));
if (history.size() > 0) {
int i;
for (i = 0; i < history.size(); i++) {
if (text.equals(history.get(i))) {
history.remove(i);
break;
}
}
history.add(0, text);
}
if (history.size() > 0) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < history.size(); i++) {
sb.append(history.get(i) + “,”);
}
sp.edit().putString(SEARCH_HISTORY, sb.toString()).commit();
} else {
sp.edit().putString(SEARCH_HISTORY, text + “,”).commit();
}
clear_history.setVisibility(View.VISIBLE);
}
}
// 清除历史数据
private void clearSearchHistory() {
searchItem.removeAll(searchItem);
sp.edit().clear().commit();
Toast.makeText(this, “清除历史记录”, Toast.LENGTH_LONG).show();
}
}
用于将控件 设置为圆形 的自定义TextView
package com.dyl.cloudtags;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.util.AttributeSet;
import android.widget.TextView;
public class CircleView extends TextView {
private Paint mBgPaint = new Paint();
PaintFlagsDrawFilter pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);
public CircleView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public CircleView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mBgPaint.setColor(Color.WHITE);
mBgPaint.setAntiAlias(true);
}
public CircleView(Context context) {
super(context);
// TODO Auto-generated constructor stub
mBgPaint.setColor(Color.WHITE);
mBgPaint.setAntiAlias(true);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredWidth = getMeasuredWidth();
int measuredHeight = getMeasuredHeight();
int max = Math.max(measuredWidth, measuredHeight);
setMeasuredDimension(max, max);
}
@Override
public void setBackgroundColor(int color) {
// TODO Auto-generated method stub
mBgPaint.setColor(color);
}
/**
-
设置通知个数显示
-
@param text
*/
public void setNotifiText(int text){
// if(text>99){
// String string = 99+“+”;
// setText(string);
// return;
// }
setText(text+“”);
}
@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.setDrawFilter(pfd);
canvas.drawCircle(getWidth()/2, getHeight()/2, Math.max(getWidth(), getHeight())/2, mBgPaint);
super.draw(canvas);
}
}
自定义布局 用于动态生成多个 控件 核心类
package com.dyl.cloudtags;
import java.util.LinkedList;
import java.util.Random;
import java.util.Vector;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
public class KeywordsFlow extends FrameLayout implements OnGlobalLayoutListener {
public static final int IDX_X = 0;
public static final int IDX_Y = 1;
public static final int IDX_TXT_LENGTH = 2;
public static final int IDX_DIS_Y = 3;
/** 由外至内的动画。 */
public static final int ANIMATION_IN = 1;
/** 由内至外的动画。 */
public static final int ANIMATION_OUT = 2;
/** 位移动画类型:从外围移动到坐标点。 */
public static final int OUTSIDE_TO_LOCATION = 1;
/** 位移动画类型:从坐标点移动到外围。 */
public static final int LOCATION_TO_OUTSIDE = 2;
/** 位移动画类型:从中心点移动到坐标点。 */
public static final int CENTER_TO_LOCATION = 3;
/** 位移动画类型:从坐标点移动到中心点。 */
public static final int LOCATION_TO_CENTER = 4;
public static final long ANIM_DURATION = 800l;
public static final int MAX = 12;
public static final int TEXT_SIZE_MAX = 20;
public static final int TEXT_SIZE_MIN = 10;
private OnClickListener itemClickListener;
private static Interpolator interpolator;
private static AlphaAnimation animAlpha2Opaque;
private static AlphaAnimation animAlpha2Transparent;
private static ScaleAnimation animScaleLarge2Normal, animScaleNormal2Large,
animScaleZero2Normal, animScaleNormal2Zero;
/** 存储显示的关键字。 */
private Vector vecKeywords;
private int width, height;
/**
-
go2Show()中被赋值为true,标识开发人员触发其开始动画显示。
-
本标识的作用是防止在填充keywrods未完成的过程中获取到width和height后提前启动动画。
-
在show()方法中其被赋值为false。
-
真正能够动画显示的另一必要条件:width 和 height不为0。
*/
private boolean enableShow;
private Random random;
private int txtAnimInType, txtAnimOutType;
/** 最近一次启动动画显示的时间。 */
private long lastStartAnimationTime;
/** 动画运行时间。 */
private long animDuration;
private Context context;
public KeywordsFlow(Context context) {
super(context);
init();
}
public KeywordsFlow(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public KeywordsFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
lastStartAnimationTime = 0l;
animDuration = ANIM_DURATION;
random = new Random();
vecKeywords = new Vector(MAX);
getViewTreeObserver().addOnGlobalLayoutListener(this);
interpolator = AnimationUtils.loadInterpolator(getContext(),
android.R.anim.decelerate_interpolator);
animAlpha2Opaque = new AlphaAnimation(0.0f, 1.0f);
animAlpha2Transparent = new AlphaAnimation(1.0f, 0.0f);
animScaleLarge2Normal = new ScaleAnimation(2, 1, 2, 1);
animScaleNormal2Large = new ScaleAnimation(1, 2, 1, 2);
animScaleZero2Normal = new ScaleAnimation(0, 1, 0, 1);
animScaleNormal2Zero = new ScaleAnimation(1, 0, 1, 0);
}
public long getDuration() {
return animDuration;
}
public void setDuration(long duration) {
animDuration = duration;
}
public boolean feedKeyword(String keyword) {
boolean result = false;
if (vecKeywords.size() < MAX) {
result = vecKeywords.add(keyword);
}
return result;
}
/**
-
开始动画显示。
-
之前已经存在的TextView将会显示退出动画。
-
@return 正常显示动画返回true;反之为false。返回false原因如下:
-
1.时间上不允许,受lastStartAnimationTime的制约;<br/>
-
2.未获取到width和height的值。<br/>
*/
public boolean go2Show(int animType) {
if (System.currentTimeMillis() - lastStartAnimationTime > animDuration) {
enableShow = true;
if (animType == ANIMATION_IN) {
txtAnimInType = OUTSIDE_TO_LOCATION;
txtAnimOutType = LOCATION_TO_CENTER;
} else if (animType == ANIMATION_OUT) {
txtAnimInType = CENTER_TO_LOCATION;
txtAnimOutType = LOCATION_TO_OUTSIDE;
}
disapper();
boolean result = show();
return result;
}
return false;
}
private void disapper() {
int size = getChildCount();
for (int i = size - 1; i >= 0; i–) {
final CircleView txv = (CircleView) getChildAt(i);
if (txv.getVisibility() == View.GONE) {
removeView(txv);
continue;
}
FrameLayout.LayoutParams layParams = (LayoutParams) txv
.getLayoutParams();
int[] xy = new int[] { layParams.leftMargin, layParams.topMargin,
txv.getWidth() };
AnimationSet animSet = getAnimationSet(xy, (width >> 1),
(height >> 1), txtAnimOutType);
txv.startAnimation(animSet);
animSet.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
txv.setOnClickListener(null);
txv.setClickable(false);
txv.setVisibility(View.GONE);
}
});
}
}
private boolean show() {
if (width > 0 && height > 0 && vecKeywords != null
&& vecKeywords.size() > 0 && enableShow) {
enableShow = false;
lastStartAnimationTime = System.currentTimeMillis();
int xCenter = width >> 1, yCenter = height >> 1;
int size = vecKeywords.size();
int xItem = width / size, yItem = height / size;
LinkedList listX = new LinkedList(), listY = new LinkedList();
for (int i = 0; i < size; i++) {
// 准备随机候选数,分别对应x/y轴位置
listX.add(i * xItem);
listY.add(i * yItem + (yItem >> 2));
}
LinkedList listTxtTop = new LinkedList();
LinkedList listTxtBottom = new LinkedList();
for (int i = 0; i < size; i++) {
String keyword = vecKeywords.get(i);
// 随机位置,糙值
int xy[] = randomXY(random, listX, listY, xItem);
// 实例化TextView
final CircleView txv = new CircleView(getContext());
txv.setBackgroundResource(R.drawable.text_view_border);
txv.setGravity(Gravity.CENTER);
txv.setOnClickListener(itemClickListener);
txv.setText(keyword);
txv.setTextColor(Color.WHITE);
txv.setPadding(8, 6, 8, 6);
txv.setSingleLine(true);
int r = random.nextInt(256);
int g= random.nextInt(256);
int b = random.nextInt(256);
int mColor = Color.rgb(r, g, b);
GradientDrawable myGrad = (GradientDrawable)txv.getBackground();
myGrad.setColor(mColor);
// txv.setBackgroundColor(mColor);
// 获取文本长度
Paint paint = txv.getPaint();
int strWidth = (int) Math.ceil(paint.measureText(keyword));
xy[IDX_TXT_LENGTH] = strWidth;
// 第一次修正:修正x坐标
if (xy[IDX_X] + strWidth > width - (xItem >> 1)) {
int baseX = width - strWidth;
// 减少文本右边缘一样的概率
xy[IDX_X] = baseX - xItem + random.nextInt(xItem >> 1);
} else if (xy[IDX_X] == 0) {
// 减少文本左边缘一样的概率
xy[IDX_X] = Math.max(random.nextInt(xItem), xItem / 3);
更多Android高级工程师进阶学习资料
进阶学习视频
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
adientDrawable)txv.getBackground();
myGrad.setColor(mColor);
// txv.setBackgroundColor(mColor);
// 获取文本长度
Paint paint = txv.getPaint();
int strWidth = (int) Math.ceil(paint.measureText(keyword));
xy[IDX_TXT_LENGTH] = strWidth;
// 第一次修正:修正x坐标
if (xy[IDX_X] + strWidth > width - (xItem >> 1)) {
int baseX = width - strWidth;
// 减少文本右边缘一样的概率
xy[IDX_X] = baseX - xItem + random.nextInt(xItem >> 1);
} else if (xy[IDX_X] == 0) {
// 减少文本左边缘一样的概率
xy[IDX_X] = Math.max(random.nextInt(xItem), xItem / 3);
更多Android高级工程师进阶学习资料
进阶学习视频
[外链图片转存中…(img-tCyXmdTY-1714693820919)]
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-ZNE5rcrf-1714693820920)]
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!