本文實例為大家分享了Android實現3D標簽云效果展示的具體代碼,供大家參考,具體內容如下
一、關于3D標簽云
TagCloudView是一個完全基于Android ViewGroup編寫的控件,支持將一組View展示為一個3D標簽云,并支持全方向滾動。
GitHub中的鏈接地址
(一)效果
頁面上標簽的數據可以自己定義,數據頁面可以滑動選擇。
(二)AndroidStudio中使用
1.在build.gradle中添加
compile ‘com.moxun:tagcloudlib:1.0.3'
2.在布局文件中引入
3.設置Adapter 繼承TagsAdapter,實現以下方法
(1)public int getCount();
返回Tag數量
(2)public View getView(Context context, int position, ViewGroup parent);
返回每個Tag實例
(3)public Object getItem(int position);
返回Tag數據
(4)public int getPopularity(int position);
針對每個Tag返回一個值,但是什么作用
4.標簽云對象的屬性設置
二、簡單的使用示例
(一)布局文件activity_main.xml設計
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" android:id = "@+id/activity_main" android:layout_width = "match_parent" android:layout_height = "match_parent" > < com.moxun.tagcloudlib.view.TagCloudView android:id = "@+id/tcv_tags" android:layout_width = "match_parent" android:layout_height = "match_parent" app:autoScrollMode = "uniform" app:radiusPercent = "0.8" /> </ RelativeLayout > |
(二)設計標簽云中的字體的布局
1
2
3
4
5
6
7
8
9
|
<? xml version = "1.0" encoding = "utf-8" ?> <!--單個標簽云中的文本的視圖--> < TextView xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "標簽云" android:textColor = "@color/textcolor_tags" /> |
(三)設計字體的顏色選擇器
(res文件夾下創建color文件夾,創建textcolor_tags.xml)
1
2
3
4
5
6
7
|
<? xml version = "1.0" encoding = "utf-8" ?> <!--標簽云的文本的字體的顏色選擇器--> < selector xmlns:android = "http://schemas.android.com/apk/res/android" > < item android:color = "#f0f" android:state_selected = "true" /> < item android:color = "#000" android:state_selected = "false" /> </ selector > |
(四)創建適配器的類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package com.lwz.cloud; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.moxun.tagcloudlib.view.TagsAdapter; import java.util.List; /** * 標簽云頁面數據的適配器 */ public class CursorTagsAdapter extends TagsAdapter { private List<String> mList; public CursorTagsAdapter( List<String> list) { this .mList = list; } @Override public int getCount() { return mList.size(); } @Override public View getView(Context context, int position, ViewGroup parent) { TextView tv = (TextView) View.inflate(context, R.layout.item_tag, null ); tv.setText(getItem(position)); return tv; } @Override public String getItem( int position) { return mList.get(position); } @Override public int getPopularity( int position) { return 1 ; } @Override public void onThemeColorChanged(View view, int themeColor) { } } |
(五)主方法調用類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
package com.lwz.cloud; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.moxun.tagcloudlib.view.TagCloudView; import java.util.ArrayList; import java.util.List; /** * 標簽云效果界面的設計 */ public class MainActivity extends AppCompatActivity implements TagCloudView.OnTagClickListener { TagCloudView tcvTags; //標簽云對象 List<String> list = new ArrayList<>(); //標簽云數據的集合 List<String> listClick = new ArrayList<>(); //點擊過的標簽云的數據的集合 @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); //給集合添加數據 for ( int i = 0 ; i < 20 ; i++) { list.add( "這是標簽" + i); } tcvTags = (TagCloudView) findViewById(R.id.tcv_tags); //設置標簽云的點擊事件 tcvTags.setOnTagClickListener( this ); //給標簽云設置適配器 CursorTagsAdapter adapter = new CursorTagsAdapter(list); tcvTags.setAdapter(adapter); } /** * 點擊標簽是回調的方法 */ @Override public void onItemClick(ViewGroup parent, View view, int position) { view.setSelected(!view.isSelected()); //設置標簽的選擇狀態 if (view.isSelected()) { //加入集合 listClick.add(list.get(position)); } else { //移除集合 listClick.remove(list.get(position)); } Toast.makeText( this , "點擊過的標簽:" + listClick.toString(), Toast.LENGTH_SHORT).show(); } } |
程序運行后的界面:
點擊幾個標簽后顯示的界面:
這就是標簽云的簡單示例。這個標簽云默認情況下是會勻速滾動的。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/wenzhi20102321/article/details/53517673