Commit 61e53d36 authored by 委座-江's avatar 委座-江

高度自适应

parent a59db2b6
......@@ -15,18 +15,18 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--<activity-->
<!--android:name=".MainActivity"-->
<!--android:configChanges="orientation|keyboardHidden|screenSize|keyboard|navigation"-->
<!--android:exported="true"-->
<!--android:launchMode="singleTask"-->
<!--android:screenOrientation="portrait">-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
<!--</activity>-->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize|keyboard|navigation"
android:exported="true"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
......
package com.widget.imagevideobanner;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import com.widget.imagevideobanner.banner.ImageVideoBanner;
import com.widget.imagevideobanner.bean.BannerBean;
......@@ -13,66 +11,34 @@ import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<BannerBean> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageVideoBanner banner = findViewById(R.id.banner);
// Button goSecondPager = findViewById(R.id.goSecondPager);
// goSecondPager.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// startActivity(new Intent(MainActivity.this, com.widget.imagevideobanner.MainActivity2.class));
// }
// });
setDatas();
banner.addData(list);
banner.startBanner();
}
private void setDatas() {
for (int i = 0; i < 1; i++) {
BannerBean listBean = new BannerBean();
if (i == 0) {
// String url = Environment.getExternalStorageDirectory() + "/huarun/video/video_home_1_72m.mp4";
String uri = "android.resource://" + getPackageName() + "/" + R.raw.default_guide;
listBean.setUrl(uri);
listBean.setType(1);//图片类型 视频
list.add(listBean);
} else if(i==1){
listBean.setUrl("http://pic11.nipic.com/20101201/4452735_182232064453_2.jpg");
listBean.setType(0);//图片类型 视频
list.add(listBean);
}else if(i == 2){
String url = Environment.getExternalStorageDirectory() + "/huarun/video/video_scan.mp4";
// String uri = "android.resource://" + getPackageName() + "/" + R.raw.video_scan;
String url = Environment.getExternalStorageDirectory() + "/Video_Compressed_converseBanner_2021_01_05_03_05_11_0.mp4";
listBean.setUrl(url);
listBean.setType(1);//图片类型 视频
listBean.setType(1);
list.add(listBean);
}else if(i == 3){
listBean.setUrl("http://120.26.246.123:18881/downloads/jjt1.jpg");
listBean.setType(0);//图片类型 视频
list.add(listBean);
} else if(i == 4){
listBean.setUrl("http://120.26.246.123:18881/downloads/jjt2.jpg");
listBean.setType(0);//图片类型 视频
} else if (i == 1) {
String url = Environment.getExternalStorageDirectory() + "/vertical_test.mp4";
listBean.setUrl(url);
listBean.setType(1);
list.add(listBean);
}
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.e("###","我是onNewIntent时间");
}
}
......@@ -125,7 +125,10 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCha
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
Bitmap b = mmr.getFrameAtTime(); // frame at 2 seconds
bannerBean.setLoadingImage(BitmapUtils.getBytes(b));
Bitmap bx = BitmapUtils.createBlurBitmap(b,40);
bannerBean.setLoadingBlurImage(BitmapUtils.getBytes(bx));
b.recycle();
bx.recycle();
mmr.release();
}
return true;
......@@ -241,6 +244,18 @@ public class ImageVideoBanner extends FrameLayout implements ViewPager.OnPageCha
startBanner(0);
}
@Override
public void updateSize(BannerBean bannerBean) {
if(mList.size() > 0 ){
for(BannerBean bean : mList){
if(bannerBean.getUrl().equals(bean.getUrl())){
bean.setHeight(bannerBean.getHeight());
bean.setWidth(bannerBean.getWidth());
}
}
}
}
private final class ViewsPagerAdapter extends FragmentStatePagerAdapter {
private com.widget.imagevideobanner.banner.ImageVideoFragment fragment;
private FragmentManager fm;
......
......@@ -11,6 +11,7 @@ import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
......@@ -27,6 +28,8 @@ import com.widget.imagevideobanner.utils.BannerLogFileUtils;
import com.widget.imagevideobanner.utils.BitmapUtils;
import java.io.File;
import java.lang.ref.WeakReference;
import java.math.BigDecimal;
public class ImageVideoFragment extends Fragment {
......@@ -38,39 +41,22 @@ public class ImageVideoFragment extends Fragment {
// 视频是否循环播放
private boolean loop;
private ImageView ivWaitLoading;
private ConstraintLayout cVideoView;
private LinearLayout llWaitLoading;
private int currentPosition;
private boolean playerPaused;
private String mUrl;
private final int STOP_PLAYER = 0x2000;
private final int START_PLAYER = 0x2001;
private final int PAUSE_PLAYER = 0x2002;
private final int SET_VIDEO_URL = 0x2003;
private static final int STOP_PLAYER = 0x2000;
private static final int START_PLAYER = 0x2001;
private static final int PAUSE_PLAYER = 0x2002;
private static final int SET_VIDEO_URL = 0x2003;
private static final int PROGRESS = 0x2004;
/**
* 使用Handler是为了避免出现ANR异常
*/
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOP_PLAYER:
stopPlayer();
break;
case START_PLAYER:
startPlayer();
break;
case PAUSE_PLAYER:
pausePlayer();
break;
case SET_VIDEO_URL:
setVideoUrl();
startPlayer();
break;
}
super.handleMessage(msg);
}
};
private Handler handler;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
......@@ -79,6 +65,7 @@ public class ImageVideoFragment extends Fragment {
bannerBean = (BannerBean) bundle.getSerializable("bannerBean");
loop = (boolean) bundle.getBoolean("loop");
Log.e(TAG, "type=" + bannerBean.getType() + ",url=" + bannerBean.getUrl());
handler = new InnerHandler(this);
}
@Nullable
......@@ -104,7 +91,9 @@ public class ImageVideoFragment extends Fragment {
mVideoView = view.findViewById(R.id.video_view);
ivWaitLoading = view.findViewById(R.id.iv_wait_loading_layout);
llWaitLoading = view.findViewById(R.id.wait_loading_layout);
setPicture();
cVideoView = view.findViewById(R.id.cVideoView);
setVideoBackground();
setPicture(bannerBean.getWidth(),bannerBean.getHeight());
initData();
}
} else {
......@@ -113,6 +102,13 @@ public class ImageVideoFragment extends Fragment {
return view;
}
public void setVideoBackground(){
if (bannerBean.getLoadingImage() == null) {
cVideoView.setBackgroundResource(R.drawable.guide_defaut);
} else {
cVideoView.setBackground(BitmapUtils.bitmap2Drawable(BitmapUtils.getBitmap(bannerBean.getLoadingBlurImage())));
}
}
/**
* 设置加载视频的等待图片
......@@ -135,12 +131,44 @@ public class ImageVideoFragment extends Fragment {
}
}
/**
* 设置加载视频的等待图片
*/
public void setPicture(int width,int height) {
if(width == 0 || height == 0){
return;
}
Log.e("####","草"+width+" "+height);
//设置设置尺寸
ConstraintLayout.LayoutParams lp2 = (ConstraintLayout.LayoutParams) ivWaitLoading.getLayoutParams();
lp2.height = height;
lp2.width = width;
ivWaitLoading.setLayoutParams(lp2);
//视频未加载完成,使用菊花
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 {
ivWaitLoading.setVisibility(View.GONE);
llWaitLoading.setVisibility(View.VISIBLE);
}
} else {//加载完成使用首帧图片
ivWaitLoading.setVisibility(View.VISIBLE);
ivWaitLoading.setImageBitmap(BitmapUtils.getBitmap(bannerBean.getLoadingImage()));
llWaitLoading.setVisibility(View.GONE);
}
}
private void initData() {
if (null != mVideoView) {
// mVideoView.setVideoPath(bannerBean.getUrl());
// mVideoView.setZOrderOnTop(true);
mVideoView.setAlpha(0);
mVideoView.requestFocus();
try {
mVideoView.setVideoURI(Uri.parse(bannerBean.getUrl()));
......@@ -150,6 +178,8 @@ public class ImageVideoFragment extends Fragment {
mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// mVideoView.setBackgroundColor(Color.TRANSPARENT);
// mVideoView.setVisibility(View.GONE);
mVideoView.pause();
if (null != listener) {
listener.onVideoCompletion(mp);
......@@ -160,12 +190,18 @@ public class ImageVideoFragment extends Fragment {
mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//重新刷新大小
refreshPortraitScreen(mp.getVideoWidth(),mp.getVideoHeight());
mp.setLooping(loop);
Log.e(TAG, "视频加载完成" + bannerBean.getUrl());
mp.setOnInfoListener(new MediaPlayer.OnInfoListener() {
@Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
mVideoView.setAlpha(1);
handler.sendEmptyMessage(PROGRESS);
llWaitLoading.setVisibility(View.GONE);
ivWaitLoading.setVisibility(View.GONE);
mVideoView.setBackgroundColor(Color.TRANSPARENT);
......@@ -208,6 +244,44 @@ public class ImageVideoFragment extends Fragment {
}
public void refreshPortraitScreen(int width,int height) {
int mHeight = mVideoView.getHeight();
int mWidth = mVideoView.getWidth();
BigDecimal viewScale = new BigDecimal(mWidth+"")
.divide(new BigDecimal(mHeight+""),2, BigDecimal.ROUND_HALF_UP);
BigDecimal videoScale = new BigDecimal(width+"")
.divide(new BigDecimal(height+""),2, BigDecimal.ROUND_HALF_UP);
if(viewScale.compareTo(videoScale) >= 0){
//按照高度缩放
BigDecimal heightScale = new BigDecimal(mHeight+"")
.divide(new BigDecimal(height+""),2, BigDecimal.ROUND_HALF_UP);
height = mHeight;
width = new BigDecimal(width+"").multiply(heightScale).intValue();
}else{
//按照宽度缩放
BigDecimal widthScale = new BigDecimal(mWidth+"")
.divide(new BigDecimal(width+""),2, BigDecimal.ROUND_HALF_UP);
width = mWidth;
height = new BigDecimal(height+"").multiply(widthScale).intValue();
}
//设置videoview 宽高
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) mVideoView.getLayoutParams();
lp.height = height;
lp.width = width;
mVideoView.setLayoutParams(lp);
if(bannerBean.getWidth() == 0 || bannerBean.getHeight() == 0){
bannerBean.setWidth(width);
bannerBean.setHeight(height);
listener.updateSize(bannerBean);
}
}
private void setVideoUrl() {
String url = bannerBean.getUrl();
......@@ -251,7 +325,6 @@ public class ImageVideoFragment extends Fragment {
private void stopPlayer() {
Log.e(TAG, "stopPlayer");
if (null != mVideoView) {
mVideoView.stopPlayback();
handler.removeCallbacksAndMessages(null);
}
......@@ -265,10 +338,19 @@ public class ImageVideoFragment extends Fragment {
return false;
}
public int getCurrentPosition() {
if (null != mVideoView) {
return mVideoView.getCurrentPosition();
}
return 0;
}
private void pausePlayer() {
if (null != mVideoView) {
Log.e(TAG, "pausePlayer 当前进度是" + mVideoView.getCurrentPosition());
mVideoView.setBackgroundColor(getResources().getColor(R.color.white));
mVideoView.setAlpha(0);
playerPaused = true;
this.currentPosition = mVideoView.getCurrentPosition();
mVideoView.pause();
......@@ -379,11 +461,40 @@ public class ImageVideoFragment extends Fragment {
public interface OnVideoCompletionListener {
void onVideoCompletion(MediaPlayer mp);
void onError(MediaPlayer mp);
void updateSize(BannerBean bannerBean);
}
public void setOnVideoCompletionListener(OnVideoCompletionListener listener) {
this.listener = listener;
}
private static class InnerHandler extends Handler{
private final WeakReference<Fragment> mFragment;
public InnerHandler(Fragment fragment){
mFragment =new WeakReference<Fragment>(fragment);
}
@Override
public void handleMessage(Message msg) {
ImageVideoFragment fragment=(ImageVideoFragment)mFragment.get();
super.handleMessage(msg);
if(fragment!=null){
switch (msg.what) {
case STOP_PLAYER:
fragment.stopPlayer();
break;
case START_PLAYER:
fragment.startPlayer();
break;
case PAUSE_PLAYER:
fragment.pausePlayer();
break;
case SET_VIDEO_URL:
fragment.setVideoUrl();
fragment.startPlayer();
break;
}
}
}
}
}
......@@ -6,6 +6,33 @@ public class BannerBean implements Serializable {
private String url;
private int type;
private byte[] loadingImage;
private byte[] loadingBlurImage;
private int width;
private int height;
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public byte[] getLoadingBlurImage() {
return loadingBlurImage;
}
public void setLoadingBlurImage(byte[] loadingBlurImage) {
this.loadingBlurImage = loadingBlurImage;
}
public byte[] getLoadingImage() {
return loadingImage;
......
......@@ -2,6 +2,8 @@ package com.widget.imagevideobanner.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import java.io.ByteArrayOutputStream;
......@@ -42,5 +44,204 @@ public class BitmapUtils {
}
}
/**
* 传递bitmap 传递模糊半径 返回一个被模糊的bitmap
* 比较耗时
* @param sentBitmap
* @param radius
* @return
*/
public static Bitmap createBlurBitmap(Bitmap sentBitmap, int radius) {
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
if (radius < 1) {
return (null);
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int[] pix = new int[w * h];
bitmap.getPixels(pix, 0, w, 0, 0, w, h);
int wm = w - 1;
int hm = h - 1;
int wh = w * h;
int div = radius + radius + 1;
int r[] = new int[wh];
int g[] = new int[wh];
int b[] = new int[wh];
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw;
int vmin[] = new int[Math.max(w, h)];
int divsum = (div + 1) >> 1;
divsum *= divsum;
int dv[] = new int[256 * divsum];
for (i = 0; i < 256 * divsum; i++) {
dv[i] = (i / divsum);
}
yw = yi = 0;
int[][] stack = new int[div][3];
int stackpointer;
int stackstart;
int[] sir;
int rbs;
int r1 = radius + 1;
int routsum, goutsum, boutsum;
int rinsum, ginsum, binsum;
for (y = 0; y < h; y++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
for (i = -radius; i <= radius; i++) {
p = pix[yi + Math.min(wm, Math.max(i, 0))];
sir = stack[i + radius];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rbs = r1 - Math.abs(i);
rsum += sir[0] * rbs;
gsum += sir[1] * rbs;
bsum += sir[2] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
}
stackpointer = radius;
for (x = 0; x < w; x++) {
r[yi] = dv[rsum];
g[yi] = dv[gsum];
b[yi] = dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (y == 0) {
vmin[x] = Math.min(x + radius + 1, wm);
}
p = pix[yw + vmin[x]];
sir[0] = (p & 0xff0000) >> 16;
sir[1] = (p & 0x00ff00) >> 8;
sir[2] = (p & 0x0000ff);
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[(stackpointer) % div];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi++;
}
yw += w;
}
for (x = 0; x < w; x++) {
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0;
yp = -radius * w;
for (i = -radius; i <= radius; i++) {
yi = Math.max(0, yp) + x;
sir = stack[i + radius];
sir[0] = r[yi];
sir[1] = g[yi];
sir[2] = b[yi];
rbs = r1 - Math.abs(i);
rsum += r[yi] * rbs;
gsum += g[yi] * rbs;
bsum += b[yi] * rbs;
if (i > 0) {
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
} else {
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
}
if (i < hm) {
yp += w;
}
}
yi = x;
stackpointer = radius;
for (y = 0; y < h; y++) {
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16)
| (dv[gsum] << 8) | dv[bsum];
rsum -= routsum;
gsum -= goutsum;
bsum -= boutsum;
stackstart = stackpointer - radius + div;
sir = stack[stackstart % div];
routsum -= sir[0];
goutsum -= sir[1];
boutsum -= sir[2];
if (x == 0) {
vmin[y] = Math.min(y + r1, hm) * w;
}
p = x + vmin[y];
sir[0] = r[p];
sir[1] = g[p];
sir[2] = b[p];
rinsum += sir[0];
ginsum += sir[1];
binsum += sir[2];
rsum += rinsum;
gsum += ginsum;
bsum += binsum;
stackpointer = (stackpointer + 1) % div;
sir = stack[stackpointer];
routsum += sir[0];
goutsum += sir[1];
boutsum += sir[2];
rinsum -= sir[0];
ginsum -= sir[1];
binsum -= sir[2];
yi += w;
}
}
bitmap.setPixels(pix, 0, w, 0, 0, w, h);
return (bitmap);
}
public static Drawable bitmap2Drawable(Bitmap bitmap) {
@SuppressWarnings("deprecation")
BitmapDrawable bd = new BitmapDrawable(bitmap);
Drawable d = (Drawable) bd;
return d;
}
}
......@@ -2,21 +2,30 @@
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:id="@+id/cVideoView">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
android:layout_height="match_parent"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<ImageView
android:id="@+id/iv_wait_loading_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="@android:color/white">
</ImageView>
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:background="@android:color/white"/>
<LinearLayout
android:id="@+id/wait_loading_layout"
......@@ -28,6 +37,7 @@
app:layout_constraintStart_toStartOf="parent"
android:gravity="center"
android:background="@color/white"
android:visibility="invisible"
android:orientation="vertical">
<LinearLayout
......
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