一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android 實現圓圈擴散水波動畫效果兩種方法

Android 實現圓圈擴散水波動畫效果兩種方法

2022-02-25 15:16程序猿tx Android

這篇文章主要介紹了Android 實現圓圈擴散水波動畫效果兩種方法,需要的朋友可以參考下

兩種方式實現類似水波擴散效果,先上圖為敬

  1. 自定義view實現
  2. 動畫實現

Android 實現圓圈擴散水波動畫效果兩種方法

自定義view實現

思路分析:通過canvas畫圓,每次改變圓半徑和透明度,當半徑達到一定程度,再次從中心開始繪圓,達到不同層級的效果,通過不斷繪制達到view擴散效果

?
1
2
3
4
5
6
7
8
9
10
private Paint centerPaint; //中心圓paint
private int radius = 100; //中心圓半徑
private Paint spreadPaint; //擴散圓paint
private float centerX;//圓心x
private float centerY;//圓心y
private int distance = 5; //每次圓遞增間距
private int maxRadius = 80; //最大圓半徑
private int delayMilliseconds = 33;//擴散延遲間隔,越大擴散越慢
private List<Integer> spreadRadius = new ArrayList<>();//擴散圓層級數,元素為擴散的距離
private List<Integer> alphas = new ArrayList<>();//對應每層圓的透明度

style文件里自定義屬性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<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>

初始化

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
}

確定圓心位置

?
1
2
3
4
5
6
7
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  super.onSizeChanged(w, h, oldw, oldh);
  //圓心位置
  centerX = w / 2;
  centerY = h / 2;
}

自定義view的繪制

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Override
protected 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);
  //TODO 可以在中間圓繪制文字或者圖片
  //延遲更新,達到擴散視覺差效果
  postInvalidateDelayed(delayMilliseconds);
}

xml樣式

?
1
2
3
4
5
6
7
8
9
10
<com.airsaid.diffuseview.widget.SpreadView
  android:id="@+id/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="100"
  app:spread_spread_color="@color/colorAccent" />

效果圖

Android 實現圓圈擴散水波動畫效果兩種方法

中心圓處可以自定義寫文字,畫圖片等等...

動畫實現

思路分析:通過動畫實現,imageView不停做動畫縮放+漸變

最中心的imageView保持不變

中間一層imageView從原始放大到1.4倍,同時從不透明變為半透明

最外層的imageView從1.4倍放大到1.8倍,同時從半透明變為全透明

利用shape畫一個圓,作為動畫基礎視圖

?
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <corners android:radius="65dp"/>
  <solid android:color="@color/colorAccent"/>
</shape>

布局視圖

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <!--中心imageView-->
  <ImageView
    android:id="@+id/iv_wave"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:layout_gravity="center"
    android:background="@drawable/shape_circle" />
  <!--中間的imageView-->
  <ImageView
    android:id="@+id/iv_wave_1"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:layout_gravity="center"
    android:background="@drawable/shape_circle" />
  <!--最外層imageView-->
  <ImageView
    android:id="@+id/iv_wave_2"
    android:layout_width="130dp"
    android:layout_height="130dp"
    android:layout_gravity="center"
    android:background="@drawable/shape_circle" />
</FrameLayout>

中間imageView的動畫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
}

最外層imageView的動畫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
}

效果圖

Android 實現圓圈擴散水波動畫效果兩種方法

相比較而言,自定義view的效果更好點,動畫實現起來更方便點。

兩種方式實現的擴散效果介紹完畢,具體項目里還是要按需變動的。

總結

以上所述是小編給大家介紹的Android 實現圓圈擴散水波動畫效果兩種方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://www.cnblogs.com/taixiang/archive/2018/05/27/9095464.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品毛片va一区二区三区 | 国产精品久久久久一区二区三区 | 四虎在线成人免费网站 | 国产一线天 | 深夜在线网址 | 亚洲精品老司机福利在线播放 | cosplay 极品videos | 亚洲欧美成人综合 | 精品一区二区高清在线观看 | 亚洲小视频网站 | 女同videos双性人 | 九草视频在线 | 韩国甜性涩爱免费观看 | 国产三级自拍视频 | 亚洲视频在线看 | 我与肥熟老妇的性事 | 国产在线步兵一区二区三区 | 天天爽视频 | 9久热这里只有精品视频在线观看 | 四虎影院的网址 | 特级淫片大乳女子高清视频 | 18hdxxxx中国 | av毛片在线看| 国产 日韩 欧美 综合 | ferr孕妇videos毛茸茸 | 免费抽搐一进一出印度 | 国产实拍会所女技师在线 | 呜嗯啊野战h呻吟男男双性 污小说在线阅读 | 四虎国产精品免费久久麻豆 | 9久热这里只有精品免费 | 国产精品免费 | 精品午夜寂寞影院在线观看 | 四虎精品永久在线网址 | a黄毛片| 九九99靖品 | 午夜影院0606免费 | 无码人妻视频又大又粗欧美 | 成人国产精品 | 日韩免费高清完整版 | 黑人双渗透 | 114毛片免费观看网站 |