Android 直播 聊天输入法弹出 视频不挤压 解决方案

参考网上已有方案,即使用DialogFragment布局聊天列表、聊天框、礼物等内容。

需要两个Fragment,底下Fragment放视频View,上层即DialogFragment。

注意,经过测试视频层必须放置在Fragment里边,若直接视频放置在Activity布局,达不到我们要的效果!!!

网上其他DialogFragment方案均说到直播Activity在清单中键盘弹出方式为 android:windowSoftInputMode="adjustPan" ,经过我的测试无需此设置

demo效果:

效果展示

完整demo代码:

AndroidManifest.xml

        <activity
            android:name=".fragment3.LiveVideo3Activity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
activity 容器 
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;

import cn.smiles.myapplication.R;

/**
 * activity 容器
 *
 * @author kaifang
 */
public class LiveVideo3Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_video3);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.hide();

    }
}

activity 布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment3.LiveVideo3Activity">

    <fragment
        android:id="@+id/fragment4"
        android:name="cn.smiles.myapplication.fragment3.VideoFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:tag="VideoFragment"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

视频显示层Fragment

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.smiles.myapplication.R;

/**
 * 底层视频显示
 *
 * @author kaifang
 */
public class VideoFragment extends Fragment {

    private View video;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_video, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        video = view.findViewById(R.id.iv_video);
        //在view布局完成后,显示上层聊天等弹窗的 DialogFragment
        video.post(new Runnable() {
            @Override
            public void run() {
                ChatFragment.newInstance("p").show(getChildFragmentManager(), "ChatFragment");
            }
        });
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public VideoFragment() {
        // Required empty public constructor
    }

}

视频层Fragment布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             tools:context=".fragment.VideoFragment">

    <ImageView
        android:id="@+id/iv_video"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@drawable/bg_video"/>

</FrameLayout>
聊天列表、聊天输入框、礼物框等显示DialogFragment
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spanned;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import cn.smiles.myapplication.R;

/**
 * 聊天列表、聊天输入框、礼物框等显示Fragment
 *
 * @author kaifang
 */
public class ChatFragment extends DialogFragment implements View.OnClickListener {
    private View layout_gift;
    private Context context;
    private RecyclerView rec_view;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //设置dialog无title
        Window window = getDialog().getWindow();
        if (window != null)
            window.requestFeature(Window.FEATURE_NO_TITLE);

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_chat, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        layout_gift = view.findViewById(R.id.layout_gift);
        view.findViewById(R.id.btn_gift).setOnClickListener(this);
        view.findViewById(R.id.btn_send).setOnClickListener(this);
        view.findViewById(R.id.click_hide).setOnClickListener(this);

        rec_view = view.findViewById(R.id.rec_view);
        initChatList();
    }

    @Override
    public void onStart() {
        super.onStart();
        //设置dialog显示时 按后退键后的操作
        Dialog dialog = getDialog();
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                    if (layout_gift.getVisibility() == View.VISIBLE) {
                        layout_gift.setVisibility(View.GONE);
                        return true;
                    } else {
                        ((Activity) context).finish();
                    }
                }
                return false;
            }
        });

        //设置dialog为透明背景、宽高填充屏幕
        Window window = dialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(android.R.color.transparent);

            WindowManager.LayoutParams windowParams = window.getAttributes();
            windowParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            windowParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            windowParams.dimAmount = 0.0f;
            window.setAttributes(windowParams);
        }
    }

    /**
     * 聊天列表
     */
    private void initChatList() {
        rec_view.setHasFixedSize(true);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rec_view.setLayoutManager(mLayoutManager);
        ArrayList<Spanned> myDataset = new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            if ((int) (Math.random() * 2) == 0) {
                Spanned spanned = Html.fromHtml("<font color='green'>小米:</font>" + UUID.randomUUID().toString());
                myDataset.add(spanned);
            } else {
                Spanned spanned = Html.fromHtml("<font color='green'>小花:</font>" + UUID.randomUUID().toString());
                myDataset.add(spanned);
            }
        }
        MyRecAdapter mAdapter = new MyRecAdapter(myDataset);
        rec_view.setAdapter(mAdapter);
    }

    /**
     * 列表适配器
     */
    public class MyRecAdapter extends RecyclerView.Adapter<MyRecAdapter.MyViewHolder> {
        private final List<Spanned> myDataset;

        MyRecAdapter(List<Spanned> myDataset) {
            this.myDataset = myDataset;
        }

        @NonNull
        @Override
        public MyRecAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item2, parent, false);
            return new MyRecAdapter.MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyRecAdapter.MyViewHolder holder, int position) {
            Spanned msg = myDataset.get(position);
            holder.mTextView.setText(msg);
        }

        @Override
        public int getItemCount() {
            return myDataset.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
            TextView mTextView;

            MyViewHolder(View v) {
                super(v);
                mTextView = v.findViewById(R.id.textView2);
                v.setOnClickListener(this);
            }

            @Override
            public void onClick(View v) {
                int position = rec_view.getChildLayoutPosition(v);
                Spanned spanned = myDataset.get(position);
                Toast.makeText(mTextView.getContext(), "点击位置 " + position + "\n" + spanned.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }

    public ChatFragment() {
        // Required empty public constructor
    }

    public static ChatFragment newInstance(String param1) {
        ChatFragment fragment = new ChatFragment();
        Bundle args = new Bundle();
        args.putString("param1", param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_send:
                Toast.makeText(context, "发送。。。", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_gift:
                hideKeyboardFrom(context);
                layout_gift.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layout_gift.setVisibility(View.VISIBLE);
                    }
                }, 300);
                break;
            case R.id.click_hide:
                hideKeyboardFrom(context);
                if (layout_gift.getVisibility() == View.VISIBLE)
                    layout_gift.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * 隐藏键盘
     *
     * @param context
     */
    public void hideKeyboardFrom(Context context) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        if (imm != null) {
            View view = getView();
            if (view != null) {
                imm.hideSoftInputFromWindow(view.getRootView().getWindowToken(), 0);
            }
        }
    }
}
DialogFragment层布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:background="@android:color/transparent"
             android:orientation="vertical"
             tools:context=".fragment.ChatInputFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <View
            android:id="@+id/click_hide"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"/>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rec_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
        </android.support.v7.widget.RecyclerView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:hint="输入聊天内容。。。"
                android:textColor="#FFF0"
                android:textColorHint="#00F">

                <requestFocus/>
            </EditText>

            <Button
                android:id="@+id/btn_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="发送"/>

            <TextView
                android:id="@+id/btn_gift"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="3dp"
                android:paddingRight="10dp"
                android:paddingBottom="3dp"
                android:text="礼物"
                android:textColor="#FF00FF"/>
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/layout_gift"
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:layout_gravity="bottom"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="礼物区域"
        android:visibility="gone"/>

</FrameLayout>

以上↑↑↑

最后附上demo中二次元人物图片

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一: 使用javacv来实现,最终也是用过ffmpeg来进行编码和推流,javacv实现到可以直接接收摄像头的帧数据 需要自己实现的代码只是打开摄像头,写一个SurfaceView进行预览,然后实现PreviewCallback将摄像头每一帧的数据交给javacv即可 javacv地址:https://github.com/bytedeco/javacv demo地址:https://github.com/beautifulSoup/RtmpRecoder/tree/master 二: 使用Android自带的编码工具,可实现硬编码,这里有一个国内大神开源的封装很完善的的库yasea,第一种方需要实现的Camera采集部分也一起封装好了,进行一些简单配置就可以实现编码推流,并且yasea目前已经直接支持摄像头的热切换,和各种滤镜效果 yasea地址(内置demo):https://github.com/begeekmyfriend/yasea 服务器 流媒体服务器我用的是srs,项目地址:https://github.com/ossrs/srs 关于srs的编译、配置、部署、在官方wiki中已经写的很详细了,并且srs同样是国内开发人员开源的项目,有全中文的文档,看起来很方便 这里有最基本的简单编译部署过程 Android直播实现(二)srs流媒体服务器部署 播放器 android端的播放使用vitamio,还是国内的开源播放器,是不是感觉国内的前辈们越来越屌了^~^! vitamio支持几乎所有常见的的视频格式和流媒体协议 vitamio地址(内置demo):https://github.com/yixia/VitamioBundle 这里使用的是yaesa库,先介绍一下直播实现的流程:
Android手机的输入时,可能有以下几个原因: 1. 输入未启用:首先要确保输入已经启用。可以在手机设置中的“语言和输入”或“键盘和输入”中查看所有安装的输入,并选择需要使用的输入。如果没有检测到要使用的输入,可以尝试下载并安装相应的输入应用程序。 2. 输入未被选中:在应用程序中,点击文本输入框时,可能需要手动选择要使用的输入。通过长按文本输入框,会选项菜单,选择“输入”并选择需要使用的输入。 3. 输入冲突:如果安装了多个输入,可能会输入冲突的情况,导致输入。可以尝试禁用除需要使用的输入外的其他输入,然后重新启动手机。 4. 软件冲突:某些应用程序可能与输入软件存在冲突,导致无输入。可以尝试卸载最近安装的应用程序,或在应用管理器中找到相关应用程序并清除其缓存数据。 5. 系统问题:偶尔,系统中的某些错误可能导致输入。可以尝试重启手机,以解决可能存在的临时问题。如果问题依然存在,可以考虑进行硬件重置或寻求专业技术支持。 总而言之,可以通过检查输入的启用状态、选中输入解决输入冲突、处理软件冲突、重启手机等方解决Android手机输入的问题。如果问题仍然无解决,建议寻求专业技术支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值