├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable │ │ │ │ └── ic_moment_liked.png │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ ├── attrs.xml │ │ │ │ └── styles.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── java │ │ │ └── widget │ │ │ │ ├── praisewidget │ │ │ │ ├── bean │ │ │ │ │ └── PraiseBean.java │ │ │ │ ├── widget │ │ │ │ │ ├── SpannableStringBuilderAllVer.java │ │ │ │ │ ├── CustomImageSpan.java │ │ │ │ │ └── PraiseWidget.java │ │ │ │ └── clickable │ │ │ │ │ └── PraiseClick.java │ │ │ │ └── Demo.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── widget │ │ │ └── praisewidget │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── widget │ │ └── praisewidget │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── img └── praise widget.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /img/praise widget.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/img/praise widget.gif -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_moment_liked.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/app/src/main/res/drawable/ic_moment_liked.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/razerdp/PraiseWidget/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .idea 10 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | PraiseWidget 3 | 等%d人 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 21 11:34:03 PDT 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/java/widget/praisewidget/bean/PraiseBean.java: -------------------------------------------------------------------------------- 1 | package widget.praisewidget.bean; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by 大灯泡 on 2015/11/21. 7 | */ 8 | public class PraiseBean implements Serializable { 9 | public String userNick;//点赞用户的名字 10 | public int userId;//点赞用户的ID 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/test/java/widget/praisewidget/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package widget.praisewidget; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/androidTest/java/widget/praisewidget/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package widget.praisewidget; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\AndroidSDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "widget.praisewidget" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.1.1' 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PraiseWidget 2 | // 这个是点赞控件哦,适用于社交类app
3 | ### 实际效果可以看看我在[朋友圈项目](https://github.com/razerdp/FriendCircle)的使用 4 | 5 | 实现流程:http://blog.csdn.net/mkfrank/article/details/49963779 6 |
7 | ## 效果图:
8 | ![image](https://github.com/razerdp/PraiseWidget/blob/master/img/praise%20widget.gif) 9 | ## 使用方法:
10 | 在xml定义控件,findViewById后,使用setDataByArray传入数据(本例子用的是PraiseBean),您可以改成你需要用的
11 | 正因为如此,我并没有抽取为一个Library
12 | 如果需要跟显示图一样超过5行就显示“等xx人”,**请跟TextView一样给定android:maxLines="5"** 13 |
14 | ## 可定义的属性:(attrs.xml)
15 | ```html 16 | 17 | 18 | //点击的背景色,默认全透明 19 | //文字颜色,默认蓝 20 | //文字大小,默认16sp 21 | //第一个点赞的图标,默认一个蓝色的心心 22 | 23 | ``` 24 | ## 注意事项:
25 | 因为使用了缓存,所以如果在activity引用了,请务必在onDestroy里面调用PraiseWidget.clearPraiseWidgetCache清掉context引用,避免无法回收 26 | -------------------------------------------------------------------------------- /app/src/main/java/widget/praisewidget/widget/SpannableStringBuilderAllVer.java: -------------------------------------------------------------------------------- 1 | package widget.praisewidget.widget; 2 | 3 | import android.text.SpannableStringBuilder; 4 | 5 | /** 6 | * Created by 大灯泡 on 2015/9/30. 7 | */ 8 | public class SpannableStringBuilderAllVer extends SpannableStringBuilder { 9 | public SpannableStringBuilderAllVer() { 10 | super(""); 11 | } 12 | public SpannableStringBuilderAllVer(CharSequence text) { 13 | super(text, 0, text.length()); 14 | } 15 | public SpannableStringBuilderAllVer(CharSequence text, int start, int end){ 16 | super(text,start,end); 17 | } 18 | 19 | public SpannableStringBuilderAllVer append(CharSequence text) { 20 | if (text == null) return this; 21 | int length = length(); 22 | return (SpannableStringBuilderAllVer)replace(length, length, text, 0, text.length()); 23 | } 24 | 25 | 26 | /**该方法在原API里面只支持API21或者以上,这里抽取出来以适应低版本*/ 27 | public SpannableStringBuilderAllVer append(CharSequence text, Object what, int flags) { 28 | if (text == null) return this; 29 | int start = length(); 30 | append(text); 31 | setSpan(what, start, length(), flags); 32 | return this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/widget/praisewidget/clickable/PraiseClick.java: -------------------------------------------------------------------------------- 1 | package widget.praisewidget.clickable; 2 | 3 | import android.content.Context; 4 | import android.text.TextPaint; 5 | import android.text.style.ClickableSpan; 6 | import android.view.View; 7 | import android.widget.Toast; 8 | 9 | /** 10 | * Created by 大灯泡 on 2015/11/21. 11 | */ 12 | public class PraiseClick extends ClickableSpan{ 13 | private static final int DEFAULT_COLOR=0xff517fae; 14 | 15 | private int color; 16 | private int userID; 17 | private String userNick; 18 | private Context mContext; 19 | private int textSize; 20 | 21 | public PraiseClick(Context context, String userNick, int userID, int color) { 22 | mContext = context; 23 | this.userNick = userNick; 24 | this.userID = userID; 25 | this.color = color; 26 | } 27 | 28 | public PraiseClick(Context context, int userID, int color) { 29 | this(context,"",userID,color); 30 | } 31 | 32 | public PraiseClick(Context context, int userID) { 33 | this(context,"",userID,0); 34 | } 35 | 36 | public PraiseClick(Context context, String userNick, int userID) { 37 | this(context,userNick,userID,0); 38 | } 39 | public PraiseClick(Context context, String userNick, int userID, int color,int textSize) { 40 | this(context,userNick,userID,color); 41 | this.textSize=textSize; 42 | } 43 | @Override 44 | public void onClick(View widget) { 45 | Toast.makeText(mContext,"当前用户名是: "+userNick+" 它的ID是: "+userID,Toast.LENGTH_SHORT).show(); 46 | 47 | } 48 | 49 | @Override 50 | public void updateDrawState(TextPaint ds) { 51 | super.updateDrawState(ds); 52 | //去掉下划线 53 | if (color == 0) { 54 | ds.setColor(DEFAULT_COLOR); 55 | } else { 56 | ds.setColor(color); 57 | } 58 | ds.setTextSize(textSize); 59 | ds.setUnderlineText(false); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /app/src/main/java/widget/praisewidget/widget/CustomImageSpan.java: -------------------------------------------------------------------------------- 1 | package widget.praisewidget.widget; 2 | 3 | /** 4 | * Created by 大灯泡 on 2015/9/28. 5 | */ 6 | 7 | import android.content.Context; 8 | import android.graphics.Canvas; 9 | import android.graphics.Paint; 10 | import android.graphics.Rect; 11 | import android.graphics.drawable.Drawable; 12 | import android.text.style.ImageSpan; 13 | 14 | public class CustomImageSpan extends ImageSpan { 15 | 16 | public CustomImageSpan(Drawable drawable) { 17 | super(drawable); 18 | } 19 | public CustomImageSpan(Context context, int resID){ 20 | super(context,resID, ALIGN_BASELINE); 21 | } 22 | 23 | public int getSize(Paint paint, CharSequence text, int start, int end, 24 | Paint.FontMetricsInt fontMetricsInt) { 25 | Drawable drawable = getDrawable(); 26 | Rect rect = drawable.getBounds(); 27 | if (fontMetricsInt != null) { 28 | Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt(); 29 | int fontHeight = fmPaint.bottom - fmPaint.top; 30 | int drHeight = rect.bottom - rect.top; 31 | 32 | int top = drHeight / 2 - fontHeight / 4; 33 | int bottom = drHeight / 2 + fontHeight / 4; 34 | 35 | fontMetricsInt.ascent = -bottom; 36 | fontMetricsInt.top = -bottom; 37 | fontMetricsInt.bottom = top; 38 | fontMetricsInt.descent = top; 39 | } 40 | return rect.right; 41 | } 42 | 43 | @Override 44 | public void draw(Canvas canvas, CharSequence text, int start, int end, 45 | float x, int top, int y, int bottom, Paint paint) { 46 | Drawable drawable = getDrawable(); 47 | canvas.save(); 48 | int transY = 0; 49 | //居中 50 | transY = ((bottom - top) - drawable.getBounds().bottom) / 2 + top; 51 | canvas.translate(x, transY); 52 | drawable.draw(canvas); 53 | canvas.restore(); 54 | } 55 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 23 | 31 |