Skip to content

Commit

Permalink
补全包内容
Browse files Browse the repository at this point in the history
  • Loading branch information
kingideayou committed Jul 30, 2015
1 parent d72c7b6 commit 2fde718
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/src/main/java/me/next/tagclouddemo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.next.tagclouddemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import me.next.tagview.TagCloudView;


public class MainActivity extends AppCompatActivity implements TagCloudView.OnTagClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TagCloudView tagCloudView = (TagCloudView) findViewById(R.id.tag_cloud_view);
List<String> tags = new ArrayList<>();
for (int i = 0; i < 20; i++) {
tags.add("标签" + i);
}
tagCloudView.setTags(tags);
tagCloudView.setOnTagClickListener(this);
}

@Override
public void onTagClick(int position) {
Toast.makeText(getApplicationContext(), "position : " + position,
Toast.LENGTH_SHORT).show();
}
}
24 changes: 24 additions & 0 deletions tagview/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc3"

defaultConfig {
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
}
17 changes: 17 additions & 0 deletions tagview/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/NeXT/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
182 changes: 182 additions & 0 deletions tagview/src/main/java/me/next/tagview/TagCloudView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package me.next.tagview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
* Created by NeXT on 15-7-29.
*/
public class TagCloudView extends ViewGroup{

private static final String TAG = TagCloudView.class.getSimpleName();
private List<String> tags;

private LayoutInflater mInflater;
private OnTagClickListener onTagClickListener;

private int sizeWidth;
private int sizeHeight;

private float mTagSize;
private int mTagColor;
private int mBackground;
private int mViewBorder;
private int mTagBorderHor;
private int mTagBorderVer;

private static final int DEFAULT_TEXT_COLOR = Color.WHITE;
private static final int DEFAULT_TEXT_SIZE = 14;
private static final int DEFAULT_TEXT_BACKGROUND = R.drawable.tag_background;
private static final int DEFAULT_VIEW_BORDER = 6;
private static final int DEFAULT_TEXT_BORDER_HORIZONTAL = 5;
private static final int DEFAULT_TEXT_BORDER_VERTICAL = 5;

public TagCloudView(Context context) {
this(context, null);
}

public TagCloudView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

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

mInflater = LayoutInflater.from(context);

TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.TagCloudView,
defStyleAttr,
defStyleAttr
);

mTagSize = a.getDimension(R.styleable.TagCloudView_tcvTextSize, DEFAULT_TEXT_SIZE);
mTagColor = a.getColor(R.styleable.TagCloudView_tcvTextColor, DEFAULT_TEXT_COLOR);
mBackground = a.getResourceId(R.styleable.TagCloudView_tcvBackground, DEFAULT_TEXT_BACKGROUND);
mViewBorder = a.getDimensionPixelSize(R.styleable.TagCloudView_tcvBorder, DEFAULT_VIEW_BORDER);
mTagBorderHor = a.getDimensionPixelSize(
R.styleable.TagCloudView_tcvItemBorderHorizontal, DEFAULT_TEXT_BORDER_HORIZONTAL);
mTagBorderVer = a.getDimensionPixelSize(
R.styleable.TagCloudView_tcvItemBorderVertical, DEFAULT_TEXT_BORDER_VERTICAL);

a.recycle();
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}

/**
* 计算 ChildView 宽高
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
/**
* 计算 ViewGroup 上级容器为其推荐的宽高
*/
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

//计算 childView 宽高
measureChildren(widthMeasureSpec, heightMeasureSpec);

int childWidth;
int childHeight;
// int marginLeft = 5;
// int marginTop = 8;
int totalWidth = 0;
int totalHeight = mTagBorderVer;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
childWidth = child.getMeasuredWidth();
childHeight = child.getMeasuredHeight();

totalWidth += childWidth + mViewBorder;

if (i == 0) {
totalHeight = childHeight + mViewBorder;
}
// + marginLeft 保证最右侧与 ViewGroup 右边距有边界
if (totalWidth + mTagBorderHor > sizeWidth) {
totalWidth = mViewBorder;
totalHeight += childHeight + mTagBorderVer;
child.layout(
totalWidth + mTagBorderHor,
totalHeight - childHeight,
totalWidth + childWidth + mTagBorderHor,
totalHeight);
totalWidth += childWidth;
} else {
child.layout(
totalWidth - childWidth + mTagBorderHor,
totalHeight - childHeight,
totalWidth + mTagBorderHor,
totalHeight);
}
}
totalHeight += mViewBorder;

/**
* 高度根据设置改变
* 如果为 MATCH_PARENT 则充满父窗体,否则根据内容自定义高度
*/
setMeasuredDimension(
sizeWidth,
(heightMode == MeasureSpec.EXACTLY ? sizeHeight : totalHeight));

}

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return super.generateLayoutParams(attrs);
}

public void setTags(List<String> tagList) {
this.tags = tagList;
if (tags != null && tags.size() > 0) {
for (int i = 0; i < tags.size(); i++) {
TextView tagView = (TextView) mInflater.inflate(R.layout.item_tag, null);
tagView.setBackgroundResource(mBackground);
tagView.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTagSize);
tagView.setTextColor(mTagColor);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tagView.setLayoutParams(layoutParams);
tagView.setText(tags.get(i));
final int finalI = i;
tagView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (onTagClickListener != null) {
onTagClickListener.onTagClick(finalI);
}
}
});
addView(tagView);
}
}
postInvalidate();
}

public void setOnTagClickListener(OnTagClickListener onTagClickListener) {
this.onTagClickListener = onTagClickListener;
}

public interface OnTagClickListener{
void onTagClick(int position);
}

}
18 changes: 18 additions & 0 deletions tagview/src/main/res/drawable/tag_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true">
<shape>
<corners android:radius="100dp"/>
<solid android:color="#FF9800" />
</shape>
</item>

<item android:state_pressed="false">
<shape>
<corners android:radius="100dp"/>
<solid android:color="#f9c813" />
</shape>
</item>

</selector>
13 changes: 13 additions & 0 deletions tagview/src/main/res/layout/item_tag.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
tools:text="哈哈哈"
android:textColor="#FFFFFF"
android:background="@drawable/tag_background"/>
9 changes: 9 additions & 0 deletions tagview/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu>
6 changes: 6 additions & 0 deletions tagview/src/main/res/values-w820dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
<dimen name="activity_horizontal_margin">64dp</dimen>
</resources>
5 changes: 5 additions & 0 deletions tagview/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
6 changes: 6 additions & 0 deletions tagview/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<resources>
<string name="app_name">TagView</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>

0 comments on commit 2fde718

Please sign in to comment.