VideoUI.java 3.87 KB
package com.sw.laryngoscope.camera;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sw.laryngoscope.R;
import com.sw.laryngoscope.activity.fragment.HomeFragment;
import com.sw.laryngoscope.manager.SpManager;
import com.sw.laryngoscope.utils.Logger;
import com.sw.laryngoscope.utils.TimeUtil;

public class VideoUI {

    private final HomeFragment fragment;
    private final View mRootView;

    TextView txt_video_time;
    //TextView txt_video_total_time;

    ImageView img_record_shortcut;
    ImageView img_record_flag;

    private boolean mRecordingStarted = false;

    public VideoUI(HomeFragment fragment, View parent) {
        this.fragment = fragment;
        mRootView = parent;

        txt_video_time = (TextView) fragment.getView().findViewById(R.id.txt_video_time);
        //txt_video_total_time = (TextView) fragment.getView().findViewById(R.id.txt_video_total_time);

        img_record_shortcut = (ImageView) fragment.getView().findViewById(R.id.img_record_shortcut);
        img_record_flag = (ImageView) fragment.getView().findViewById(R.id.img_record_flag);
    }

    public void setRecordingTime(String text, String total) {
        txt_video_time.setText(text);
        //txt_video_total_time.setText(total);

        Logger.d("========text=========" + text);
        if ( img_record_flag.getVisibility() == View.VISIBLE ) {
            img_record_flag.setVisibility(View.GONE);
        } else if ( !text.isEmpty() ) {
            img_record_flag.setVisibility(View.VISIBLE);
        }
    }

    public void showRecordingUIBack(boolean recording) {
        mRecordingStarted = recording;
        if (recording) {
            txt_video_time.setText("");
            txt_video_time.setVisibility(View.VISIBLE);
            img_record_shortcut.setSelected(true);
            //txt_video_total_time.setText("");
            //txt_video_total_time.setVisibility(View.VISIBLE);
        } else {
            txt_video_time.setText("");
            //txt_video_record.setVisibility(View.GONE);
            txt_video_time.setVisibility(View.GONE);
            img_record_flag.setVisibility(View.GONE);
            img_record_shortcut.setSelected(false);
            //txt_video_total_time.setText("");
        }
    }

    public SpannableString valueAndIcon(Context context, String str, int resId) {

        boolean isLeft = context.getResources().getConfiguration().locale.getLanguage().endsWith("ir");
        String res;
        if (isLeft) {
            res = "   " + str;
        } else {
            res = str + "   ";
        }
        SpannableString sp = new SpannableString(res);
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resId);
        Bitmap newBitmap = alterSizeBitmap(bitmap, 60, 60);

        if (newBitmap != null) {
            ImageSpan imageSpan = new ImageSpan(context, newBitmap);
            if (!isLeft) {
                sp.setSpan(imageSpan, res.length() - 1, res.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            } else {
                sp.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            }
        }
        return sp;
    }

    public Bitmap alterSizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {

        float scaleWidth = ((float) newWidth) / bitmap.getWidth();
        float scaleHeight = ((float) newHeight) / bitmap.getHeight();

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

}