我正在尝试创建一个需要在复杂布局中显示VideoView列表的应用程序。这个概念与Instagram和Facebook应用程序非常相似。主屏幕上有一堆视频和其他东西。
我创建了一个由带有三个项目的工具栏组成的视图,工具栏下的videoView和视频下的bottomBar,带有一些textview和imageview。
我在一个简单的Vertical LinearLayout中以幻想方式对这个视图进行了充气。为了尽量减少处理器的压力,我一次创建十个视图。一旦我到达滚动条的末尾,我再添加15个,依此类推。 当然我没有初始化任何视频,我只是使用inScale,inSampleSize和inDensity参数专门调整大小的Bitmap来设置videoView Background。 这个概念与Instagram完全相同。当我达到40或50个观看量时问题就出现了..它开始滞后这么糟糕。
这就是我如何夸大布局:
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));
}
我想知道Instagram如何在没有延迟的情况下加载所有数量的视频?我可以使用一些技巧吗?