模仿搜狗浏览器加载小球

2024-08-22 03:18

本文主要是介绍模仿搜狗浏览器加载小球,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

用过android 搜狗浏览器客户端的用户就知道,每次加载页面就会有个下落的加载小球,为了实现它,我上网查资料,在泡在网上的日子上面找到了相关资料,但是没有具体详细的教程,所以我就分享一下我的思路。

先上一张效果图(由于博主一直没有找到怎么在博客中播放gif,所以就来一张静态的吧,要想看具体效果,清下载源码编译):
这里写图片描述

具体思路:

1、继承View自写控件,并获取控件长宽。
2、根据已经获取的长宽,设置小球下落的距离。
3、确定小球开始变形的临界点,小球下落至此就开始画椭圆和小球影子。
4、用ValueAnimator控制小球下落的速度和获取小球的下落位置,并且随时更新小球。

运动过程图形解说:

这里写图片描述

主要实现代码:

package com.tielizi.loaderanimation;import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;import java.util.Random;/*** Created by Administrator on 2015/10/12.*/
public class LoaderView extends View {private int radious;//半径private float ratio;//下降比例private Paint shadowPaint,circlePaint;//画下落的影子和小球的画笔private float endX,startY,endY,currentDropDistance,dropDistance,changedistance,currentY = 0;private RectF rectF;private boolean flag;//标记标记线程是否结束private String [] color = new String[]{"#9966FF","#FF6600","#FF00FF","#66FF33","#FF0000"};public LoaderView(Context context) {super(context);init();}public LoaderView(Context context, AttributeSet attrs) {super(context, attrs);init();}public LoaderView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();}//    public LoaderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//        init();
//    }private void init(){flag = true;circlePaint = new Paint();circlePaint.setStyle(Paint.Style.FILL);circlePaint.setAntiAlias(true);shadowPaint = new Paint();shadowPaint.setColor(Color.GRAY);shadowPaint.setStyle(Paint.Style.FILL);shadowPaint.setAntiAlias(true);new Thread(new Runnable() {//线程改变画笔颜色@Overridepublic void run() {while(flag){circlePaint.setColor(Color.parseColor(color[(new Random()).nextInt(color.length)]));try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();}@Overrideprotected void onDraw(Canvas canvas) {Log.i("px","Draw!");if(currentY == 0){executeValueAnimatior();}else{currentDropDistance = currentY-startY;changedistance = dropDistance*3/4;ratio = (currentDropDistance - changedistance)/(dropDistance/4)/2;drawDropCircle(canvas);}super.onDraw(canvas);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {Log.i("px","Ready!");radious = getWidth()/20;endX = getWidth()/2;endY = getHeight()*2/3;startY = endY*2/3;dropDistance = endY/3;super.onMeasure(widthMeasureSpec, heightMeasureSpec);}private void drawDropCircle(Canvas canvas){//话下落的小球if(currentDropDistance<changedistance){canvas.drawCircle(endX,currentY,radious,circlePaint);}else{drawShadow(canvas);rectF = new RectF(endX-radious*(1+ratio),currentY-radious,endX+radious*(1+ratio),currentY+radious*(1-ratio));canvas.drawOval(rectF,circlePaint);}}private void drawShadow(Canvas canvas){//画影子rectF = new RectF(endX-radious*(1+ratio)/2,endY+radious/4,endX+radious*(1+ratio)/2,endY+radious/2+radious/10);canvas.drawOval(rectF,shadowPaint);
//        canvas.drawCircle(endX,endY+radious,radious*ratio,paint);}private void executeValueAnimatior(){//下落的小球位置的动画参数变化ValueAnimator valueAnimator = ValueAnimator.ofInt((int)startY,(int)endY);valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {currentY = (Integer)animation.getAnimatedValue();invalidate();}});valueAnimator.setInterpolator(new AccelerateInterpolator(1.2f));valueAnimator.setRepeatCount(-1);valueAnimator.setRepeatMode(2);valueAnimator.setDuration(500);valueAnimator.start();}public void setFlag(boolean b){//设置为线程结束flag = b;Log.i("px","结束线程!");}
}

好了。就是这么简单。

源码下载

相关博客:
时钟加载View

更多教程请访问博主博客。有什么问题可以私信博主。

这篇关于模仿搜狗浏览器加载小球的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1095085

相关文章

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

JavaWeb-WebSocket浏览器服务器双向通信方式

《JavaWeb-WebSocket浏览器服务器双向通信方式》文章介绍了WebSocket协议的工作原理和应用场景,包括与HTTP的对比,接着,详细介绍了如何在Java中使用WebSocket,包括配... 目录一、概述二、入门2.1 POM依赖2.2 编写配置类2.3 编写WebSocket服务2.4 浏

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

SpringBoot项目删除Bean或者不加载Bean的问题解决

《SpringBoot项目删除Bean或者不加载Bean的问题解决》文章介绍了在SpringBoot项目中如何使用@ComponentScan注解和自定义过滤器实现不加载某些Bean的方法,本文通过实... 使用@ComponentScan注解中的@ComponentScan.Filter标记不加载。@C

springboot 加载本地jar到maven的实现方法

《springboot加载本地jar到maven的实现方法》如何在SpringBoot项目中加载本地jar到Maven本地仓库,使用Maven的install-file目标来实现,本文结合实例代码给... 在Spring Boothttp://www.chinasem.cn项目中,如果你想要加载一个本地的ja

最好用的WPF加载动画功能

《最好用的WPF加载动画功能》当开发应用程序时,提供良好的用户体验(UX)是至关重要的,加载动画作为一种有效的沟通工具,它不仅能告知用户系统正在工作,还能够通过视觉上的吸引力来增强整体用户体验,本文给... 目录前言需求分析高级用法综合案例总结最后前言当开发应用程序时,提供良好的用户体验(UX)是至关重要

MyBatis延迟加载的处理方案

《MyBatis延迟加载的处理方案》MyBatis支持延迟加载(LazyLoading),允许在需要数据时才从数据库加载,而不是在查询结果第一次返回时就立即加载所有数据,延迟加载的核心思想是,将关联对... 目录MyBATis如何处理延迟加载?延迟加载的原理1. 开启延迟加载2. 延迟加载的配置2.1 使用

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

Flutter 进阶:绘制加载动画

绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。