Android自定义view实现圆的扩散效果

所属分类: 软件编程 / Android 阅读数: 88
收藏 0 赞 0 分享

本文实例为大家分享了Android自定义View的实现水波纹,供大家参考,具体内容如下

一、实现效果

MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  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"
  android:orientation="vertical"
  tools:context=".MainActivity">

  <jt.com.animatorcirecle.myview.DiffuseView
    android:id="@+id/diffuseView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    app:diffuse_color="@color/colorAccent"
    app:diffuse_coreColor="@color/colorPrimaryDark"
    app:diffuse_coreImage="@android:drawable/ic_menu_search"
    app:diffuse_coreRadius="100"
    app:diffuse_maxWidth="300"
    app:diffuse_speed="5"
    app:diffuse_width="4"/>

  <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="开始扩散"/>

  <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:text="停止扩散"/>
</LinearLayout>

MainActivity中的点击事件

public class MainActivity extends AppCompatActivity {
  private Button button;
  private Button button2;
  private DiffuseView diffuseView;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button =  findViewById(R.id.button);
    button2 =  findViewById(R.id.button2);
    diffuseView = findViewById(R.id.diffuseView);

    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        diffuseView.start();
      }
    });

    button2.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        diffuseView.stop();
      }
    });
  }
}

自定义view类

public class DiffuseView extends View {

  /** 扩散圆圈颜色 */
  private int mColor = getResources().getColor(R.color.colorAccent);
  /** 圆圈中心颜色 */
  private int mCoreColor = getResources().getColor(R.color.colorPrimary);
  /** 中心圆半径 */
  private float mCoreRadius = 150;
  /** 扩散圆宽度 */
  private int mDiffuseWidth = 3;
  /** 最大宽度 */
  private Integer mMaxWidth = 255;
  /** 扩散速度 */
  private int mDiffuseSpeed = 5;
  /** 是否正在扩散中 */
  private boolean mIsDiffuse = false;
  // 透明度集合
  private List<Integer> mAlphas = new ArrayList<>();
  // 扩散圆半径集合
  private List<Integer> mWidths = new ArrayList<>();
  private Paint mPaint;

  public DiffuseView(Context context) {
    this(context, null);
  }

  public DiffuseView(Context context, AttributeSet attrs) {
    this(context, attrs, -1);
  }

  public DiffuseView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DiffuseView, defStyleAttr, 0);
    mColor = a.getColor(R.styleable.DiffuseView_diffuse_color, mColor);
    mCoreColor = a.getColor(R.styleable.DiffuseView_diffuse_coreColor, mCoreColor);
    mCoreRadius = a.getFloat(R.styleable.DiffuseView_diffuse_coreRadius, mCoreRadius);
    mDiffuseWidth = a.getInt(R.styleable.DiffuseView_diffuse_width, mDiffuseWidth);
    mMaxWidth = a.getInt(R.styleable.DiffuseView_diffuse_maxWidth, mMaxWidth);
    mDiffuseSpeed = a.getInt(R.styleable.DiffuseView_diffuse_speed, mDiffuseSpeed);
    a.recycle();
  }

  private void init() {
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mAlphas.add(255);
    mWidths.add(0);
  }

  @Override
  public void invalidate() {
    if(hasWindowFocus()){
      super.invalidate();
    }
  }

  @Override
  public void onWindowFocusChanged(boolean hasWindowFocus) {
    super.onWindowFocusChanged(hasWindowFocus);
    if(hasWindowFocus){
      invalidate();
    }
  }

  @Override
  public void onDraw(Canvas canvas) {
    // 绘制扩散圆
    mPaint.setColor(mColor);
    for (int i = 0; i < mAlphas.size(); i ++) {
      // 设置透明度
      Integer alpha = mAlphas.get(i);
      mPaint.setAlpha(alpha);
      // 绘制扩散圆
      Integer width = mWidths.get(i);
      canvas.drawCircle(getWidth() / 2, getHeight() / 2, mCoreRadius + width, mPaint);

      if(alpha > 0 && width < mMaxWidth){
        mAlphas.set(i, alpha - mDiffuseSpeed > 0 ? alpha - mDiffuseSpeed : 1);
        mWidths.set(i, width + mDiffuseSpeed);
      }
    }
    // 判断当扩散圆扩散到指定宽度时添加新扩散圆
    if (mWidths.get(mWidths.size() - 1) >= mMaxWidth / mDiffuseWidth) {
      mAlphas.add(255);
      mWidths.add(0);
    }
    // 超过10个扩散圆,删除最外层
    if(mWidths.size() >= 10){
      mWidths.remove(0);
      mAlphas.remove(0);
    }

    // 绘制中心圆
    mPaint.setAlpha(255);
    mPaint.setColor(mCoreColor);
    canvas.drawCircle(getWidth() / 2, getHeight() / 2, mCoreRadius, mPaint);

    if(mIsDiffuse){
      invalidate();
    }
  }

  /**
   * 开始扩散
   */
  public void start() {
    mIsDiffuse = true;
    invalidate();
  }

  /**
   * 停止扩散
   */
  public void stop() {
    mIsDiffuse = false;
    mWidths.clear();
    mAlphas.clear();
    mAlphas.add(255);
    mWidths.add(0);
    invalidate();
  }

  /**
   * 是否扩散中
   */
  public boolean isDiffuse(){
    return mIsDiffuse;
  }

  /**
   * 设置扩散圆颜色
   */
  public void setColor(int colorId){
    mColor = colorId;
  }

  /**
   * 设置中心圆颜色
   */
  public void setCoreColor(int colorId){
    mCoreColor = colorId;
  }

  /**
   * 设置中心圆半径
   */
  public void setCoreRadius(int radius){
    mCoreRadius = radius;
  }

  /**
   * 设置扩散圆宽度(值越小宽度越大)
   */
  public void setDiffuseWidth(int width){
    mDiffuseWidth = width;
  }

  /**
   * 设置最大宽度
   */
  public void setMaxWidth(int maxWidth){
    mMaxWidth = maxWidth;
  }

  /**
   * 设置扩散速度,值越大速度越快
   */
  public void setDiffuseSpeed(int speed){
    mDiffuseSpeed = speed;
  }
}

自己添加的attrs.xml(创建在Values包底下,切勿倒错)

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <!--扩散圆颜色-->
  <attr name="diffuse_color" format="color"/>
  <!--中心圆颜色-->
  <attr name="diffuse_coreColor" format="color"/>
  <!--中心圆图片-->
  <attr name="diffuse_coreImage" format="reference"/>
  <!--中心圆半径-->
  <attr name="diffuse_coreRadius" format="float"/>
  <!--扩散圆宽度,值越小越宽-->
  <attr name="diffuse_width" format="integer"/>
  <!--最大扩散宽度-->
  <attr name="diffuse_maxWidth" format="integer"/>
  <!--扩散速度,值越大越快-->
  <attr name="diffuse_speed" format="integer"/>

  <declare-styleable name="DiffuseView">
    <attr name="diffuse_color"/>
    <attr name="diffuse_coreColor"/>
    <attr name="diffuse_coreImage"/>
    <attr name="diffuse_coreRadius"/>
    <attr name="diffuse_width"/>
    <attr name="diffuse_maxWidth"/>
    <attr name="diffuse_speed"/>
  </declare-styleable>

</resources>

这样就搞定了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多