Commit 4555129f authored by gaodapeng's avatar gaodapeng

优化了bitmap的回收

parent d3c90cdb
......@@ -58,7 +58,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
super(context, attrs);
mContext = context;
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();
initView(context);
......@@ -67,7 +67,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.banner_imge_video, this, true);
mViewPager = view.findViewById(R.id.view_pager);
mAdapter = new ViewsPagerAdapter(((FragmentActivity)context).getSupportFragmentManager());
mAdapter = new ViewsPagerAdapter(((FragmentActivity) context).getSupportFragmentManager());
mViewPager.setAdapter(mAdapter);
mViewPager.setOnPageChangeListener(this);
//ViewPager手势滑动禁用
......@@ -79,14 +79,13 @@ 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);
}
public void addData(List<com.widget.imagevideobanner.bean.BannerBean> listBean){
public void addData(List<com.widget.imagevideobanner.bean.BannerBean> listBean) {
mAdapter.addData(listBean);
}
......@@ -94,11 +93,11 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
* 初始化视频的Bitmap
*/
private void initBannerBitmap() {
if(mList.size() == 0){
if (mList.size() == 0) {
return;
}
for(BannerBean bannerBean : mList){
if(bannerBean.getType() == 1 && null == bannerBean.getLoadingImage()){
for (BannerBean bannerBean : mList) {
if (bannerBean.getType() == 1 && null == bannerBean.getLoadingImage()) {
setLoadingImages(bannerBean);
}
}
......@@ -107,9 +106,10 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
/**
* 获取视频的第一帧作为截图
*
* @param bannerBean
*/
private void setLoadingImages(final BannerBean bannerBean){
private void setLoadingImages(final BannerBean bannerBean) {
if (sExecutor == null) {
sExecutor = Executors.newScheduledThreadPool(5);
}
......@@ -118,17 +118,18 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
public Boolean call() {
try {
String url = bannerBean.getUrl();
if(!url.contains("android.resource")){
if (!url.contains("android.resource")) {
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
mmr.setDataSource(url);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
Bitmap b = mmr.getFrameAtTime(); // frame at 2 seconds
bannerBean.setLoadingImage( BitmapUtils.getBytes(b));
bannerBean.setLoadingImage(BitmapUtils.getBytes(b));
b.recycle();
mmr.release();
}
return true;
}catch (Exception e){
} catch (Exception e) {
return false;
}
}
......@@ -143,7 +144,6 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
......@@ -153,7 +153,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
public void onPageSelected(int position) {
autoCurrIndex = position;
// Toast.makeText(context,"position="+position,Toast.LENGTH_SHORT).show();
if(mList.get(position).getType() ==1){
if (mList.get(position).getType() == 1) {
//如果是视频
stopBanner();
}
......@@ -180,7 +180,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
}
};
public void stopBanner(){
public void stopBanner() {
if (timer != null) {
timer.cancel();
timer = null;
......@@ -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;
}
public void startBanner(){
public void startBanner() {
//如果第一页是视频 不用定时器
if(mList.get(0).getType() == 0){
if (mList.get(0).getType() == 0) {
startBanner(period);
}
}
public void startBanner(long delay){
public void startBanner(long delay) {
stopBanner();
timer = new Timer();
createTimerTask();//创建定时器
timer.schedule(timerTask,delay,period);
timer.schedule(timerTask, delay, period);
}
public void createTimerTask(){
public void createTimerTask() {
timerTask = new TimerTask() {
@Override
public void run() {
......@@ -229,7 +229,7 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
@Override
public void onVideoCompletion(MediaPlayer mp) {
if(mList.size() ==1){
if (mList.size() == 1) {
//当集合中只有一个视频时,循环播放
mAdapter.getFragment().circulationPlayer();
}
......@@ -241,9 +241,10 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
startBanner(0);
}
private final class ViewsPagerAdapter extends FragmentStatePagerAdapter{
private final class ViewsPagerAdapter extends FragmentStatePagerAdapter {
private com.widget.imagevideobanner.banner.ImageVideoFragment fragment;
private FragmentManager fm;
public ViewsPagerAdapter(FragmentManager fm) {
super(fm);
this.fm = fm;
......@@ -255,18 +256,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
fragment.setOnVideoCompletionListener(ImageVideoBanner.this);
com.widget.imagevideobanner.bean.BannerBean bannerBean = mList.get(position);
Bundle bundle = new Bundle();
bundle.putSerializable("bannerBean",bannerBean);
bundle.putBoolean("loop",getLoop());
bundle.putSerializable("bannerBean", bannerBean);
bundle.putBoolean("loop", getLoop());
fragment.setArguments(bundle);
return fragment;
}
/**
* 只有1个视频的时候循环播放
*
* @return
*/
private boolean getLoop() {
if(mList.size() == 1 && mList.get(0).getType() == 1){
if (mList.size() == 1 && mList.get(0).getType() == 1) {
return true;
}
return false;
......@@ -285,19 +287,19 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
return super.getItemPosition(object);
}
public void replaceData(List<BannerBean> listBean){
public void replaceData(List<BannerBean> listBean) {
// 新数据和原来数据对比,不一致才去刷新
boolean change = compareData(listBean);
if(!change){
if (!change) {
notifyDataSetChanged();
// startBanner();
return;
}
if(null != listBean){
if (null != listBean) {
mList.clear();
addData(listBean);
//如果第一张是图片 需要开启定时器
if(listBean.get(0).getType() == 0){
if (listBean.get(0).getType() == 0) {
startBanner();
}
autoCurrIndex = 0;
......@@ -308,17 +310,18 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
/**
* 对比数据
*
* @param listBean
* @return
*/
private boolean compareData(List<BannerBean> listBean){
if(listBean == null || listBean.size() == 0){
private boolean compareData(List<BannerBean> listBean) {
if (listBean == null || listBean.size() == 0) {
return false;
}
if(listBean.size() == mList.size()){
for(int i = 0 ; i < mList.size() ; i++){
if(mList.get(i).getType() != listBean.get(i).getType()
|| !mList.get(i).getUrl().equals(listBean.get(i).getUrl())){
if (listBean.size() == mList.size()) {
for (int i = 0; i < mList.size(); i++) {
if (mList.get(i).getType() != listBean.get(i).getType()
|| !mList.get(i).getUrl().equals(listBean.get(i).getUrl())) {
return true;
}
}
......@@ -328,8 +331,8 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
}
public void addData(List<BannerBean> listBean){
if(null != listBean){
public void addData(List<BannerBean> listBean) {
if (null != listBean) {
mList.addAll(listBean);
}
initBannerBitmap();
......@@ -341,6 +344,5 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCh
}
}
}
package com.widget.imagevideobanner.banner;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
......@@ -88,10 +91,10 @@ public class ImageVideoFragment extends Fragment {
view = LayoutInflater.from(getActivity()).inflate(R.layout.item_image_view, container, false);
ImageView imageView = view.findViewById(R.id.iv);
File file = new File(bannerBean.getUrl());
if(file.exists()){
if (file.exists()) {
Glide.with(container.getContext()).load(file)
.into(imageView);
}else {
} else {
Glide.with(container.getContext()).load(bannerBean.getUrl())
.into(imageView);
}
......@@ -116,20 +119,19 @@ public class ImageVideoFragment extends Fragment {
*/
public void setPicture() {
//视频未加载完成,使用菊花
if(bannerBean.getLoadingImage() == null){
if(bannerBean.getUrl().contains("android.resource")){
if (bannerBean.getLoadingImage() == null) {
if (bannerBean.getUrl().contains("android.resource")) {
ivWaitLoading.setVisibility(View.VISIBLE);
ivWaitLoading.setImageResource(R.drawable.guide_defaut);
llWaitLoading.setVisibility(View.GONE);
}else{
} else {
ivWaitLoading.setVisibility(View.GONE);
llWaitLoading.setVisibility(View.VISIBLE);
}
}else{//加载完成使用首帧图片
} else {//加载完成使用首帧图片
ivWaitLoading.setVisibility(View.VISIBLE);
ivWaitLoading.setImageBitmap(BitmapUtils.getBitmap(bannerBean.getLoadingImage()));
llWaitLoading.setVisibility(View.GONE);
}
}
......@@ -142,7 +144,7 @@ public class ImageVideoFragment extends Fragment {
mVideoView.requestFocus();
try {
mVideoView.setVideoURI(Uri.parse(bannerBean.getUrl()));
}catch (Exception e){
} catch (Exception e) {
}
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
......@@ -221,7 +223,7 @@ public class ImageVideoFragment extends Fragment {
public void startPlayer() {
if (null != mVideoView) {
// setWaitLoading();
Log.e(TAG, "startPlayer "+currentPosition);
Log.e(TAG, "startPlayer " + currentPosition);
mVideoView.setBackgroundColor(Color.TRANSPARENT);
mVideoView.seekTo(currentPosition);
mVideoView.start();
......@@ -265,7 +267,7 @@ public class ImageVideoFragment extends Fragment {
private void pausePlayer() {
if (null != mVideoView) {
Log.e(TAG, "pausePlayer 当前进度是"+mVideoView.getCurrentPosition());
Log.e(TAG, "pausePlayer 当前进度是" + mVideoView.getCurrentPosition());
mVideoView.setBackgroundColor(getResources().getColor(R.color.white));
playerPaused = true;
this.currentPosition = mVideoView.getCurrentPosition();
......@@ -351,6 +353,21 @@ public class ImageVideoFragment extends Fragment {
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
public void 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