[英]Android: Repeat a complex view inside a LinearLayout (Performance)
I m trying to create an application that need to show a list of VideoViews inside a complex layout. 我正在尝试创建一个需要在复杂布局内显示VideoView列表的应用程序。 The concept is very similar to Instagram and Facebook application.
这个概念与Instagram和Facebook应用程序非常相似。 The Home screen with a bunch of videos and other stuff.
主屏幕上有一堆视频和其他内容。
I've created a view composed by a toolbarr with three items, A videoView under the toolbar, and a bottomBar under the videoview with some textviews and imageviews. 我创建了一个由工具栏组成的视图,其中包含三个项目,工具栏下的videoView,以及带有某些textview和imageview的videoview下的bottomBar。
I inflate this view dinamically inside a simple Vertical LinearLayout. 我在一个简单的Vertical LinearLayout中将视图动态膨胀。 To stress as little as possible the processor, I create ten views at a time .. Once I reach the end of the scrollBar I add another 15 and so on.
为了尽可能减少处理器的压力,我一次创建了十个视图。.一旦到达scrollBar的末尾,我又添加了15个,依此类推。 Of course I don't initialize any video, I just set videoView Background with a Bitmap specially resized using inScale, inSampleSize and inDensity parameters.
当然,我不初始化任何视频,我只是使用位图设置videoView背景,并使用inScale,inSampleSize和inDensity参数对其进行了专门调整。 The concept is identical to Instagram.
这个概念与Instagram相同。 The problem comes when I reach an amount of 40 or 50 views.. It starts lag so bad.
当我达到40或50次观看量时,问题就来了。
This is how I inflate the layout: 这就是我给布局充气的方法:
View videoView = getLayoutInflater().inflate(R.layout.post_layout_2, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
params.setMargins(0, 25, 0, 0);
videoView.setLayoutParams(params);
//Initialize components
CircleImageView profImg = (CircleImageView) videoView.findViewById(R.id.post_profile_image);
TextView postUsername = (TextView) videoView.findViewById(R.id.post_username);
TextView postChallengeTitile = (TextView) videoView.findViewById(R.id.post_challenge_title);
TextView postViews = (TextView) videoView.findViewById(R.id.post_views_count);
TextView postLikes = (TextView) videoView.findViewById(R.id.post_like_count);
//Fill components
profImg.setImageBitmap(imgProfile);
postUsername.setText("@"+username);
postChallengeTitile.setText("#"+ challengeTitle);
postViews.setText(String.valueOf(NumberFormat.getNumberInstance(Locale.US).format(views)));
postLikes.setText(String.valueOf(NumberFormat.getNumberInstance(Locale.US).format(count)));
cont.addView(videoView);
final TextView durationTxt = (TextView) videoView.findViewById(R.id.video_duration_txt);
final VideoView video = (VideoView) videoView.findViewById(R.id.videoView2);
final ProgressBar progressDialog = (ProgressBar) videoView.findViewById(R.id.progressBar);
//final String pathVideo = "android.resource://" + getActivity().getPackageName() + "/" + R.raw.mot;
final String pathVideo = "https://videoLink";
final Uri uri = Uri.parse(pathVideo);
BitmapFactory.Options myBit = new BitmapFactory.Options();
myBit.inSampleSize = calculateInSampleSize(myBit, 300, 350);
myBit.inScaled = true;
myBit.inDensity = 600;
myBit.inTargetDensity = 300* myBit.inSampleSize;
Bitmap resize = BitmapFactory.decodeResource(getResources(), R.drawable.foto_test, myBit);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
video.setBackground(new BitmapDrawable(getResources(), resize));
}
I was wondering how does Instagram load all that number of videos without lag? 我想知道Instagram如何加载所有数量的视频而不会出现延迟? Is there some tricks I can use?
我可以使用一些技巧吗?
That is the classic use case for RecyclerView . 那是RecyclerView的经典用例。 If will inflate enough views to fill the screen, when you scroll down will reuse the created views.
如果将使足够多的视图填充到整个屏幕,则向下滚动时将重复使用创建的视图。
Another option is Litho from Facebook. 另一个选择是来自Facebook的Litho 。 Here is an article from Facebook about how they are doing it.
这是Facebook上有关他们的工作方式的文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.