무엇인가 로딩을 할때 띄워줄 로딩 프로그레스이다.
Animation을 사용해 Image를 Rotate 한다.
class ContentsLoadingProgress {
companion object {
const val LOADING_SCREEN_TAG = "contents_loading_screen"
val mContentViewHashMap: HashMap<String, ViewGroup> = HashMap()
@JvmStatic
@JvmOverloads
/**
* @param touchable = 콘텐츠 로딩화면 뒤에 터치되지 않게 설정
*/
fun showProgress(key: String, activity: Activity, touchable: Boolean = false) {
if(mContentViewHashMap.containsKey(key)) return//이미 노출 중이라면.
val contentView = activity.findViewById<ViewGroup>(android.R.id.content)
mContentViewHashMap.put(key, contentView)
val inflater = LayoutInflater.from(activity)
inflater.inflate(R.layout.include_contents_loading_screen, null)?.let {
it.isClickable = touchable
contentView.addView(it)
it.layoutParams?.apply {
width = ViewGroup.LayoutParams.MATCH_PARENT
height = ViewGroup.LayoutParams.MATCH_PARENT
}
val loadingImageView = it.findViewById<ImageView>(R.id.vodlist_loading_pregross)
val rotateAnim = AnimationUtils.loadAnimation(activity, R.anim.loading_image_rotate_anim)
rotateAnim.fillAfter = true
loadingImageView.animation = rotateAnim
it.tag = LOADING_SCREEN_TAG
}
}
@JvmStatic
fun hideProgress(key: String) {
if(mContentViewHashMap.containsKey(key)) {
mContentViewHashMap.get(key)?.apply {
val loadingScreen: View? = findViewById<View>(R.id.loading_screen)
if(loadingScreen != null && loadingScreen.visibility == View.VISIBLE)
loadingScreen.visibility = View.GONE
removeView(findViewWithTag(LOADING_SCREEN_TAG))
}
mContentViewHashMap.remove(key)
}
}
}
}
include로 사용될 layout
include_contents_loading_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/loading_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#80000000">
<ImageView
android:id="@+id/vodlist_loading_pregross"
android:layout_width="@dimen/common_90dp"
android:layout_height="@dimen/common_90dp"
android:layout_gravity="center"
android:src="@drawable/loading"/>
</FrameLayout>
사용되는 anim
linear_interpolator.xml
<?xml version="1.0" encoding="utf-8"?>
<linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android" />
loading_image_rotate_anim.xml
drawable 이미지는 구현의도에 맞게 수정
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="6000"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:interpolator="@anim/linear_interpolator">
</rotate>
사용
ContentsLoadingProgress.showProgress(this.getLocalClassName(), this, true) //Progress Show
ContentsLoadingProgress.hideProgress(this.localClassName) //Progress Hide
//ContentsLoadingProgress.showProgress(mActivity.getLocalClassName(), mActivity, true) //Progress Show
//ContentsLoadingProgress.hideProgress(mActivity.localClassName) //Progress Hide
'Android > Util' 카테고리의 다른 글
TextView \n 문자 없이 중앙정렬시 좌우 여백 지우기 (0) | 2021.04.13 |
---|