Skip to content

Commit ef0b134

Browse files
author
fengpeng
committed
preview video
1 parent ab8748e commit ef0b134

6 files changed

Lines changed: 95 additions & 12 deletions

File tree

picker/src/main/java/pizzk/media/picker/adapter/PreviewPhotoAdapter.kt

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,71 @@
11
package pizzk.media.picker.adapter
22

33
import android.content.Context
4+
import android.provider.MediaStore
5+
import android.view.LayoutInflater
46
import android.view.View
57
import android.view.ViewGroup
8+
import android.widget.VideoView
69
import androidx.viewpager.widget.PagerAdapter
710
import com.github.chrisbanes.photoview.PhotoView
11+
import pizzk.media.picker.R
812
import pizzk.media.picker.arch.PickControl
913
import pizzk.media.picker.source.IMedia
1014
import pizzk.media.picker.source.IMediaSource
1115
import pizzk.media.picker.utils.PickUtils
1216

1317
class PreviewPhotoAdapter(private val context: Context, private val source: IMediaSource) :
1418
PagerAdapter() {
15-
private val views: MutableList<PhotoView> = ArrayList(5)
19+
private val views: MutableList<View> = ArrayList(5)
1620
private val lp: ViewGroup.LayoutParams = ViewGroup.LayoutParams(
1721
ViewGroup.LayoutParams.MATCH_PARENT,
1822
ViewGroup.LayoutParams.MATCH_PARENT
1923
)
20-
private var currentItem: PhotoView? = null
24+
private var vCurrent: View? = null
2125
private var clickBlock: (View) -> Unit = { _ -> }
2226
private var scaleBlock: () -> Unit = { }
27+
private var vPlaying: View? = null
2328

2429
override fun isViewFromObject(view: View, obj: Any): Boolean = view === obj
2530

2631
override fun getCount(): Int = source.count()
2732

2833
override fun instantiateItem(container: ViewGroup, position: Int): Any {
29-
val view: PhotoView = if (views.isEmpty()) {
30-
PhotoView(context)
34+
val view: View = if (views.isEmpty()) {
35+
LayoutInflater.from(context).inflate(R.layout.preview_photo_page_item, null)
3136
} else {
3237
views.removeAt(0)
3338
}
34-
view.setOnScaleChangeListener { _, _, _ -> scaleBlock() }
3539
container.addView(view, lp)
3640
view.setOnClickListener(clickBlock)
37-
val media: IMedia = source[position] ?: return view
38-
PickControl.imageLoad().load(view, media.uri(), media.mimeType())
41+
//
42+
val vPhoto = view.findViewById<PhotoView>(R.id.vPhoto)
43+
vPhoto.setOnScaleChangeListener { _, _, _ -> scaleBlock() }
44+
val vVideo = view.findViewById<VideoView>(R.id.video)
45+
val vPause = view.findViewById<View>(R.id.vPause)
46+
//
47+
val media: IMedia? = source[position]
48+
val isVideo = media?.mediaType() == MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO
49+
vVideo.visibility = View.GONE
50+
vPause.visibility = if (isVideo) View.VISIBLE else View.GONE
51+
//
52+
media ?: return view
53+
PickControl.imageLoad().load(vPhoto, media.uri(), media.mimeType())
54+
vVideo.setVideoURI(media.uri())
55+
vVideo.setOnCompletionListener {
56+
vVideo.visibility = View.GONE
57+
}
58+
vVideo.setOnErrorListener { _, _, _ ->
59+
vVideo.visibility = View.GONE
60+
return@setOnErrorListener true
61+
}
62+
vPause.setOnClickListener {
63+
vVideo.stopPlayback()
64+
vVideo.start()
65+
vVideo.resume()
66+
vPlaying = view
67+
vVideo.visibility = View.VISIBLE
68+
}
3969
return view
4070
}
4171

@@ -48,17 +78,29 @@ class PreviewPhotoAdapter(private val context: Context, private val source: IMed
4878
}
4979

5080
override fun destroyItem(container: ViewGroup, position: Int, obj: Any) {
51-
val view: PhotoView = obj as PhotoView
81+
val view: View = obj as View
82+
if (views.size < 5) views.add(view)
5283
container.removeView(view)
53-
views.add(view)
5484
}
5585

5686
override fun setPrimaryItem(container: ViewGroup, position: Int, obj: Any) {
5787
super.setPrimaryItem(container, position, obj)
58-
currentItem = obj as? PhotoView
88+
vCurrent = obj as? View
5989
}
6090

61-
fun getPrimaryItem(): PhotoView? = currentItem
91+
fun resetScale() {
92+
val view = vCurrent ?: return
93+
val vPhoto = view.findViewById<PhotoView>(R.id.vPhoto)
94+
if (vPhoto.scale != 1.0f) vPhoto.scale = 1.0f
95+
}
96+
97+
fun stopPlay() {
98+
val view = vCurrent ?: vPlaying ?: return
99+
val vVideo = view.findViewById<VideoView>(R.id.video)
100+
vVideo.stopPlayback()
101+
vVideo.visibility = View.GONE
102+
vPlaying = null
103+
}
62104

63105
fun getPath(position: Int): String = source[position]?.uri()?.toString() ?: ""
64106

picker/src/main/java/pizzk/media/picker/view/PreviewActivity.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ class PreviewActivity : AppCompatActivity() {
198198
val select: Boolean =
199199
selectAdapter.onPreviewChanged(photoAdapter.getPath(index), selectedView)
200200
switchSelectBox(select, index)
201-
photoAdapter.getPrimaryItem()?.let { if (it.scale != 1.0f) it.scale = 1.0f }
201+
photoAdapter.resetScale()
202+
photoAdapter.stopPlay()
202203
if (!adjust) return
203204
val smooth = false
204205
photosView.setCurrentItem(index, smooth)
@@ -266,4 +267,9 @@ class PreviewActivity : AppCompatActivity() {
266267
doneButton.enable(true)
267268
}
268269
}
270+
271+
override fun onPause() {
272+
super.onPause()
273+
photoAdapter.stopPlay()
274+
}
269275
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<com.github.chrisbanes.photoview.PhotoView
9+
android:id="@+id/vPhoto"
10+
android:layout_width="match_parent"
11+
android:layout_height="match_parent" />
12+
13+
<ImageView
14+
android:id="@+id/vPause"
15+
android:layout_width="60dp"
16+
android:layout_height="60dp"
17+
android:src="@mipmap/icon_video_pause"
18+
android:visibility="gone"
19+
app:layout_constraintBottom_toBottomOf="parent"
20+
app:layout_constraintEnd_toEndOf="parent"
21+
app:layout_constraintStart_toStartOf="parent"
22+
app:layout_constraintTop_toTopOf="parent"
23+
tools:ignore="ContentDescription"
24+
tools:visibility="visible" />
25+
26+
<VideoView
27+
android:id="@+id/video"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:visibility="gone"
31+
app:layout_constraintBottom_toBottomOf="parent"
32+
app:layout_constraintTop_toTopOf="parent" />
33+
</androidx.constraintlayout.widget.ConstraintLayout>
612 Bytes
Loading

picker/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<color name="album_item_uncheck">#ffffff</color>
1111
<color name="album_section_mask">#eb212123</color>
1212
<color name="album_section_background">#ffffff</color>
13+
<color name="album_preview_pause_bkg">#52000000</color>
1314
<!--预览-->
1415
<color name="pick_preview_overlay">#e5000000</color>
1516
<color name="pick_review_background">#000000</color>

picker/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
<dimen name="pick_chose_bkg_radius">2dp</dimen>
2525
<dimen name="pick_chose_split_line">0.1dp</dimen>
2626
<dimen name="pick_choose_item_text_size">16sp</dimen>
27+
<dimen name="album_preview_video_pause_size">90dp</dimen>
2728
</resources>

0 commit comments

Comments
 (0)