麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Android > 正文

Android自定義view實現(xiàn)圓的擴散效果

2019-10-21 21:26:08
字體:
供稿:網(wǎng)友

本文實例為大家分享了Android自定義View的實現(xiàn)水波紋,供大家參考,具體內(nèi)容如下

一、實現(xiàn)效果

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 ++) {      // 設(shè)置透明度      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;  }  /**   * 設(shè)置擴散圓顏色   */  public void setColor(int colorId){    mColor = colorId;  }  /**   * 設(shè)置中心圓顏色   */  public void setCoreColor(int colorId){    mCoreColor = colorId;  }  /**   * 設(shè)置中心圓半徑   */  public void setCoreRadius(int radius){    mCoreRadius = radius;  }  /**   * 設(shè)置擴散圓寬度(值越小寬度越大)   */  public void setDiffuseWidth(int width){    mDiffuseWidth = width;  }  /**   * 設(shè)置最大寬度   */  public void setMaxWidth(int maxWidth){    mMaxWidth = maxWidth;  }  /**   * 設(shè)置擴散速度,值越大速度越快   */  public void setDiffuseSpeed(int speed){    mDiffuseSpeed = speed;  }}

自己添加的attrs.xml(創(chuàng)建在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>

這樣就搞定了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 成av人在线观看 | 久久狠狠高潮亚洲精品 | 亚州综合一区 | 久久综合久久美利坚合众国 | 天天夜夜操操 | 91福利免费观看 | 国产精品午夜一区 | 看免费毛片| 亚洲精品午夜在线 | 人人做人人看 | 久久久久亚洲精品 | 久久久久久久久浪潮精品 | 国产色爱综合网 | 青青青在线免费 | 色网免费观看 | 久久精片| 国产成人综合在线 | 国产午夜三级一区二区三桃花影视 | 欧美日韩国产成人在线 | 国产激情视频在线 | 国产精品久久久久久久久久iiiii | 亚洲日本欧美 | 天堂在线中文资源 | 黄色影院在线看 | 欧美精品毛片 | 日韩视频一区二区三区在线观看 | 黄色av网站免费 | av不卡免费在线 | 国产亚洲综合精品 | 一级做人爱c黑人影片 | 午夜视频啊啊啊 | 亚州综合网 | 九九热精品在线视频 | 欧美日韩国产成人在线 | 美女污污视频在线观看 | 久久欧美亚洲另类专区91大神 | 亚洲精品成人在线视频 | 国产精品av久久久久久网址 | 免费一级在线视频 | 神马视频我不卡 | xxxxxx免费 |