在从活动A到活动B的转换中,我有一个共享元素。但是,A中的共享元素使用TransitionDrawable,有时我希望使用startTransition
作为转换的一部分来触发可绘制的转换。(B中的共享元素类似于转换的结束状态。)
共享元素转换文档声明,实现将B中的共享元素移动到A中共享元素的屏幕位置,然后运行共享元素进入转换。
是否可以将A中的元素移动到B元素的位置(允许我通过共享元素回调运行startTransition )而不是其他方式?P.S.:我知道共享元素退出转换,但我不认为这对我有帮助,因为这总是在活动转换开始之前运行,而且我看不到在文档中改变这种情况的方法。
发布于 2017-06-11 00:40:11
我通过SharedElementCallback利用共享元素快照。虽然总体上这是相当复杂的,但下面是我的代码中处理获取快照和使用快照作为底层重新创建TransitionDrawable的部分。
override fun onSharedElementEnd(sharedElementNames: MutableList<String>,
sharedElements: MutableList<View>,
sharedElementSnapshots: MutableList<View>?) {
if (sharedElements.size < 1)
return
val shElem = sharedElements[0]
if (shElem !is ImageView)
return
mainPhoto = shElem
if (mainPhoto!!.drawable !is TransitionDrawable) {
val layers = arrayOf(sharedElementSnapshots!![0].background, mainPhoto!!.drawable)
if (exiting)
layers.reverse()
val transDrwbl = android.graphics.drawable.TransitionDrawable(layers)
mainPhoto!!.setImageDrawable(transDrwbl)
} else
needToUseReverse = true
}
/**
* I'm copying these from v21 SharedElementCallback.java because, while the
* new functionality in v24 (v23?) seems nice, I don't want something unexpected
* to happen on newer versions
*/
override fun onCaptureSharedElementSnapshot(sharedElement: View, viewToGlobalMatrix: Matrix,
screenBounds: RectF): Parcelable {
val mTempMatrix = Matrix(viewToGlobalMatrix)
val transitionUtils = Class.forName("android.transition.TransitionUtils")
val createViewBitmap = transitionUtils.getDeclaredMethod("createViewBitmap", View::class.java, Matrix::class.java, RectF::class.java)
val pcbl = createViewBitmap.invoke(null, sharedElement, mTempMatrix, screenBounds) as Parcelable
return pcbl
}
override fun onMapSharedElements(names: MutableList<String>, sharedElements: MutableMap<String, View>) {
for (key in sharedElements.keys.toList())
if (key != currentSharedElementName)
sharedElements.remove(key)
}
override fun onCreateSnapshotView(context: Context, snapshot: Parcelable): View? {
var view: View? = null
if (snapshot is Bitmap) {
view = View(context)
val resources = context.resources
view.background = BitmapDrawable(resources, snapshot)
}
return view
}
https://stackoverflow.com/questions/44447478
复制相似问题