- 源码类别:多页切换TabHost
- 源码大小:未知
- 编码格式:gbk,utf8,不限
- 运行环境:Android studio
现在新版360手机助手的界面都做得挺漂亮的,在切换底部导航时的波纹效果也很好看,刚好最近看了个开源项目才了解到原来Drawable做动画效果也怎么好用,所以就仿照360实现了下带波纹的TabHost。
先来看一下实现后的效果:

说明一下实现要点:
1. 因为我们项目之前用的是FragmentTabHost,所以我直接继承FragmentTabHost来实现动画效果更方便;
2. 波纹动画的实现其实是自定义带动画效果的Drawable,然后将Drawable设置为Tab菜单的背景;
3. 其它的就是一些Tab菜单切换的处理了。
一. 自定义波纹Drawable
自定义Drawable只要继承Drawable并实现以下4个方法,同时实现Animatable接口:
-
public class RippleDrawable extends Drawable implements Animatable {
-
@Override
-
public void draw(Canvas canvas) {
-
-
}
-
-
@Override
-
public void setAlpha(int alpha) {
-
-
}
-
-
@Override
-
public void setColorFilter(ColorFilter colorFilter) {
-
-
}
-
-
@Override
-
public int getOpacity() {
-
-
return PixelFormat.RGBA_8888;
-
}
-
-
@Override
-
public void start() {
-
-
}
-
-
@Override
-
public void stop() {
-
-
}
-
-
@Override
-
public boolean isRunning() {
-
-
return false;
-
}
-
}
这几个方法中最重要的就是draw()方法了,相信自定义过View的都知道我们图形就是在这里绘制,这里也一样,其它方法在这里影响不大,最后一个方法用来设置Drawable的颜色格式。要实现动画Drawable需要实现Animatable接口,并实现3个方法如下,其实不实现这个接口也能做动画效果,但还是实现比较好。
下面是整个波纹Drawable的实现代码:
-
-
-
-
-
public class RippleDrawable extends Drawable implements Animatable {
-
-
-
-
-
public static final int MODE_LEFT = 1;
-
public static final int MODE_MIDDLE = 2;
-
public static final int MODE_RIGHT = 3;
-
-
private int mMode = MODE_MIDDLE;
-
-
private Paint mPaintFront;
-
private Paint mPaintBehind;
-
-
private RectF mRect;
-
-
private int mHalfWidth;
-
private int mHalfHeight;
-
-
private int mRadius;
-
-
private int mDivideSpace;
-
-
private int mFullSpace;
-
-
private ValueAnimator mValueAnimator;
-
-
-
public RippleDrawable(int frontColor, int behindColor, int mode) {
-
mPaintFront = new Paint(Paint.ANTI_ALIAS_FLAG);
-
mPaintFront.setColor(frontColor);
-
mPaintBehind = new Paint(Paint.ANTI_ALIAS_FLAG);
-
mPaintBehind.setColor(behindColor);
-
mRect = new RectF();
-
mMode = mode;
-
}
-
-
@Override
-
public void draw(Canvas canvas) {
-
if (mRadius > mHalfWidth) {
-
int count = canvas.save();
-
canvas.drawCircle(mHalfWidth, mHalfHeight, mHalfWidth, mPaintBehind);
-
canvas.restoreToCount(count);
-
count = canvas.save(); &
本站资源仅限于学习研究,严禁从事商业或者非法活动!
源码搜藏网所有源码来自互联网转载与用户上传分享,如果侵犯了您的权益请与我们联系,我们将在24小时内删除!谢谢!