package kln.android.shapedetection.fragments;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import org.opencv.android.Utils;
import org.opencv.core.Mat;

import java.io.File;

import kln.android.shapedetection.R;

/**
 * Created by kln on 6/24/16.
 */
public abstract class BaseFragment extends Fragment{

    private TextView mTitle = null;
    private ImageView mImageView = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        mTitle = (TextView) rootView.findViewById(R.id.section_label);
        mTitle.setText(getString(R.string.fragment_title_default));
        mImageView = (ImageView) rootView.findViewById(R.id.imageView);
        return rootView;
    }

    protected void setTitle(final String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }

    public void setImage(final String imagePath) {
        if (mImageView != null) {
            mImageView.setImageURI(Uri.fromFile(new File(imagePath)));
        }
    }

    public void setImage(final Uri uri) {
        if (mImageView != null) {
            mImageView.setImageURI(uri);
        }
    }

    public void showMatImage(final Mat mat) {
        //convert to bitmap & show image
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Bitmap bmp = Bitmap.createBitmap(mat.width(), mat.height(), Bitmap.Config.ARGB_8888);
                Utils.matToBitmap(mat, bmp, true);
                mImageView.setImageBitmap(bmp);
            }
        });
    }

    protected abstract void show();
}

@@ -162,16 +162,41 @@
 agment;%0A
+import android.util.Log;%0A
 import a
@@ -636,16 +636,45 @@
  = null;
+%0A    private Mat mMat = null;
 %0A%0A    @O
@@ -1190,23 +1190,270 @@
     
-return rootView
+Log.d(%22KLN%22, %22onViewCreated....%22);%0A%0A        return rootView;%0A    %7D%0A%0A    @Override%0A    public void onViewCreated(View view,%0A                              Bundle savedInstanceState) %7B%0A        super.onViewCreated(view, savedInstanceState);%0A        displayMatImage()
 ;%0A  
@@ -1954,40 +1954,19 @@
     
-//convert to bitmap & show image
+mMat = mat;
 %0A   
@@ -2065,32 +2065,230 @@
 oid run() %7B%0A    
+            displayMatImage();%0A                show();%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void displayMatImage() %7B%0A        if (mMat != null) %7B%0A            //convert to bitmap & show image%0A
             Bitm
@@ -2317,16 +2317,17 @@
 Bitmap(m
+M
 at.width
@@ -2331,16 +2331,17 @@
 dth(), m
+M
 at.heigh
@@ -2371,20 +2371,16 @@
 _8888);%0A
-    
         
@@ -2402,16 +2402,17 @@
 Bitmap(m
+M
 at, bmp,
@@ -2415,28 +2415,24 @@
 bmp, true);%0A
-    
             
@@ -2471,33 +2471,17 @@
         
-  
-  %7D%0A        %7D);
+%7D
 %0A    %7D%0A%0A

fix fragment image redraw on viewCreate