* 물이 하나도 채워지지 않은 (progress 가 0) 이미지 ic_water_empty.png 와 물이 모두 채워진 (progress 가 100) 이미지 ic_water_full.png 가 필요
MainActivity.java
public class MainActivity extends AppCompatActivity {
// progress 만큼 물이 채워지는 애니메이션
private ValueAnimator mAnimation = new ValueAnimator();
// 프로그레스를 보여줄 뷰
FrameLayout progressBar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
Button btnStart = findViewById(R.id.btnStart);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// ex) 80% 까지 물이 채워짐
updateProgress(80);
}
});
// 애니메이션
mAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(@NonNull ValueAnimator valueAnimator) {
// 애니메이션 프레임마다 호출 - 배경 drawable의 level 만큼 위로 차오른다
progressBar.getBackground().setLevel((Integer) valueAnimator.getAnimatedValue());
}
});
mAnimation.setDuration(2000);
}
// 프로그레스 업데이트 : progress - 0~100
private void updateProgress(int progress) {
progress = Math.max(0, progress);
progress = Math.min(progress, 100);
// ClipDrawable은 0~10000 사이의 level 값
int level = (int) Math.round((double) progress / 100 * 10000);
// 0~level 값만큼 애니메이션 실행
mAnimation.setIntValues(0, level);
mAnimation.start();
}
}
|
cs |
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/progressBar"
android:background="@drawable/progress_water"
android:layout_centerInParent="true"
android:layout_width="128dp"
android:layout_height="128dp" />
<Button
android:id="@+id/btnStart"
android:text="Start"
android:layout_marginTop="24dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
|
cs |
progress_water.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_water_empty" />
<item android:drawable="@drawable/progress_water_clip" />
</layer-list>
|
cs |
progress_water_clip.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/ic_water_full"
android:clipOrientation="vertical"
android:gravity="bottom">
</clip>
|
cs |
결과
'IT > Ⅱ. Android' 카테고리의 다른 글
[Android] FCM 발송 (1) | 2025.06.29 |
---|---|
[Android] GIF, SVG, APNG 이미지 사용 (2) | 2025.05.21 |
[Android] PDF 문서 보기 - PdfRenderer API 사용 (1) | 2025.05.14 |
[Android] PDF 문서 보기 - WebView 사용 (0) | 2025.05.13 |
[Android] PDF 문서 보기 - 라이브러리 사용 (0) | 2025.05.10 |