Commit 4555129f authored by gaodapeng's avatar gaodapeng

优化了bitmap的回收

parent d3c90cdb
package com.widget.imagevideobanner.banner; package com.widget.imagevideobanner.banner;
import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer; import android.media.MediaPlayer;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
...@@ -88,10 +91,10 @@ public class ImageVideoFragment extends Fragment { ...@@ -88,10 +91,10 @@ public class ImageVideoFragment extends Fragment {
view = LayoutInflater.from(getActivity()).inflate(R.layout.item_image_view, container, false); view = LayoutInflater.from(getActivity()).inflate(R.layout.item_image_view, container, false);
ImageView imageView = view.findViewById(R.id.iv); ImageView imageView = view.findViewById(R.id.iv);
File file = new File(bannerBean.getUrl()); File file = new File(bannerBean.getUrl());
if(file.exists()){ if (file.exists()) {
Glide.with(container.getContext()).load(file) Glide.with(container.getContext()).load(file)
.into(imageView); .into(imageView);
}else { } else {
Glide.with(container.getContext()).load(bannerBean.getUrl()) Glide.with(container.getContext()).load(bannerBean.getUrl())
.into(imageView); .into(imageView);
} }
...@@ -116,21 +119,20 @@ public class ImageVideoFragment extends Fragment { ...@@ -116,21 +119,20 @@ public class ImageVideoFragment extends Fragment {
*/ */
public void setPicture() { public void setPicture() {
//视频未加载完成,使用菊花 //视频未加载完成,使用菊花
if(bannerBean.getLoadingImage() == null){ if (bannerBean.getLoadingImage() == null) {
if(bannerBean.getUrl().contains("android.resource")){ if (bannerBean.getUrl().contains("android.resource")) {
ivWaitLoading.setVisibility(View.VISIBLE); ivWaitLoading.setVisibility(View.VISIBLE);
ivWaitLoading.setImageResource(R.drawable.guide_defaut); ivWaitLoading.setImageResource(R.drawable.guide_defaut);
llWaitLoading.setVisibility(View.GONE); llWaitLoading.setVisibility(View.GONE);
}else{ } else {
ivWaitLoading.setVisibility(View.GONE); ivWaitLoading.setVisibility(View.GONE);
llWaitLoading.setVisibility(View.VISIBLE); llWaitLoading.setVisibility(View.VISIBLE);
} }
}else{//加载完成使用首帧图片 } else {//加载完成使用首帧图片
ivWaitLoading.setVisibility(View.VISIBLE); ivWaitLoading.setVisibility(View.VISIBLE);
ivWaitLoading.setImageBitmap(BitmapUtils.getBitmap(bannerBean.getLoadingImage())); ivWaitLoading.setImageBitmap(BitmapUtils.getBitmap(bannerBean.getLoadingImage()));
llWaitLoading.setVisibility(View.GONE); llWaitLoading.setVisibility(View.GONE);
}
}
} }
...@@ -142,7 +144,7 @@ public class ImageVideoFragment extends Fragment { ...@@ -142,7 +144,7 @@ public class ImageVideoFragment extends Fragment {
mVideoView.requestFocus(); mVideoView.requestFocus();
try { try {
mVideoView.setVideoURI(Uri.parse(bannerBean.getUrl())); mVideoView.setVideoURI(Uri.parse(bannerBean.getUrl()));
}catch (Exception e){ } catch (Exception e) {
} }
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
...@@ -221,7 +223,7 @@ public class ImageVideoFragment extends Fragment { ...@@ -221,7 +223,7 @@ public class ImageVideoFragment extends Fragment {
public void startPlayer() { public void startPlayer() {
if (null != mVideoView) { if (null != mVideoView) {
// setWaitLoading(); // setWaitLoading();
Log.e(TAG, "startPlayer "+currentPosition); Log.e(TAG, "startPlayer " + currentPosition);
mVideoView.setBackgroundColor(Color.TRANSPARENT); mVideoView.setBackgroundColor(Color.TRANSPARENT);
mVideoView.seekTo(currentPosition); mVideoView.seekTo(currentPosition);
mVideoView.start(); mVideoView.start();
...@@ -265,7 +267,7 @@ public class ImageVideoFragment extends Fragment { ...@@ -265,7 +267,7 @@ public class ImageVideoFragment extends Fragment {
private void pausePlayer() { private void pausePlayer() {
if (null != mVideoView) { if (null != mVideoView) {
Log.e(TAG, "pausePlayer 当前进度是"+mVideoView.getCurrentPosition()); Log.e(TAG, "pausePlayer 当前进度是" + mVideoView.getCurrentPosition());
mVideoView.setBackgroundColor(getResources().getColor(R.color.white)); mVideoView.setBackgroundColor(getResources().getColor(R.color.white));
playerPaused = true; playerPaused = true;
this.currentPosition = mVideoView.getCurrentPosition(); this.currentPosition = mVideoView.getCurrentPosition();
...@@ -351,6 +353,21 @@ public class ImageVideoFragment extends Fragment { ...@@ -351,6 +353,21 @@ public class ImageVideoFragment extends Fragment {
sendPauseVideoMsg(); sendPauseVideoMsg();
} }
@Override
public void onDestroyView() {
if (ivWaitLoading != null) {
Drawable drawable = ivWaitLoading.getDrawable();
ivWaitLoading.setImageDrawable(null);
if (drawable instanceof BitmapDrawable) {
Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
if (!bm.isRecycled()) {
bm.recycle();
}
}
}
super.onDestroyView();
}
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment