博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RevealView
阅读量:5752 次
发布时间:2019-06-18

本文共 4477 字,大约阅读时间需要 14 分钟。

记录一个demo,自己很喜欢 ,坚持不懈向大神学习

。。

先看效果

001.gif

再看代码

<pre>

package github.alex.revealview;

import android.animation.Animator;

import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;

/**

* Created by Miroslaw Stanek on 18.01.15.
*/
public class RevealView extends View {
/**填充色*/
private Paint fillPaint;
/**初始化状态*/
public static final int state_not_started = 0;
/**揭露动画开始*/
public static final int state_fill_started = 1;
/**揭露动画完成*/
public static final int state_finished = 2;
private int state = state_not_started;
/**开始的X坐标,相对于手机屏幕,不包括状态栏*/
private int startLocationX;
/**开始的Y坐标,相对于手机屏幕,不包括状态栏*/
private int startLocationY;
/**当前揭露动画的半径*/
private int currentRadius;
private ObjectAnimator revealAnimator;
private Interpolator interpolator = null;
private OnStateChangeListener onStateChangeListener;
/**揭露动画的时间*/
private int fillTime;
public RevealView(Context context) {
super(context);
}
public RevealView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}

private void initView() {    fillPaint = new Paint();    fillPaint.setStyle(Paint.Style.FILL);    fillPaint.setColor(Color.WHITE);    interpolator = new AccelerateInterpolator();    fillTime = 400;}public void setFillColor(int color) {    fillPaint.setColor(color);}public void startFromLocation(int[] tapLocationOnScreen) {    changeState(state_fill_started);    startLocationX = tapLocationOnScreen[0];    startLocationY = tapLocationOnScreen[1];    /\*此时 利用反射机制 自动调用setCurrentRadius方法,并将参数传递过去\*/    revealAnimator = ObjectAnimator.ofInt(this, "currentRadius", 0, getWidth() + getHeight()).setDuration(fillTime);    revealAnimator.setInterpolator(interpolator);    revealAnimator.addListener(new MyAnimatorListenerAdapter());    revealAnimator.start();}private void changeState(int state) {    if (this.state == state) {        return;    }    this.state = state;    if (onStateChangeListener != null) {        onStateChangeListener.onStateChange(state);    }}public void setCurrentRadius(int radius) {    this.currentRadius = radius;    invalidate();}@Overrideprotected void onDraw(Canvas canvas) {    if (state == state_finished) {        canvas.drawRect(0, 0, getWidth(), getHeight(), fillPaint);    } else {        canvas.drawCircle(startLocationX, startLocationY, currentRadius, fillPaint);    }}private final class MyAnimatorListenerAdapter extends AnimatorListenerAdapter{    @Override    public void onAnimationEnd(Animator animation) {        changeState(state_finished);    }}public void setOnStateChangeListener(OnStateChangeListener onStateChangeListener) {    this.onStateChangeListener = onStateChangeListener;}public static interface OnStateChangeListener {    void onStateChange(int state);}

}

</pre>

<pre>

public class NewActivity extends AppCompatActivity {

private RevealView revealView;
private int[] location;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
location = getIntent().getExtras().getIntArray("location");
initView();
}
private void initView() {
findViewById(R.id.bt_5).setOnClickListener(new MyOnClickListener());
revealView = (RevealView) findViewById(R.id.rv);
revealView.setVisibility(View.VISIBLE);
revealView.setFillColor(Color.parseColor("#BBFF5722"));
/*当一个视图树将要绘制时,所要调用的回调函数的接口类*/
revealView.getViewTreeObserver().addOnPreDrawListener(new MyOnPreDrawListener());
revealView.setOnStateChangeListener(new MyOnStateChangeListener());
}

private final class MyOnClickListener implements View.OnClickListener {    @Override    public void onClick(View v) {        finish();    }}private final class MyOnStateChangeListener implements RevealView.OnStateChangeListener {    @Override    public void onStateChange(int state) {        if (state == RevealView.state_finished) {            revealView.setVisibility(View.GONE);        }    }}private final class MyOnPreDrawListener implements ViewTreeObserver.OnPreDrawListener {    @Override    public boolean onPreDraw() {        //必须remove掉 不然会重复调用        revealView.getViewTreeObserver().removeOnPreDrawListener(this);        revealView.startFromLocation(location);        return false;    }}

}

</pre>

转载地址:http://uiukx.baihongyu.com/

你可能感兴趣的文章
第三章 Python 的容器: 列表、元组、字典与集合
查看>>
struct timeval
查看>>
微信小程序开发 -- 点击右上角实现转发功能
查看>>
问题解决-Failed to resolve: com.android.support.constraint:constraint-layout:1.0.0-alpha7
查看>>
与MS Project相关的两个项目
查看>>
[转载]ASP.NET MVC Music Store教程(1):概述和新项目
查看>>
css实现单行,多行文本溢出显示省略号……
查看>>
使用 SharpSvn 执行 svn 操作的Demo
查看>>
js函数大全
查看>>
刷题笔记
查看>>
iOS app exception的解决方案
查看>>
Mongodb启动命令mongod参数说明
查看>>
TCP&UDP压力测试工具
查看>>
oracle 导入数据
查看>>
首个5G智慧机场落地广州 速度是4G的50倍
查看>>
Android 最简单的自定义Dialog之一
查看>>
磨刀不误砍柴 - 配置适合工作学习的桌面环境
查看>>
自己动手写docker-3
查看>>
Java笔记-反射机制(一)
查看>>
redux v3.7.2源码解读与学习之 applyMiddleware
查看>>