Last active
April 5, 2017 14:00
-
-
Save icris/93d8feaf7774e71a9749ee92869d089a to your computer and use it in GitHub Desktop.
A dialogFragment with a loading gif.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import android.app.Dialog | |
| import android.content.DialogInterface | |
| import android.os.Bundle | |
| import android.support.v4.app.DialogFragment | |
| import android.view.* | |
| import com.facebook.common.util.UriUtil | |
| import com.facebook.drawee.backends.pipeline.Fresco | |
| import com.facebook.drawee.drawable.ScalingUtils | |
| import com.facebook.drawee.view.SimpleDraweeView | |
| import org.jetbrains.anko.* | |
| import org.jetbrains.anko.custom.ankoView | |
| typealias OnCancelListener = () -> Unit | |
| /** | |
| * loading dialog | |
| * Created by icris on 2017/3/27. | |
| */ | |
| class LoadingDialog(val cancelable: Boolean = false, val onCancelListener: OnCancelListener = {}) : DialogFragment() { | |
| override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | |
| isCancelable = cancelable | |
| return super.onCreateDialog(savedInstanceState).apply { | |
| window?.setBackgroundDrawableResource(android.R.color.transparent) | |
| } | |
| } | |
| override fun onCancel(dialog: DialogInterface?) { | |
| super.onCancel(dialog) | |
| onCancelListener() | |
| } | |
| override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? { | |
| return MUI().createView(AnkoContext.create(activity, this)) | |
| } | |
| private class MUI : AnkoComponent<LoadingDialog> { | |
| override fun createView(ui: AnkoContext<LoadingDialog>): View = with(ui) { | |
| frameLayout { | |
| simpleDraweeView { | |
| lparams(width = dip(80), height = dip(80)) | |
| imageRes = R.raw.ripple | |
| hierarchy.actualImageScaleType = ScalingUtils.ScaleType.FIT_CENTER | |
| } | |
| } | |
| } | |
| } | |
| } | |
| inline fun ViewManager.simpleDraweeView(theme: Int = 0, init: SimpleDraweeView.() -> Unit) | |
| = ankoView(::SimpleDraweeView, theme) { init() } | |
| var SimpleDraweeView.imageRes get() = 1 | |
| set(value) { | |
| val controller = Fresco.newDraweeControllerBuilder() | |
| .setUri(UriUtil.getUriForResourceId(value)) | |
| .setAutoPlayAnimations(true) | |
| .setOldController(controller) | |
| .build() | |
| setController(controller) | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment