The essence of the problem: There is an array of subtitles (long start, long end, string text). I need to change the width (int) for the corresponding item in RecyclerView depending on the display time. If the end and the beginning of the next one do not coincide in time, then I add an empty element between them.

subs.add(new Subtitle(subtitles.get(i - 1).getEnd(), subtitles.get(i).getStart(), "")); 

secInPx = 50; - number of pixels per 1 second of time. At first I tried to do this:

 int width = (int) ((end - start)*secInPx)/1000 

on each subtitle there will be a loss of fractions of pixels. For 45 minutes of video (approximately 2000 subtitles) I get an offset of about 400 pixels.

then I decided to add to the next element everything that was lost earlier. rvWidth - the width of the RecyclerView,

 rvWidth = (videoDuration / 1000) * Constant.secInPx; for (int i = 0; i < subtitles.size(); i++) { widthSums.add((int) ((subtitles.get(i).getEnd()*rvWidth)/videoDuration)); } subtitles.get(0).setWidth(widthSums.get(0)); for (int i = 1; i < subtitles.size(); i++) { subtitles.get(i).setWidth(widthSums.get(i) - widthSums.get(i-1)); } 

now we get an offset of about 30 pixels, the same 45 minutes, 2000 subtitles.

Screenshot How can you even more accurately determine the width of the elements?

The red lines are where the show should start and the end of the subtitles "did the allfather"

  • Can use in calculations float or double ? The division of a priori has an error, discarding the remainder. - woesss
  • I cannot then use anything other than an integer in changing width. In my second algorithm, I tried to correct the error, but so far I have not received an accurate result (( - Andriy Martsinkevych
  • At the time of application, you can lead to the desired type. From the question I cannot grasp the essence of the problem - how is the width of the element related to time and their number? - woesss
  • already tried to do so, the first way that I described here. Edited the question, added a photo. Subtitles lose pixels relative to timeline - Andriy Martsinkevych

0