代码示例见 神秘的gitHub链接 (/view/widget包下均为自定义控件
继承自View类的控件在绘制中分为三个主要过程
在这一阶段确定控件的大小,应对不同specModel采取不用策略
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
一般直接调用super.onMeasure(widthMeasureSpec,heightMeasureSpec)
就可以了。如果不能达到预期效果,就需要先了解一下class#MeasureSpec
在onMeasure函数传入的两个int并不单纯是view的width和height,只有30位用于记录数值,高二位记录specModel 大概,反正不全是用来计数的 。specModel有如下三种,以height属性为例:
在确定view的height和width之后,调用setMeasuredDimension(width,height);
为view设定大小,否则不会生效。
确定view左上位置 具体作用不明,用到再更
这一过程负责绘制你的CustomView,也是实现动画效果的关键部分。根据计时器数指计算参数,调用invalidate()
不断刷新控件实现动画。
示例:仿华为天气:日出日落动画效果
三者调用关系如下图
如果需要额外的信息来配置自定义控件,可以在res/value资源文件下新建Resource File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="自定义控件类名">
<attr name="自定义属性名称" format="属性格式">
</declare-styleable>
</resources>
现在仅定义属性我们还无法在控件加载时获取,获取还需要写构造函数。
关于构造函数有如下三种重载函数
public CustomView(Context context)
在代码中动态为页面动态添加时调用构造器,需要配合setter配置属性
public CustomView(Context context, @Nullable AttributeSet attrs)
从xml文件中构造控件时调用的构造器,获取属性方法如下
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.yourCustomViewClassName);
if (typedArray!=null){
int color = typedArray.getInt(R.styleable.declare_attr , defValue);
// 注意String类型没有默认值,如果xml文件中没有配置获取为null
String text = typedArray.getString(R.styleable.declare_attr);
// 获取dimension类型时,默认值单位为px
float width = typedArray.getDimension(R.styleable.declare_attr, defValue);
typedArray.recycle();
}
public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
从xml中构造控件且使用theme attr style时调用。大概用不到,用到再更
这里仅对Android Anime效果做简单概述
实现较为简单但是由于不会画图所以从来没有试过
补间动画有四种动画效果:alpha 透明度 / rotate 旋转 / scale 缩放 / translate 平移
配置补间动画需要在res/anim资源文件夹下新建Animation Resource File。配置属性见开发文档,下面以alpha示例
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.8"
android:toAlpha="1"
android:duration="3000"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
</alpha>
页面布局
<ImageView
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="bg pic"/>
bg = findViewById(R.id.background);
Animation animation = AnimationUtils.loadAnimation(SplashActivity.this,R.anim.yourResoucreFileName);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//动画开始时回调函数
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束后回调函数
}
@Override
public void onAnimationRepeat(Animation animation) {
//动画重复时回调函数
}
});
bg.startAnimation(animation);
今天分享的主题是工行去 O数据库选型与分布式架构设计。 图片来自 Pexels 本次分...
前言 在之前的一篇文章 详解递归的正确打开方式 中,我们详细讲解了经典的斐波那...
一个文件存储在哪个 DataNode 节点的哪个位置的元数据信息metadata上是由 NameNo...
最近在前台导入3000条记录时,经常发现只导入大概500条记录,然后就会报ERROR Tr...
效果良好的表属性设置: 复制代码 代码如下: table cellSpacing="0" cellPadding...
本文实例讲述了Ajax对缓存的处理方法。分享给大家供大家参考,具体如下: 缓存 ...
在封装Vue组件时,我依旧会交叉使用函数式组件的方式来实现。关于函数式组件,我...
Spring MVC的文件下载实例详解 读取文件 要下载文件,首先是将文件内容读取进来...
一、index.js ReactDOM.render( React.StrictMode TodoList / /React.StrictMode...
3月份GitHub上最热门的开源项目排行已经出炉啦,一起来看看吧: 1. awesome-comp...