Commit 4555129f authored by gaodapeng's avatar gaodapeng

优化了bitmap的回收

parent d3c90cdb
...@@ -36,7 +36,7 @@ import java.util.concurrent.ScheduledExecutorService; ...@@ -36,7 +36,7 @@ import java.util.concurrent.ScheduledExecutorService;
import wseemann.media.FFmpegMediaMetadataRetriever; import wseemann.media.FFmpegMediaMetadataRetriever;
public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageChangeListener, com.widget.imagevideobanner.banner.ImageVideoFragment.OnVideoCompletionListener { public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageChangeListener, com.widget.imagevideobanner.banner.ImageVideoFragment.OnVideoCompletionListener {
private static final int UPTATE_VIEWPAGER = 0; private static final int UPTATE_VIEWPAGER = 0;
private ViewPager mViewPager; private ViewPager mViewPager;
private List<BannerBean> mList = new ArrayList<>(); private List<BannerBean> mList = new ArrayList<>();
...@@ -58,7 +58,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -58,7 +58,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
super(context, attrs); super(context, attrs);
mContext = context; mContext = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.banner); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.banner);
period = typedArray.getInt(R.styleable.banner_period,5000); period = typedArray.getInt(R.styleable.banner_period, 5000);
typedArray.recycle(); typedArray.recycle();
initView(context); initView(context);
...@@ -67,7 +67,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -67,7 +67,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
private void initView(Context context) { private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.banner_imge_video, this, true); View view = LayoutInflater.from(context).inflate(R.layout.banner_imge_video, this, true);
mViewPager = view.findViewById(R.id.view_pager); mViewPager = view.findViewById(R.id.view_pager);
mAdapter = new ViewsPagerAdapter(((FragmentActivity)context).getSupportFragmentManager()); mAdapter = new ViewsPagerAdapter(((FragmentActivity) context).getSupportFragmentManager());
mViewPager.setAdapter(mAdapter); mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(this); mViewPager.setOnPageChangeListener(this);
//ViewPager手势滑动禁用 //ViewPager手势滑动禁用
...@@ -79,26 +79,25 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -79,26 +79,25 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
}); });
} }
public void replaceData(List<com.widget.imagevideobanner.bean.BannerBean> listBean){ public void replaceData(List<com.widget.imagevideobanner.bean.BannerBean> listBean) {
mAdapter.replaceData(listBean); mAdapter.replaceData(listBean);
} }
public void addData(List<com.widget.imagevideobanner.bean.BannerBean> listBean){ public void addData(List<com.widget.imagevideobanner.bean.BannerBean> listBean) {
mAdapter.addData(listBean); mAdapter.addData(listBean);
} }
/** /**
* 初始化视频的Bitmap * 初始化视频的Bitmap
*/ */
private void initBannerBitmap() { private void initBannerBitmap() {
if(mList.size() == 0){ if (mList.size() == 0) {
return; return;
} }
for(BannerBean bannerBean : mList){ for (BannerBean bannerBean : mList) {
if(bannerBean.getType() == 1 && null == bannerBean.getLoadingImage()){ if (bannerBean.getType() == 1 && null == bannerBean.getLoadingImage()) {
setLoadingImages(bannerBean); setLoadingImages(bannerBean);
} }
} }
...@@ -107,9 +106,10 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -107,9 +106,10 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
/** /**
* 获取视频的第一帧作为截图 * 获取视频的第一帧作为截图
*
* @param bannerBean * @param bannerBean
*/ */
private void setLoadingImages(final BannerBean bannerBean){ private void setLoadingImages(final BannerBean bannerBean) {
if (sExecutor == null) { if (sExecutor == null) {
sExecutor = Executors.newScheduledThreadPool(5); sExecutor = Executors.newScheduledThreadPool(5);
} }
...@@ -118,17 +118,18 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -118,17 +118,18 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
public Boolean call() { public Boolean call() {
try { try {
String url = bannerBean.getUrl(); String url = bannerBean.getUrl();
if(!url.contains("android.resource")){ if (!url.contains("android.resource")) {
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever(); FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(url); mmr.setDataSource(url);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM); mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST); mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
Bitmap b = mmr.getFrameAtTime(); // frame at 2 seconds Bitmap b = mmr.getFrameAtTime(); // frame at 2 seconds
bannerBean.setLoadingImage( BitmapUtils.getBytes(b)); bannerBean.setLoadingImage(BitmapUtils.getBytes(b));
b.recycle();
mmr.release(); mmr.release();
} }
return true; return true;
}catch (Exception e){ } catch (Exception e) {
return false; return false;
} }
} }
...@@ -143,7 +144,6 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -143,7 +144,6 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
} }
@Override @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
...@@ -153,7 +153,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -153,7 +153,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
public void onPageSelected(int position) { public void onPageSelected(int position) {
autoCurrIndex = position; autoCurrIndex = position;
// Toast.makeText(context,"position="+position,Toast.LENGTH_SHORT).show(); // Toast.makeText(context,"position="+position,Toast.LENGTH_SHORT).show();
if(mList.get(position).getType() ==1){ if (mList.get(position).getType() == 1) {
//如果是视频 //如果是视频
stopBanner(); stopBanner();
} }
...@@ -180,7 +180,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -180,7 +180,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
} }
}; };
public void stopBanner(){ public void stopBanner() {
if (timer != null) { if (timer != null) {
timer.cancel(); timer.cancel();
timer = null; timer = null;
...@@ -194,25 +194,25 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -194,25 +194,25 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
/** /**
* 设置轮播图 图片展示间隔 * 设置轮播图 图片展示间隔
*/ */
public void setPeriod(int period ){ public void setPeriod(int period) {
this.period = period; this.period = period;
} }
public void startBanner(){ public void startBanner() {
//如果第一页是视频 不用定时器 //如果第一页是视频 不用定时器
if(mList.get(0).getType() == 0){ if (mList.get(0).getType() == 0) {
startBanner(period); startBanner(period);
} }
} }
public void startBanner(long delay){ public void startBanner(long delay) {
stopBanner(); stopBanner();
timer = new Timer(); timer = new Timer();
createTimerTask();//创建定时器 createTimerTask();//创建定时器
timer.schedule(timerTask,delay,period); timer.schedule(timerTask, delay, period);
} }
public void createTimerTask(){ public void createTimerTask() {
timerTask = new TimerTask() { timerTask = new TimerTask() {
@Override @Override
public void run() { public void run() {
...@@ -229,7 +229,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -229,7 +229,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
@Override @Override
public void onVideoCompletion(MediaPlayer mp) { public void onVideoCompletion(MediaPlayer mp) {
if(mList.size() ==1){ if (mList.size() == 1) {
//当集合中只有一个视频时,循环播放 //当集合中只有一个视频时,循环播放
mAdapter.getFragment().circulationPlayer(); mAdapter.getFragment().circulationPlayer();
} }
...@@ -241,9 +241,10 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -241,9 +241,10 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
startBanner(0); startBanner(0);
} }
private final class ViewsPagerAdapter extends FragmentStatePagerAdapter{ private final class ViewsPagerAdapter extends FragmentStatePagerAdapter {
private com.widget.imagevideobanner.banner.ImageVideoFragment fragment; private com.widget.imagevideobanner.banner.ImageVideoFragment fragment;
private FragmentManager fm; private FragmentManager fm;
public ViewsPagerAdapter(FragmentManager fm) { public ViewsPagerAdapter(FragmentManager fm) {
super(fm); super(fm);
this.fm = fm; this.fm = fm;
...@@ -255,18 +256,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -255,18 +256,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
fragment.setOnVideoCompletionListener(ImageVideoBanner.this); fragment.setOnVideoCompletionListener(ImageVideoBanner.this);
com.widget.imagevideobanner.bean.BannerBean bannerBean = mList.get(position); com.widget.imagevideobanner.bean.BannerBean bannerBean = mList.get(position);
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putSerializable("bannerBean",bannerBean); bundle.putSerializable("bannerBean", bannerBean);
bundle.putBoolean("loop",getLoop()); bundle.putBoolean("loop", getLoop());
fragment.setArguments(bundle); fragment.setArguments(bundle);
return fragment; return fragment;
} }
/** /**
* 只有1个视频的时候循环播放 * 只有1个视频的时候循环播放
*
* @return * @return
*/ */
private boolean getLoop() { private boolean getLoop() {
if(mList.size() == 1 && mList.get(0).getType() == 1){ if (mList.size() == 1 && mList.get(0).getType() == 1) {
return true; return true;
} }
return false; return false;
...@@ -285,19 +287,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -285,19 +287,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
return super.getItemPosition(object); return super.getItemPosition(object);
} }
public void replaceData(List<BannerBean> listBean){ public void replaceData(List<BannerBean> listBean) {
// 新数据和原来数据对比,不一致才去刷新 // 新数据和原来数据对比,不一致才去刷新
boolean change = compareData(listBean); boolean change = compareData(listBean);
if(!change){ if (!change) {
notifyDataSetChanged(); notifyDataSetChanged();
// startBanner(); // startBanner();
return; return;
} }
if(null != listBean){ if (null != listBean) {
mList.clear(); mList.clear();
addData(listBean); addData(listBean);
//如果第一张是图片 需要开启定时器 //如果第一张是图片 需要开启定时器
if(listBean.get(0).getType() == 0){ if (listBean.get(0).getType() == 0) {
startBanner(); startBanner();
} }
autoCurrIndex = 0; autoCurrIndex = 0;
...@@ -307,18 +309,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -307,18 +309,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
/** /**
* 对比数据 * 对比数据
* @param listBean *
* @param listBean
* @return * @return
*/ */
private boolean compareData(List<BannerBean> listBean){ private boolean compareData(List<BannerBean> listBean) {
if(listBean == null || listBean.size() == 0){ if (listBean == null || listBean.size() == 0) {
return false; return false;
} }
if(listBean.size() == mList.size()){ if (listBean.size() == mList.size()) {
for(int i = 0 ; i < mList.size() ; i++){ for (int i = 0; i < mList.size(); i++) {
if(mList.get(i).getType() != listBean.get(i).getType() if (mList.get(i).getType() != listBean.get(i).getType()
|| !mList.get(i).getUrl().equals(listBean.get(i).getUrl())){ || !mList.get(i).getUrl().equals(listBean.get(i).getUrl())) {
return true; return true;
} }
} }
...@@ -328,8 +331,8 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -328,8 +331,8 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
} }
public void addData(List<BannerBean> listBean){ public void addData(List<BannerBean> listBean) {
if(null != listBean){ if (null != listBean) {
mList.addAll(listBean); mList.addAll(listBean);
} }
initBannerBitmap(); initBannerBitmap();
...@@ -341,6 +344,5 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh ...@@ -341,6 +344,5 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
} }
} }
} }
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