自定义view-视察动画之雅虎新闻摘要加载

2024-02-16 20:50

本文主要是介绍自定义view-视察动画之雅虎新闻摘要加载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里写图片描述

首先布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><include layout="@layout/fragment_page_first"/><com.hbwj.a20_.LoadingView
        android:id="@+id/thirdScreenView"android:layout_width="match_parent"android:layout_height="match_parent"tools:layout_editor_absoluteY="8dp"tools:layout_editor_absoluteX="8dp" /></RelativeLayout>

fragment_page_first布局和上一篇文章内容一样

LoadingView:自定义加载view

public class LoadingView extends View {// 旋转动画执行的时间private final long ROTATION_ANIMATION_TIME = 1400;private boolean mInitParams;private float mRotationRadius;//绕着旋转的动画的半径private float mCircleRadius;//小圆的半径private float mCurrentRotationAngle;// 小圆的颜色列表private int[] mCircleColors;//整体颜色背景private int mSplashColor = Color.WHITE;// 代表当前状态所画动画private LoadingState mLoadingState;//画笔private Paint mPaint;private int cententX, cententY;private float mCurrentRotationRadius;//当前半径// 空心圆初始半径private float mHoleRadius = 0F;// 屏幕对角线的一半private float mDiagonalDist;public LoadingView(Context context) {this(context, null);}public LoadingView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mCircleColors = context.getResources().getIntArray(R.array.splash_circle_colors);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);if (!mInitParams) {initParams();}if (mLoadingState == null) {mLoadingState = new RotationState();}mLoadingState.drawable(canvas);}//初始化参数private void initParams() {mRotationRadius = getMeasuredWidth() / 4;// 每个小圆的半径 = 大圆半径的 1/8mCircleRadius = mRotationRadius / 8;mInitParams = true;mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setDither(true);cententX = getWidth() / 2;cententY = getHeight() / 2;mDiagonalDist= (float) Math.sqrt(cententX*cententX+cententY*cententY);}/*** 消失:给外部调用*/public void disappear() {//开始聚合动画//关闭动画if (mLoadingState instanceof RotationState) {RotationState rotationState = (RotationState) mLoadingState;rotationState.cancel();}mLoadingState = new MergeState();}public abstract class LoadingState {public abstract void drawable(Canvas canvas);}/*** 展开动画*/public class ExpendState extends LoadingState {ValueAnimator mAnimator;public ExpendState() {if (mAnimator == null) {mAnimator=ObjectAnimator.ofFloat(0,mDiagonalDist);//从0到圆的对角线的一半mAnimator.setDuration(ROTATION_ANIMATION_TIME);mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mHoleRadius= (float) animation.getAnimatedValue();invalidate();}});mAnimator.start();}}@Overridepublic void drawable(Canvas canvas) {//画笔的宽度float strokeWidth=mDiagonalDist-mHoleRadius;
//            float strokeWidth=mDiagonalDist;mPaint.setStrokeWidth(strokeWidth );mPaint.setColor(mSplashColor);mPaint.setStyle(Paint.Style.STROKE);
//            float radius=strokeWidth/2;//向内float radius=strokeWidth/2+mHoleRadius;canvas.drawCircle(cententX,cententY,radius, mPaint);}}/*** 聚合动画*/public class MergeState extends LoadingState {ValueAnimator mAnimator;public MergeState() {if (mAnimator == null) {//从外圆的半径到中心位置mAnimator = ObjectAnimator.ofFloat(mRotationRadius, 0);mAnimator.setDuration(ROTATION_ANIMATION_TIME / 2);mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mCurrentRotationRadius = (float) animation.getAnimatedValue();invalidate();}});// 开始的时候向后然后向前甩mAnimator.setInterpolator(new AnticipateInterpolator(5f));mAnimator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {mLoadingState = new ExpendState();}});//不断重复使用//mAnimator.setRepeatCount(-1);mAnimator.start();}}@Overridepublic void drawable(Canvas canvas) {//绘制白色背景canvas.drawColor(mSplashColor);//画6个圆//没份的角度double precentAngle = 2 * Math.PI / mCircleColors.length;for (int i = 0; i < mCircleColors.length; i++) {mPaint.setColor(mCircleColors[i]);//当前的角度=初始化的角度+旋转的角度double currentAngle = precentAngle * i + mCurrentRotationAngle;float cx = (float) (cententX + mCurrentRotationRadius * Math.cos(currentAngle));float cy = (float) (cententY + mCurrentRotationRadius * Math.sin(currentAngle));canvas.drawCircle(cx, cy, mCircleRadius, mPaint);}}}/*** 旋转动画*/public class RotationState extends LoadingState {ValueAnimator mAnimator;public RotationState() {if (mAnimator == null) {//0-360度mAnimator = ObjectAnimator.ofFloat(0, 2 * (float) Math.PI);mAnimator.setDuration(ROTATION_ANIMATION_TIME);mAnimator.setInterpolator(new LinearInterpolator());mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mCurrentRotationAngle = (float) animation.getAnimatedValue();invalidate();}});//不断重复使用mAnimator.setRepeatCount(-1);mAnimator.start();}}@Overridepublic void drawable(Canvas canvas) {//绘制白色背景canvas.drawColor(mSplashColor);//画6个圆//没份的角度double precentAngle = 2 * Math.PI / mCircleColors.length;for (int i = 0; i < mCircleColors.length; i++) {mPaint.setColor(mCircleColors[i]);//当前的角度=初始化的角度+旋转的角度double currentAngle = precentAngle * i + mCurrentRotationAngle;float cx = (float) (cententX + mRotationRadius * Math.cos(currentAngle));float cy = (float) (cententY + mRotationRadius * Math.sin(currentAngle));canvas.drawCircle(cx, cy, mCircleRadius, mPaint);}}public void cancel() {mAnimator.cancel();}}
}

MainActivity中使用

  final LoadingView loadingView = (LoadingView) findViewById(R.id.thirdScreenView);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {loadingView.disappear();}},2000);

这篇关于自定义view-视察动画之雅虎新闻摘要加载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

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

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

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)是至关重要

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

SpringBoot 自定义消息转换器使用详解

《SpringBoot自定义消息转换器使用详解》本文详细介绍了SpringBoot消息转换器的知识,并通过案例操作演示了如何进行自定义消息转换器的定制开发和使用,感兴趣的朋友一起看看吧... 目录一、前言二、SpringBoot 内容协商介绍2.1 什么是内容协商2.2 内容协商机制深入理解2.2.1 内容

MyBatis延迟加载的处理方案

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

Android WebView的加载超时处理方案

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