a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android實現水波紋特效

2019-10-21 21:26:02
字體:
來源:轉載
供稿:網友

最近需要做個類似于水波紋動畫的效果,思考了一下不需要UI切個動態圖,Android原生的技術利用動畫或者自定義控件都可以實現,下面上個圖類似于這樣的效果

Android,水波紋,特效

下面請看第一種動畫實現,這種方式較為簡單些,就是利用3個ImageView不斷地做縮放和漸變的動畫。

布局文件定義一下

<RelativeLayout  android:id="@+id/rl"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:gravity="center"  android:layout_marginBottom="160dp">  <!--中心imageView-->  <ImageView    android:id="@+id/iv_wave"    android:layout_width="150dp"    android:layout_height="150dp"    android:layout_centerHorizontal="true"    android:background="@drawable/shape_circle" />  <!--中間的imageView-->  <ImageView    android:id="@+id/iv_wave_1"    android:layout_width="150dp"    android:layout_height="150dp"    android:layout_centerHorizontal="true"    android:background="@drawable/shape_circle" />  <!--最外層imageView-->  <ImageView    android:id="@+id/iv_wave_2"    android:layout_width="150dp"    android:layout_height="150dp"    android:layout_centerHorizontal="true"    android:background="@drawable/shape_circle" /></RelativeLayout>

接下來中間的ImageView保持不變,通過操作另外兩個ImageView達到效果

private void setAnim1() {  AnimationSet as = new AnimationSet(true);  //縮放動畫,以中心從原始放大到1.4倍  ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 1.4f, 1.0f, 1.4f,      ScaleAnimation.RELATIVE_TO_SELF, 0.5f,      ScaleAnimation.RELATIVE_TO_SELF, 0.5f);  //漸變動畫  AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);  scaleAnimation.setDuration(800);  scaleAnimation.setRepeatCount(Animation.INFINITE);  alphaAnimation.setRepeatCount(Animation.INFINITE);  as.setDuration(800);  as.addAnimation(scaleAnimation);  as.addAnimation(alphaAnimation);  iv1.startAnimation(as);}private void setAnim2() {  AnimationSet as = new AnimationSet(true);  //縮放動畫,以中心從1.4倍放大到1.8倍  ScaleAnimation scaleAnimation = new ScaleAnimation(1.4f, 1.8f, 1.4f, 1.8f,      ScaleAnimation.RELATIVE_TO_SELF, 0.5f,      ScaleAnimation.RELATIVE_TO_SELF, 0.5f);  //漸變動畫  AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 0.1f);  scaleAnimation.setDuration(800);  scaleAnimation.setRepeatCount(Animation.INFINITE);  alphaAnimation.setRepeatCount(Animation.INFINITE);  as.setDuration(800);  as.addAnimation(scaleAnimation);  as.addAnimation(alphaAnimation);  iv2.startAnimation(as);}

接下來就是第二種自定義動畫實現

首先定義style文件自定義屬性--在values下創建attrs.xml文件

<declare-styleable name="SpreadView">  <!--中心圓顏色-->  <attr name="spread_center_color" format="color" />  <!--中心圓半徑-->  <attr name="spread_radius" format="integer" />  <!--擴散圓顏色-->  <attr name="spread_spread_color" format="color" />  <!--擴散間距-->  <attr name="spread_distance" format="integer" />  <!--擴散最大半徑-->  <attr name="spread_max_radius" format="integer" />  <!--擴散延遲間隔-->  <attr name="spread_delay_milliseconds" format="integer" /></declare-styleable>

接下來創建SpreadView繼承view,初始化構造方法

public SpreadView(Context context) {  this(context,null,0);} public SpreadView(Context context, @Nullable AttributeSet attrs) {  this(context, attrs,0);} public SpreadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  super(context, attrs, defStyleAttr);  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SpreadView, defStyleAttr, 0);  radius = a.getInt(R.styleable.SpreadView_spread_radius, radius);  maxRadius = a.getInt(R.styleable.SpreadView_spread_max_radius, maxRadius);  int centerColor = a.getColor(R.styleable.SpreadView_spread_center_color, ContextCompat.getColor(context, R.color.colorAccent));  int spreadColor = a.getColor(R.styleable.SpreadView_spread_spread_color, ContextCompat.getColor(context, R.color.colorAccent));  distance = a.getInt(R.styleable.SpreadView_spread_distance, distance);  a.recycle();  centerPaint = new Paint();  centerPaint.setColor(centerColor);  centerPaint.setAntiAlias(true);  //最開始不透明且擴散距離為0  alphas.add(255);  spreadRadius.add(0);  spreadPaint = new Paint();  spreadPaint.setAntiAlias(true);  spreadPaint.setAlpha(255);  spreadPaint.setColor(spreadColor);}

自定義View的繪制:

@Overrideprotected void onDraw(Canvas canvas) {  super.onDraw(canvas);  for (int i = 0; i < spreadRadius.size(); i++) {    int alpha = alphas.get(i);    spreadPaint.setAlpha(alpha);    int width = spreadRadius.get(i);    //繪制擴散的圓    canvas.drawCircle(centerX, centerY, radius + width, spreadPaint);    //每次擴散圓半徑遞增,圓透明度遞減    if (alpha > 0 && width < 300) {      alpha = alpha - distance > 0 ? alpha - distance : 1;      alphas.set(i, alpha);      spreadRadius.set(i, width + distance);    }  }  //當最外層擴散圓半徑達到最大半徑時添加新擴散圓  if (spreadRadius.get(spreadRadius.size() - 1) > maxRadius) {    spreadRadius.add(0);    alphas.add(255);  }  //超過8個擴散圓,刪除最先繪制的圓,即最外層的圓  if (spreadRadius.size() >= 8) {    alphas.remove(0);    spreadRadius.remove(0);  }  //中間的圓  canvas.drawCircle(centerX, centerY, radius, centerPaint);  //延遲更新,達到擴散視覺差效果  postInvalidateDelayed(delayMilliseconds);}

最后在activity的布局文件中引用自定義屬性:

<com.example.louliang.spread.SpreadView  android:layout_width="match_parent"  android:layout_height="wrap_content"  app:spread_center_color="@color/colorAccent"  app:spread_delay_milliseconds="35"  app:spread_distance="5"  app:spread_max_radius="90"  app:spread_radius="150"  app:spread_spread_color="@color/colorAccent" />

以上兩種方法就實現了水波紋的效果,下載完整的demo請點擊鏈接,希望對大家有所幫助。

源碼下載:Android實現水波紋特效

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 欧美亚洲一级 | 欧洲尺码日本国产精品 | 日韩激情| 国产伦精品一区二区 | 一级毛片网 | 成人av电影免费看 | 久久精品久久精品国产大片 | 精品久久久一区 | 国产福利在线播放麻豆 | 久久国产精品99久久久久久老狼 | 日本阿v视频高清在线中文 日本在线观看 | 欧美成年网站 | 国产精品第一区第27页 | 精品一二区 | 成人免费毛片嘿嘿连载视频 | 一级黄网 | 国产精品久久久久久久久久三级 | 亚洲午夜视频在线观看 | 国产精品日韩欧美一区二区三区 | 日韩性网站 | 精品久久久一区 | 国产99久| 亚洲美女av在线 | 一区二区三区观看视频 | 精品一区二区久久久久久久网站 | 天天干com | 亚洲精品国产成人 | 亚洲精品乱码久久久久v最新版 | 中文字幕日韩在线视频 | 婷婷色综合 | 精品一区二区三区在线观看视频 | 一本色道亚洲精品aⅴ | 一本色道久久综合亚洲精品按摩 | 日本免费一区二区三区 | 日韩欧美在线观看 | 国产成人免费在线观看视频 | 97视频在线免费观看 | 色播久久| 亚洲婷婷一区二区三区 | 成人做爰999 | 国产高清中文字幕 |