开源项目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 …

Neo4j:遍历查询超时

在过去的几周中&#xff0c;我一直在花一些业余时间来创建一个应用程序&#xff0c;该应用程序从Open Roads数据生成运行路线-当然已转换并导入到Neo4j中&#xff01; 我创建了一个用户定义的过程&#xff0c;该过程结合了几个最短路径查询&#xff0c;但是如果它们花费的时间…

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 …

使用LayoutParams设置布局

1、public static class ViewGroup.LayoutParams extends Objectjava.lang.Object ↳android.view.ViewGroup.LayoutParams Class Overview LayoutParams are used by views to tell their parents how they want to be laid out. LayoutParams是ViewGroup的一个内部类, 每个…

记录意外的开关选项

Java开发人员可以做很多事情来使自己的生活以及维护该代码的其他人的生活更加轻松。 在本文中&#xff0c;我将探讨开发人员可以采用的一种非常简单的方法&#xff0c;以使每个人都更轻松。 这篇文章的要点对于每个阅读它的人来说似乎都是显而易见的&#xff0c;但是我发现这样…

View的绘制

1、当测量好一个View后就可以重写onDraw()方法&#xff0c;并在Canvas对象上绘制所需的图形。 public class Canvas extends Objectjava.lang.Object ↳android.graphics.Canvas Public ConstructorsCanvas()Construct an empty raster canvas.Canvas(Bitmap bitmap)Construc…

ViewGroup的测量及绘制

1、ViewGroup的测量public abstract class ViewGroup extends Viewimplements ViewManager ViewParent java.lang.Object ↳android.view.View ↳android.view.ViewGroup Class Overview A ViewGroup is a special view that can contain other views (called children.)…

ejb 2.0 3.0_定义EJB 3.1视图(本地,远程,无接口)

ejb 2.0 3.0这篇文章将讨论使用批注定义EJB视图的可能方法&#xff08;最后我将只提到使用EJB部署描述符&#xff09;。我将重点介绍最新的EJB 3.1视图&#xff0c;这些视图将省略旧的本地&#xff0c;远程和本地接口。 因此&#xff0c;我们可以选择&#xff1a; 远程业务界面…

View的事件分发机制简述

要分析的对象就是MotionEvent&#xff0c;点击事件的事件分发其实就是对MotionEvent事件的分发过程&#xff0c;当MotionEvent产生后&#xff0c;系统需要把这个事件传递给一个具体的View&#xff0c;这个传递过程就是分发过程。这个过程由三个很重要的方法共同完成&#xff1a…

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…

通过投影增强数据模型

介绍 数据模型可能很棘手。 建模可能会更加困难。 有时候&#xff0c;应该放入数据库表中的信息不一定是我们要在每段代码中使用的信息。 和其他许多次一样&#xff0c;Spring来了。 一个称为投影的小功能可以帮助我们在普通界面中仅用几行数据映射数据。 在本文中&#xff0…

ListView常用属性、方法

1、设置item间分割线&#xff0c;分割线高度 android:divider"android:color/darker_gray" android:dividerHeight"10dp" 分割线不仅仅可以设置为一个颜色&#xff0c;也可以设置为一个图片资源。 分割线设置为透明&#xff1a; android:divider"…

具有弹性效果的ListView

Android默认的ListView在滚动到顶端或底端&#xff0c;没有很好的提示&#xff0c;下面通过源码来实现弹性效果。 ListView中有一个控制滑动到边缘的处理方法&#xff1a; protected boolean overScrollBy(int deltaX, int deltaY,int scrollX, int scrollY,int scrollRangeX,…

rememberme多久_使用Spring Security添加RememberMe身份验证

rememberme多久我在“ 向Jiwhiz博客添加社交登录”一文中提到&#xff0c;RememberMe功能不适用于Spring Social Security。 好吧&#xff0c;这是因为该应用程序现在不通过用户名和密码对用户进行身份验证&#xff0c;并且完全依靠社交网站&#xff08;例如Google&#xff0c;…

Java 9:对可选的增强

之前 &#xff0c;我写过Java 8中引入的Optional类&#xff0c;该类用于对可能不存在的值进行建模并减少可能引发NullPointerException的位置的数量。 Java 9向Optional添加了三个新方法&#xff1a; 1. ifPresentOrElse 新ifPresentOrElse方法允许你执行一个动作&#xff0c…

电脑还原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;中则使用…

JDBC连接备忘单

抽象 这是常见数据库的JDBC连接的快速参考。 我似乎必须大量查找此信息&#xff0c;因此我认为最好将所有参考资料放在一个地方。 德比 <dependency><groupId>org.apache.derby</groupId><artifactId>derbyclient</artifactId><version>1…