Android仿抖音点击效果

640?wx_fmt=gif

640?wx_fmt=jpeg

Linux编程 点击右侧关注,免费入门到精通! 640?wx_fmt=jpeg


作者丨wish_xy

https://www.jianshu.com/p/1d17c38a3db1


学习自定义view,想找点东西耍一下,刚好看到抖音的点赞效果不错,尝试一下。

640?wx_fmt=gif抖音效果:


640?wx_fmt=gif


话不多说,先上代码:


 
 

public class Love extends RelativeLayout {
    private Context mContext;
    float[] num = {-30-2002030};//随机心形图片角度

    public Love(Context context{
        super(context);
        initView(context);
    }

    public Love(Context context, @Nullable AttributeSet attrs{
        super(context, attrs);
        initView(context);
    }

    public Love(Context context, @Nullable AttributeSet attrs, int defStyleAttr{
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context{
        mContext = context;
    }

    @Override
    protected void dispatchDraw(Canvas canvas
{
        super.dispatchDraw(canvas);
        ImageView imageView = new ImageView(mContext);
        LayoutParams params = new LayoutParams(100100);
        params.leftMargin = getWidth() - 200;
        params.topMargin = getHeight() / 2 - 300;
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
        imageView.setLayoutParams(params);
        addView(imageView);

        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v
{
                Toast.makeText(mContext, "这里是点击爱心的动画,待展示", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event
{

        final ImageView imageView = new ImageView(mContext);
        LayoutParams params = new LayoutParams(300300);
        params.leftMargin = (intevent.getX() - 150;
        params.topMargin = (intevent.getY() - 300;
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
        imageView.setLayoutParams(params);
        addView(imageView);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scale(imageView, "scaleX"2f, 0.9f, 1000))
                .with(scale(imageView, "scaleY"2f, 0.9f, 1000))
                .with(rotation(imageView, 00, num[new Random().nextInt(4)]))
                .with(alpha(imageView, 011000))
                .with(scale(imageView, "scaleX"0.9f, 150150))
                .with(scale(imageView, "scaleY"0.9f, 150150))
                .with(translationY(imageView, 0-600800400))
                .with(alpha(imageView, 10300400))
                .with(scale(imageView, "scaleX"13f, 700400))
                .with(scale(imageView, "scaleY"13f, 700400));

        animatorSet.start();
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation
{
                super.onAnimationEnd(animation);
                removeViewInLayout(imageView);
            }
        });
        return super.onTouchEvent(event);
    }

    public static ObjectAnimator scale(View view, String propertyName, float fromfloat to, long time, long delayTime{
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , propertyName
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationX(View view, float fromfloat to, long time, long delayTime{
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationX"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator translationY(View view, float fromfloat to, long time, long delayTime{
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "translationY"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator alpha(View view, float fromfloat to, long time, long delayTime{
        ObjectAnimator translation = ObjectAnimator.ofFloat(view
                , "alpha"
                , from, to);
        translation.setInterpolator(new LinearInterpolator());
        translation.setStartDelay(delayTime);
        translation.setDuration(time);
        return translation;
    }

    public static ObjectAnimator rotation(View view, long time, long delayTime, float... values{
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, "rotation", values);
        rotation.setDuration(time);
        rotation.setStartDelay(delayTime);
        rotation.setInterpolator(new TimeInterpolator() {
            @Override
            public float getInterpolation(float input
{
                return input;
            }
        });
        return rotation;
    }
    }


640?wx_fmt=gif实现思路


在点击时触发将心形的图片add到整个view中,然后在执行动画。主要的处理逻辑都在onTouchEvent()事件中,下面我们来详细讲解一下思路和代码:


 
 

@Override
    public boolean onTouchEvent(MotionEvent event) {

        final ImageView imageView = new ImageView(mContext);
        LayoutParams params = new LayoutParams(300300);
        params.leftMargin = (int) event.getX() - 150;
        params.topMargin = (int) event.getY() - 300;
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));
        imageView.setLayoutParams(params);
        addView(imageView);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(scale(imageView, "scaleX"2f, 0.9f, 1000))
                .with(scale(imageView, "scaleY"2f, 0.9f, 1000))
                .with(rotation(imageView, 00num[new Random().nextInt(4)]))
                .with(alpha(imageView, 011000))
                .with(scale(imageView, "scaleX"0.9f, 150150))
                .with(scale(imageView, "scaleY"0.9f, 150150))
                .with(translationY(imageView, 0-600800400))
                .with(alpha(imageView, 10300400))
                .with(scale(imageView, "scaleX"13f, 700400))
                .with(scale(imageView, "scaleY"13f, 700400));

        animatorSet.start();
        animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeViewInLayout(imageView);
            }
        });
        return super.onTouchEvent(event);
    }


首先,我们需要在触摸事件中做监听,当有触摸时,创建一个展示心形图片的ImageView。


 
 

final ImageView imageView = new ImageView(mContext);
    imageView.setImageDrawable(getResources().getDrawable(R.drawable.heart_red));//设置红色心形图片


设置图片展示的位置,是需要在手指触摸的位置上方,即触摸点是心形的下方角的位置。所以我们需要将ImageView设置到手指的位置


 
 

LayoutParams params = new LayoutParams(300300);
 params.leftMargin = (intevent.getX() - 150;
 params.topMargin = (intevent.getY() - 300;
 imageView.setLayoutParams(params);

给imageView add到父view中。

 
 

addView(imageView);


640?wx_fmt=gif设置imageView动画


 
 

AnimatorSet animatorSet = new AnimatorSet();
 animatorSet.play(scale(imageView, "scaleX"2f, 0.9f, 1000))//缩放动画,X轴2倍缩小至0.9倍
                .with(scale(imageView, "scaleY"2f, 0.9f, 1000))//缩放动画,Y轴2倍缩小至0.9倍
                .with(rotation(imageView, 00num[new Random().nextInt(4)]))//旋转动画,随机旋转角度num={-30.-20,0,20,30}
                .with(alpha(imageView, 011000))//渐变透明度动画,透明度从0-1.
                .with(scale(imageView, "scaleX"0.9f, 150150))//缩放动画,X轴0.9倍缩小至1倍
                .with(scale(imageView, "scaleY"0.9f, 150150))//缩放动画,Y轴0.9倍缩小至1倍
                .with(translationY(imageView, 0-600800400))//平移动画,Y轴从0向上移动600单位
                .with(alpha(imageView, 10300400))//透明度动画,从1-0
                .with(scale(imageView, "scaleX"13f, 700400))//缩放动画,X轴1倍放大至3倍
                .with(scale(imageView, "scaleY"13f, 700400));//缩放动画,Y轴1倍放大至3倍
animatorSet.start();


当然,我们不可能无限制的增加view,在view消失之后,需要手动的移除改ImageView。


 
 

animatorSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                removeViewInLayout(imageView);
            }
        });


640?wx_fmt=gif效果如下:


640?wx_fmt=gif


 推荐↓↓↓ 

640?wx_fmt=png

?16个技术公众号】都在这里!

涵盖:程序员大咖、源码共读、程序员共读、数据结构与算法、黑客技术和网络安全、大数据科技、编程前端、Java、Python、Web编程开发、Android、iOS开发、Linux、数据库研发、幽默程序员等。

640?wx_fmt=png万水千山总是情,点个 “ 好看” 行不行
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 仿抖音APP下拉刷新功能,首先分析这个效果的实现思路,大致如下:   1、上拉时页面有翻页效果,可以用scrollview的pagingEnabled来实现,也就是说列表页不管你用tableview还是collectionview,只要每个cell是全屏的就可以。   2、下拉:当页面不是停留在第一个cell时,下拉就只是scrollView的滚动效果,不会触发刷新,当页面停留在第一个cell,也就是说scrollView.contentOffset.y = 0的时候,手指下拉才会触发刷新效果,并且下拉时scrollView不动,也就是没有scrollview的弹性效果,因此scrollView.bounces = NO。   3、既然下拉时scrollView不动,就不能使用代理来监听scrollView的滑动实现刷新,于是我想到了用touches的系列方法来监控手指下滑位移。   4、动画分解有五步:   (1)下拉时“推荐、附近”的那个导航条和“下拉刷新内容”的视图有渐隐渐显的效果,位置也随着手指下移,可以通过手指下滑位移计算alpha来实现   (2)下拉时,“下拉刷新内容”的视图右边那个有缺口的小圆环会随着手指滑动转圈,下滑时逆时针旋转   (3)下滑一定距离后如果不松手,又继续上滑,会执行前两步的反效果,圆环顺时针旋转,手指停在屏幕上,圆环就停止转动   (4)下滑到某个临界点,导航条和刷新视图都不再移动(此时导航条已经完全透明),所以可以通过计算起始点和当前点移动距离来计算透明度、位移、旋转角度,这些操作都在touchesMoved中实现   (5)到临界点松手后,导航条和刷新视图都回到原始位置,小圆环一直顺时针转圈,直到刷新结束,停止动画,隐藏刷新视图,显示导航条,如果没达到临界点就松手,不会触发刷新。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值