开源项目GoodView点赞效果

点赞+1效果:

GoodView方法:


使用GoodView的Demo:

   public class MainActivity extends Activity {

        @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main)final GoodView goodView = new GoodView(this);Button button = new Button(this);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {goodView.setText("+1");goodView.show(v);}});}}


 
<span style="font-family: Arial, Helvetica, sans-serif;">实践GitHub开源GoodView:</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>

GoodView.java :

/** Copyright (C) 2016 venshine.cn@gmail.com** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package sunny.example.opengoodview.goodview;import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;/*** 点赞效果** @author venshine*/
@SuppressLint("NewApi")
public class GoodView extends PopupWindow implements IGoodView {private String mText = TEXT;private int mTextColor = TEXT_COLOR;private int mTextSize = TEXT_SIZE;private int mFromY = FROM_Y_DELTA;private int mToY = TO_Y_DELTA;private float mFromAlpha = FROM_ALPHA;private float mToAlpha = TO_ALPHA;private int mDuration = DURATION;private int mDistance = DISTANCE;private AnimationSet mAnimationSet;private boolean mChanged = false;private Context mContext = null;private TextView mGood = null;public GoodView(Context context) {super(context);mContext = context;initView();}private void initView() {RelativeLayout layout = new RelativeLayout(mContext);RelativeLayout.LayoutParams params =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);params.addRule(RelativeLayout.CENTER_HORIZONTAL);params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);mGood = new TextView(mContext);mGood.setIncludeFontPadding(false);mGood.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize);mGood.setTextColor(mTextColor);mGood.setText(mText);mGood.setLayoutParams(params);layout.addView(mGood);setContentView(layout);int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);mGood.measure(w, h);setWidth(mGood.getMeasuredWidth());setHeight(mDistance + mGood.getMeasuredHeight());setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));setFocusable(false);setTouchable(false);setOutsideTouchable(false);mAnimationSet = createAnimation();}/*** 设置文本** @param text*/public void setText(String text) {if (TextUtils.isEmpty(text)) {throw new IllegalArgumentException("text cannot be null.");}mText = text;mGood.setText(text);mGood.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));int w = (int) mGood.getPaint().measureText(text);setWidth(w);setHeight(mDistance + getTextViewHeight(mGood, w));}private static int getTextViewHeight(TextView textView, int width) {int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.AT_MOST);int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);textView.measure(widthMeasureSpec, heightMeasureSpec);return textView.getMeasuredHeight();}/*** 设置文本颜色** @param color*/private void setTextColor(int color) {mTextColor = color;mGood.setTextColor(color);}/*** 设置文本大小** @param textSize*/private void setTextSize(int textSize) {mTextSize = textSize;mGood.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);}/*** 设置文本信息** @param text* @param textColor* @param textSize*/public void setTextInfo(String text, int textColor, int textSize) {setTextColor(textColor);setTextSize(textSize);setText(text);}/*** 设置图片** @param resId*/public void setImage(int resId) {setImage(mContext.getResources().getDrawable(resId));}/*** 设置图片** @param drawable*/public void setImage(Drawable drawable) {if (drawable == null) {throw new IllegalArgumentException("drawable cannot be null.");}if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {mGood.setBackground(drawable);} else {mGood.setBackgroundDrawable(drawable);}mGood.setText("");setWidth(drawable.getIntrinsicWidth());setHeight(mDistance + drawable.getIntrinsicHeight());}/*** 设置移动距离** @param dis*/public void setDistance(int dis) {mDistance = dis;mToY = dis;mChanged = true;setHeight(mDistance + mGood.getMeasuredHeight());}/*** 设置Y轴移动属性** @param fromY* @param toY*/public void setTranslateY(int fromY, int toY) {mFromY = fromY;mToY = toY;mChanged = true;}/*** 设置透明度属性** @param fromAlpha* @param toAlpha*/public void setAlpha(float fromAlpha, float toAlpha) {mFromAlpha = fromAlpha;mToAlpha = toAlpha;mChanged = true;}/*** 设置动画时长** @param duration*/public void setDuration(int duration) {mDuration = duration;mChanged = true;}/*** 重置属性*/public void reset() {mText = TEXT;mTextColor = TEXT_COLOR;mTextSize = TEXT_SIZE;mFromY = FROM_Y_DELTA;mToY = TO_Y_DELTA;mFromAlpha = FROM_ALPHA;mToAlpha = TO_ALPHA;mDuration = DURATION;mDistance = DISTANCE;mChanged = false;mAnimationSet = createAnimation();}/*** 展示** @param v*/public void show(View v) {if (!isShowing()) {int offsetY = -v.getHeight() - getHeight();showAsDropDown(v, v.getWidth() / 2 - getWidth() / 2, offsetY);if (mAnimationSet == null || mChanged) {mAnimationSet = createAnimation();mChanged = false;}mGood.startAnimation(mAnimationSet);}}/*** 动画** @return*/private AnimationSet createAnimation() {mAnimationSet = new AnimationSet(true);TranslateAnimation translateAnim = new TranslateAnimation(0, 0, mFromY, -mToY);AlphaAnimation alphaAnim = new AlphaAnimation(mFromAlpha, mToAlpha);mAnimationSet.addAnimation(translateAnim);mAnimationSet.addAnimation(alphaAnim);mAnimationSet.setDuration(mDuration);mAnimationSet.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {if (isShowing()) {new Handler().post(new Runnable() {@Overridepublic void run() {dismiss();}});}}@Overridepublic void onAnimationRepeat(Animation animation) {}});return mAnimationSet;}
}
IGoodView.java :

/** Copyright (C) 2016 venshine.cn@gmail.com** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package sunny.example.opengoodview.goodview;import android.graphics.Color;/*** @author venshine*/
public interface IGoodView {int DISTANCE = 60;   // 默认移动距离int FROM_Y_DELTA = 0; // Y轴移动起始偏移量int TO_Y_DELTA = DISTANCE; // Y轴移动最终偏移量float FROM_ALPHA = 1.0f;    // 起始时透明度float TO_ALPHA = 0.0f;  // 结束时透明度int DURATION = 800; // 动画时长String TEXT = ""; // 默认文本int TEXT_SIZE = 16; // 默认文本字体大小int TEXT_COLOR = Color.BLACK;   // 默认文本字体颜色}

MainActivity.java :

/** Copyright (C) 2016 venshine.cn@gmail.com** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**      http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package sunny.example.opengoodview;import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;import sunny.example.opengoodview.goodview.GoodView;/*** Demo** @author venshine*/
public class MainActivity extends ActionBarActivity {GoodView mGoodView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mGoodView = new GoodView(this);}//android:onClick="good"public void good(View view) {((ImageView) view).setImageResource(R.drawable.good_checked);mGoodView.setText("+1");mGoodView.show(view);}public void good2(View view) {((ImageView) view).setImageResource(R.drawable.good_checked);mGoodView.setImage(getResources().getDrawable(R.drawable.good_checked));mGoodView.show(view);}public void collection(View view) {((ImageView) view).setImageResource(R.drawable.collection_checked);mGoodView.setTextInfo("收藏成功", Color.parseColor("#f66467"), 12);mGoodView.show(view);}public void bookmark(View view) {((ImageView) view).setImageResource(R.drawable.bookmark_checked);mGoodView.setTextInfo("收藏成功", Color.parseColor("#ff941A"), 12);mGoodView.show(view);}public void reset(View view) {((ImageView) findViewById(R.id.good)).setImageResource(R.drawable.good);((ImageView) findViewById(R.id.good2)).setImageResource(R.drawable.good);((ImageView) findViewById(R.id.collection)).setImageResource(R.drawable.collection);((ImageView) findViewById(R.id.bookmark)).setImageResource(R.drawable.bookmark);mGoodView.reset();}}



本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/348805.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

开源RefreshListView下拉刷新效果

1、AnimationDrawable java.lang.Object ↳android.graphics.drawable.Drawable ↳android.graphics.drawable.DrawableContainer ↳android.graphics.drawable.AnimationDrawable 文档概述&#xff1a;An object used to create frame-by-frame animations, defined …

View的三大流程之View的测量

1、public class View extends Objectimplements Drawable.Callback KeyEvent.Callback AccessibilityEventSourcejava.lang.Object ↳android.view.View Class Overview This class represents the basic building block for user interface components. A View occupies a …

ListView的使用用ViewHolder提升效率

public class ListView extends AbsListViewjava.lang.Object ↳android.view.View ↳android.view.ViewGroup ↳android.widget.AdapterView<T extends android.widget.Adapter> ↳android.widget.AbsListView ↳android.widget.ListView Class Ove…

电脑还原Mac地址

之前修改过电脑Mac&#xff0c;现在与手机Mac冲突&#xff0c;导致手机无法连接校园网。 控制面板——》打开网络和共享中心——》选择更改适配器设置 本地连接——》右键选择属性 上图选择配置 高级——》在下拉菜单中选择网络地址——》右边选为“不存在” 之后本地连接会自…

聊天ListView使用ViewHolder

聊天界面会展示至少两种布局&#xff0c;即收到消息和发送消息。这样一个ListView与平时使用的ListView的最大不同&#xff0c;在于它有两个不同的布局&#xff1a;收到的布局和发送的布局。需要利用Adapter实现这样的效果。即需要在获取布局的时候判断该获取哪个布局。 public…

动态改变ListView布局

在getView()时&#xff0c;通过判断选择加载不同的布局。 点击某个item的时候&#xff0c;变为foucus状态&#xff0c;其他的items还原。 下面用两个方法给item设置两个不同的布局。 //foucus状态&#xff0c;显示一个ImageViewprivate View addFocusView(int i) {ImageView…

jpa 关系拥有方_JPA:确定关系的归属方

jpa 关系拥有方使用Java Persistence API&#xff08;JPA&#xff09;时&#xff0c;通常需要在两个实体之间创建关系。 这些关系是通过使用外键在数据模型&#xff08;例如数据库&#xff09;中定义的&#xff0c;而在我们的对象模型&#xff08;例如Java&#xff09;中则使用…

获取View坐标

滑动一个View&#xff0c;即移动一个View&#xff0c;改变其当前所处的位置&#xff0c;通过不断改变View的坐标实现滑动的效果。 1、Android坐标系&#xff1a;坐标原点在屏幕左上角。 public void getLocationOnScreen(int[] location) Computes the coordinates of this vi…

李宏毅 课程打包_按功能而不是按层打包课程

李宏毅 课程打包大多数企业Java应用程序在设计上都有一些相似之处。 这些应用程序的打包通常由它们使用的框架&#xff08;如Spring&#xff0c;EJB或Hibernate等&#xff09;驱动。或者&#xff0c;您可以按功能对打包进行分组。 像任何其他有关建模的项目一样&#xff0c;这也…

Java反射,但速度更快

在编译时不知道Java类的最快方法是什么&#xff1f; Java框架通常会这样做。 很多。 它可以直接影响其性能。 因此&#xff0c;让我们对不同的方法进行基准测试&#xff0c;例如反射&#xff0c;方法句柄和代码生成。 用例 假设我们有一个简单的Person类&#xff0c;其中包含名…

Git时间

1、Git是目前世界上最先进的分布式版本控制系统。和集中式版本控制系统相比&#xff0c;分布式版本控制系统的安全性要高很多&#xff0c;因为每个人电脑里都有完整的版本库&#xff0c;某一个人的电脑坏掉了不要紧&#xff0c;随便从其他人那里复制一个就可以了。而集中式版本…

java中update_Java 7 Update 21安全改进的详细信息

java中updateOracle昨天发布了三个Java更新 。 重要的是要注意它们包含一些与安全性相关的更改。 一段时间以来&#xff0c;这些变更中的大多数已经宣布&#xff0c;首先要注意的是Oracle按计划交付。 Oracle公司Java平台安全经理米尔顿史密斯&#xff08;Milton Smith&…

访问GitHub超慢的解决办法

是github某个CDN(Content Delivery Network&#xff0c;即内容分发网络?)被屏蔽所致。 附件--->记事本&#xff08;选择以管理员身份运行&#xff09;——文件——打开C:\Windows\System32\drivers\etc 右下角选择“所有文件” 选择hosts 打开 如图在这行下面添加绑定IP…

学习使用Whally GraalVM!

介绍 在Truffle在神圣的Graal中服务&#xff1a;Graal和Truffle在JVM上进行多语种语言解释的帖子中&#xff0c;我们得到了简短的介绍&#xff0c;并对Graal&#xff0c;Truffle及其周围的一些概念进行了深入研究。 但是&#xff0c;如果不深入研究实用性&#xff0c;那么任何技…

Android样式开发---shape

Thanks to:转载自Keegan小钢 原文链接&#xff1a;http://keeganlee.me/post/android/20150830 一个应用&#xff0c;应该保持一套统一的样式&#xff0c;包括Button、EditText、ProgressBar、Toast、Checkbox等各种控件的样式&#xff0c;还包括控件间隔、文字大小和颜色、阴影…

SwipeRefreshLayout官方推荐下拉刷新

SwipeRefreshLayoutpublic class SwipeRefreshLayout extends ViewGroup implements NestedScrollingParent, NestedScrollingChildjava.lang.Object↳android.view.View↳android.view.ViewGroup↳ android.support.v4.widget.SwipeRefreshLayout API doc&#xff1a;http://…

开源PagerSlidingTabStrip的使用Tab与ViewPager的完美结合

链接地址&#xff1a;https://github.com/astuetz/PagerSlidingTabStrip 下载PagerSlidingTabStrip-master 将com.astuetz包&#xff0c;res下的下的资源复制进工程 布局文件&#xff1a;activity_main.xml <RelativeLayout xmlns:android"http://schemas.android.co…

android动画详解

转自&#xff1a;工匠若水 http://blog.csdn.net/yanbober 1 背景 不能只分析源码呀&#xff0c;分析的同时也要整理归纳基础知识&#xff0c;刚好有人微博私信让全面说说Android的动画&#xff0c;所以今天来一发Android应用的各种Animation大集合。英文厉害的请直接移步参考…

凹数科技笔试

一、Java 1、成员变量作用域public/protected/defaultprivate/区别&#xff1f; public&#xff1a;该成员变量或其方法对当前类、同一包、子类、其他包都可见&#xff0c;所有类和对象都可以直接访问。 protected&#xff1a;该成员变量或其方法对当前类、同一包、子类都可…

上传至GitHub

在工作目录下&#xff1a; git init git status git add . git commit -m"IndoorLocation" git status git remote add origin githttps://github.com/HiSunny/HelloInLoc.git git pull https://github.com/HiSunny/HelloInLoc.git master git push https://…