我需要在视频搜索栏的每一秒上绘制图像。我已经放置了一个搜索栏,并在具有透明背景的搜索栏上方的xml中放置了水平布局。我已经在该linearlayout中动态填充了图像。但是我需要将图像放置在视频的特定秒上,例如10秒钟,50秒钟,200秒钟。现在,当搜索栏达到10秒时,10秒钟的图像不会位于搜索栏下方。
任何帮助都是有意义的。
video_preview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/black_transparent_background">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<Button
android:id="@+id/btnBack"
android:layout_width="15dp"
android:layout_height="20dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:background="@drawable/back_arrow" />
<TextView
android:id="@+id/txtEventTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="25dp"
android:layout_toRightOf="@id/btnBack"
android:text="Santa Rosa Marathon"
android:textColor="@color/white"
android:textSize="@dimen/text_24" />
<Button
android:id="@+id/btnCamera"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_margin="20dp"
android:background="@drawable/camera_icon_white" />
<RelativeLayout
android:id="@+id/laySeekbarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/laySeekbar"
android:layout_marginBottom="5dp"
android:background="@drawable/seekbar_text_back">
<com.neurun.utils.FontTextViewBold
android:id="@+id/seekBarProgressValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:padding="2dp"
android:text="0 mi"
android:textColor="@color/black" />
<com.neurun.utils.FontTextViewBold
android:id="@+id/txtVideoElevationValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seekBarProgressValue"
android:layout_centerHorizontal="true"
android:padding="2dp"
android:text="0 sqft"
android:textColor="@color/black" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/laySeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="@+id/seekBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:background="@drawable/seekbar_layout_background">
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressBackgroundTint="@color/white"
android:thumb="@drawable/seekbar_thumb" />
</RelativeLayout>
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="17dp"
android:layout_marginRight="17dp"
android:scrollbars="none">
<LinearLayout
android:id="@+id/layLandmarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<ProgressBar
android:id="@+id/videoProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
我的班级文件:
public class VideoPreviewTest extends BaseActivity implements View.OnClickListener
{
LinearLayoutManager layoutManager;
ProgressBar videoProgress;
TextView txtEventTitle, seekBarProgressValue, txtVideoElevationValue;
RelativeLayout laySeekbarText;
VideoView videoView;
MediaMetadataRetriever mediaMetadataRetriever;
private int position = 0;
private MediaController mediaController;
SeekBar seekBar;
boolean isPlaying = false;
ImageView imgStart;
Button btnBack, btnCamera, btn2x;
int length;
String eventId, routeId;
LinearLayout layLandmarks;
int videoCurrentProgress;
HorizontalScrollView horizontalScrollView1;
// String videoUrl="https://s3.amazonaws.com/neurun-hosting-mobilehub-2051724628/54MB_SF+1st+Half+Marathon+(480p_low_Garmin+%2B+4x_480p_5mins_iMovie).mp4";
//String videoUrl = "http://www.youtubemaza.com/files/data/366/Tom%20And%20Jerry%20055%20Casanova%20Cat%20(1951).mp4";
String videoUrl;
@Override
protected void onRestart() {
super.onRestart();
Log.e("inside restart >", "restart");
videoView.start();
}
@Override
protected void onPause() {
super.onPause();
Log.e("inside onPause >>", "onPause");
// mVideoUri = null;
if (videoView != null) {
videoView.pause();
length = videoView.getCurrentPosition();
Log.e("player stopped at >", length + "");
// mMediaPlayer = null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_preview);
txtVideoElevationValue = (TextView) findViewById(R.id.txtVideoElevationValue);
seekBarProgressValue = (TextView) findViewById(R.id.seekBarProgressValue);
laySeekbarText = (RelativeLayout) findViewById(R.id.laySeekbarText);
// laySeekbarText.setVisibility(View.GONE);
horizontalScrollView1 = (HorizontalScrollView) findViewById(R.id.horizontalScrollView1);
VideoPreviewTest.this.runOnUiThread(new Runnable() {
@Override
public void run() {
horizontalScrollView1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
});
videoProgress = (ProgressBar) findViewById(R.id.videoProgress);
videoProgress.setVisibility(View.VISIBLE);
txtEventTitle = (TextView) findViewById(R.id.txtEventTitle);
layLandmarks = (LinearLayout) findViewById(R.id.layLandmarks);
String eventName = getIntent().getExtras().getString("eventName", "");
txtEventTitle.setText(eventName);
eventId = getIntent().getExtras().getString("eventId", "");
Log.e("event id >>", eventId);
routeId = getIntent().getExtras().getString("routeId", "");
Log.e("routeId >>", routeId);
String vi = getIntent().getExtras().getString("videoUrl", "").trim();
videoUrl = vi;
mediaMetadataRetriever = new MediaMetadataRetriever();
try {
if (Build.VERSION.SDK_INT >= 14) {
mediaMetadataRetriever.setDataSource(videoUrl, new HashMap<String, String>());
} else {
mediaMetadataRetriever.setDataSource(videoUrl);
}
} catch (Exception e) {
e.printStackTrace();
}
videoView = (VideoView) findViewById(R.id.videoView);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
params.width = metrics.widthPixels;
params.height = metrics.heightPixels;
params.leftMargin = 0;
videoView.setLayoutParams(params);
imgStart = (ImageView) findViewById(R.id.imgStart);
imgStart.setOnClickListener(this);
btn2x = (Button) findViewById(R.id.btnZoom);
btnBack = (Button) findViewById(R.id.btnBack);
btnCamera = (Button) findViewById(R.id.btnCamera);
btnCamera.setOnClickListener(this);
btnBack.setOnClickListener(this);
btn2x.setOnClickListener(this);
seekBar = (SeekBar) findViewById(R.id.seekBar);
int seekbarWidth = seekBar.getWidth();
Log.e("seekbarWidth >>", seekbarWidth + "");
seekBar.getProgressDrawable().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.MULTIPLY);
seekBar.getThumb().setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_IN);
int width = seekBar.getWidth()
- seekBar.getPaddingLeft()
- seekBar.getPaddingRight();
int thumbPos = seekBar.getPaddingLeft()
+ width
* seekBar.getProgress()
/ seekBar.getMax();
// Log.e("seekbar width, thumbPos1 >>", width + "" + thumbPos);
// Set the media controller buttons
if (mediaController == null) {
mediaController = new MediaController(VideoPreviewTest.this);
// Set the videoView that acts as the anchor for the MediaController.
mediaController.setAnchorView(videoView);
// Set MediaController for VideoView
// videoView.setMediaController(mediaController);
}
try {
// ID of video file.
/* int id = this.getRawResIdByName("myvideo");
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + id));*/
Uri video = Uri.parse(videoUrl);
videoView.setVideoURI(video);
Log.e("video duration >>", videoView.getDuration() + "");
// videoView.start();
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.seekTo(position);
if (position == 0) {
videoView.start();
}
seekBar.setMax(videoView.getDuration());
seekBar.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (seekBar.getViewTreeObserver().isAlive())
seekBar.getViewTreeObserver().removeOnPreDrawListener(this);
int seekbarWidth = seekBar.getWidth();
Log.e("seekbarWidth >>", seekbarWidth + "");
// put your code here
return false;
}
});
seekBar.postDelayed(onEverySecond, 1000);
mp.setLooping(true);
// When video Screen change size.
mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
// Re-Set the videoView that acts as the anchor for the MediaController
videoProgress.setVisibility(View.GONE);
mediaController.setAnchorView(videoView);
}
});
}
});
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
videoCurrentProgress = progress;
int seekbarWidth = seekBar.getWidth()
- seekBar.getPaddingLeft()
- seekBar.getPaddingRight();
int thumbPos = seekBar.getPaddingLeft()
+ seekbarWidth
* seekBar.getProgress()
/ seekBar.getMax();
Log.e("progress >>", progress + "");
say_minutes_left(progress, thumbPos);
if (fromUser) {
// this is when actually seekbar has been seeked to a new position
videoView.seekTo(progress);
}
}
});
if (CheckNetworkStatus.getInstance(VideoPreviewTest.this).isOnline()) {
new GetLandmarks(Constants.LANDMARKS, routeId, eventId).execute();
} else {
Utils.showSingleButtonAlert(VideoPreviewTest.this, getString(R.string.network_not_available), getString(R.string.ok));
}
}
private void say_minutes_left(int how_many, int thumbPos) {
String what_to_say = String.valueOf(how_many / 1000);
Log.e("progress ,thumb position", what_to_say + " , " + thumbPos);
seekBarProgressValue.setText(what_to_say + " sec");
int pos = how_many / 1000;
// txtVideoElevationValue.setText(elevationWithLandmarksArrayList.get(pos).getLandmarkDesc());
// seekBarProgressValue.setText("7.1 m 230sft");
int xPos = getSeekBarThumbPosX(seekBar);
Log.e("xpos >>", xPos + "");
int seek_label_pos = Math.abs((((seekBar.getRight() - seekBar.getLeft()) * seekBar.getProgress()) / seekBar.getMax()) + seekBar.getLeft());
Log.e("progress ,thumb position, seek_label_pos >", what_to_say + " , " + thumbPos + ", " + seek_label_pos);
laySeekbarText.setX(xPos - 40);
}
public int getSeekBarThumbPosX(SeekBar seekBar) {
int posX;
if (Build.VERSION.SDK_INT >= 16) {
posX = seekBar.getThumb().getBounds().centerX();
} else {
int left = seekBar.getLeft() + seekBar.getPaddingLeft();
int right = seekBar.getRight() - seekBar.getPaddingRight();
float width = (float) (seekBar.getProgress() * (right - left)) / seekBar.getMax();
posX = Math.round(width) + seekBar.getThumbOffset();
}
return posX;
}
private Runnable onEverySecond = new Runnable() {
@Override
public void run() {
if (seekBar != null) {
seekBar.setProgress(videoView.getCurrentPosition());
}
if (videoView.isPlaying()) {
seekBar.postDelayed(onEverySecond, 1000);
}
}
};
// Find ID corresponding to the name of the resource (in the directory raw).
public int getRawResIdByName(String resName) {
String pkgName = this.getPackageName();
// Return 0 if not found.
int resID = this.getResources().getIdentifier(resName, "raw", pkgName);
Log.e("AndroidVideoView", "Res Name: " + resName + "==> Res ID = " + resID);
return resID;
}
// When you change direction of phone, this method will be called.
// It store the state of video (Current position)
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Store current position.
savedInstanceState.putInt("CurrentPosition", videoView.getCurrentPosition());
videoView.pause();
}
// After rotating the phone. This method is called.
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Get saved position.
position = savedInstanceState.getInt("CurrentPosition");
videoView.seekTo(position);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
}
}
public class GetLandmarks extends AsyncTask<Void, String, String> {
private ProgressDialog pd = new ProgressDialog(VideoPreviewTest.this);
String apiUrl, eventId, routeId;
public GetLandmarks(String s, String routeIds, String eventId1) {
this.apiUrl = s;
this.eventId = eventId1;
this.routeId = routeIds;
apiUrl = apiUrl + "?" + "route_id=" + routeId + "&event_id=" + eventId;
Log.e("url >>", apiUrl);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd.setTitle(R.string.loading);
pd.show();
}
@Override
protected String doInBackground(Void... voids) {
// Utils.volleyPost(CreateAccount.this,strings[0],jsonBody);
String oauth_response = "error";
oauth_response = ConstantMethods.getMethodHttp(apiUrl);
Log.e("response >>", oauth_response);
if (oauth_response != null) {
return oauth_response;
} else return "error";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (pd != null && pd.isShowing()) {
pd.cancel();
}
Log.e("result >", result);
if (result.equalsIgnoreCase("error")) {
} else {
try {
String landmarkName = "", landmarkIcon = "", video_time = "", landmarkId = "", landmarkDesc = "", landmarkType = "";
double latitude = 0, longitude = 0;
JSONObject jsonObject = new JSONObject(result);
String landMarks = jsonObject.getString("landmarks");
String latLng = jsonObject.getString("latlngs");
String elevationData = jsonObject.getString("elevation_data");
Log.e("elevationData >>", elevationData);
JSONArray jsonArray = null;
if (landMarks.equalsIgnoreCase("[]")) {
} else {
jsonArray = new JSONArray(landMarks);
final ImageView[] dynamic_imageView = new ImageView[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.getJSONObject(i);
landmarkId = jsonObject1.getString("id");
landmarkName = jsonObject1.getString("title");
landmarkDesc = jsonObject1.getString("description");
latitude = jsonObject1.getDouble("latitude");
longitude = jsonObject1.getDouble("longitude");
video_time = jsonObject1.getString("video_time_in_sec");
Log.e("video_time_in_sec", video_time);
landmarkIcon = jsonObject1.getString("image");
Log.e("landmarkIcon", landmarkIcon);
landmarkType = jsonObject1.getString("item_type");
dynamic_imageView[i] = new ImageView(VideoPreviewTest.this);
LinearLayout.LayoutParams params;
if (jsonArray.length() > 5) {
params = new LinearLayout.LayoutParams(
30, 30
);
params.setMargins(10, 0, 10, 0);
} else {
params = new LinearLayout.LayoutParams(
30, 30
);
params.setMargins(100, 0, 100, 0);
}
dynamic_imageView[i].setLayoutParams(params);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(70, 70);
dynamic_imageView[i].setLayoutParams(lp);
Glide.with(VideoPreviewTest.this)
.load(landmarkIcon)
.override(30, 30)
.into(dynamic_imageView[i]);
dynamic_imageView[i].setId(i);
dynamic_imageView[i].setTag(landmarkType + "~" + landmarkId + "~" + landmarkName + "~" + landmarkDesc + "~" + latitude
+ "~" + longitude + "~" + video_time + "~" + landmarkIcon);
dynamic_imageView[i].setPadding(3, 0, 3, 0);
seekBar.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (seekBar.getViewTreeObserver().isAlive())
seekBar.getViewTreeObserver().removeOnPreDrawListener(this);
int seekbarWidth = seekBar.getWidth();
Log.e("seekbarWidth >>", seekbarWidth + "");
return false;
}
});
// Log.e("seekbar width >>", seekBar.getWidth() + "");
// LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(BaseApplication.getScreenWidth(), LinearLayout.LayoutParams.WRAP_CONTENT);
// layLandmarks.setLayoutParams(lp1);
layLandmarks.addView(dynamic_imageView[i]);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}