无法在Android中更正图像位图图层的顺序

时间:2018-04-10 22:39:10

标签: java android opencv opencv4android

我有一段代码与图像进行比较,并在差异上放置一个标记。到目前为止它运作良好,除了添加的最新标记图层总是显示在所有旧标记下面。我有一个最新的黄色和旧的红色。当差异接近其中一个红色标记时,黄色标记显示在这些标记后面。

是否有人可以帮助我让黄色(最新标记)出现在顶部?

到目前为止,这是我的代码:

public class CheckmarkActivity extends AppCompatActivity implements ZoomLayout.OnZoomableLayoutClickEventListener  {

    TextView tv;
    RelativeLayout relativeLayout_work;
    ImageView imageViewtest;
    Bitmap prevBmp = null;
    Timer t;
    TimerTask task;
    int time = 100;

    float image_Width;
    float image_Height;

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

        setContentView(R.layout.activity_checkmark);
        if (getResources().getBoolean(R.bool.is_tablet)) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }

        tv = findViewById(R.id.tv);

        relativeLayout_work = findViewById(R.id.relativeLayout_work);
        imageViewtest = findViewById(R.id.imageViewtest);

        prevBmp = ViewcontrollerActivity.workSession.getLastScreenShot();

        if (prevBmp == null || ViewcontrollerActivity.workSession.workScreenShot == null) {
            setResult(Activity.RESULT_CANCELED);
            finish();
        }

        startTimer();

   }

   // image compare
   class TestAsync extends AsyncTask<Object, Integer, String>
   {
       String TAG = getClass().getSimpleName();
       PointF p;
       Bitmap test_3;
       protected void onPreExecute (){
           super.onPreExecute();
           Log.d(TAG + " PreExceute","On pre Exceute......");
       }

       protected String doInBackground(Object...arg0) {

           test_3 = ImageHelper.findDifference(CheckmarkActivity.this, prevBmp, ViewcontrollerActivity.workSession.workScreenShot);

           p = ImageHelper.findShot(test_3);
           time = 1;

           return "You are at PostExecute";
       }

       protected void onProgressUpdate(Integer...a){
           super.onProgressUpdate(a);
       }

       protected void onPostExecute(String result) {
           super.onPostExecute(result);
           addImageToImageview();

           PointF np = Session.convertPointBitmap2View(p, relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
           tv.setX(np.x - tv.getWidth() / 2);
           tv.setY(np.y - tv.getHeight() / 2);

           tv.setVisibility(View.VISIBLE);

//           imageViewtest.setImageBitmap(test_3);
       }
   }

    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        @Override
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                {
                    Log.i("OpenCV", "OpenCV loaded successfully");
                    new TestAsync().execute();
                } break;
                default:
                {
                    super.onManagerConnected(status);
                } break;
            }
        }
    };


    @Override
    protected void onResume() {
        super.onResume();
        if (!OpenCVLoader.initDebug()) {
            Log.d("OpenCV", "Internal OpenCV library not found. Using OpenCV Manager for initialization");
            OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, this, mLoaderCallback);
        } else {
            Log.d("OpenCV", "OpenCV library found inside package. Using it!");
            mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
        }
    }


    public static int[] getBitmapOffset(ImageView img,  Boolean includeLayout) {
        int[] offset = new int[2];
        float[] values = new float[9];

        Matrix m = img.getImageMatrix();
        m.getValues(values);

        offset[0] = (int) values[5];
        offset[1] = (int) values[2];

        if (includeLayout) {
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) img.getLayoutParams();
            int paddingTop = (int) (img.getPaddingTop() );
            int paddingLeft = (int) (img.getPaddingLeft() );

            offset[0] += paddingTop + lp.topMargin;
            offset[1] += paddingLeft + lp.leftMargin;
        }
        return offset;
    }

    public static int[] getBitmapPositionInsideImageView(ImageView imageView) {
        int[] ret = new int[4];

        if (imageView == null || imageView.getDrawable() == null)
            return ret;

        // Get image dimensions
        // Get image matrix values and place them in an array
        float[] f = new float[9];
        imageView.getImageMatrix().getValues(f);

        // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
        final float scaleX = f[Matrix.MSCALE_X];
        final float scaleY = f[Matrix.MSCALE_Y];

        // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
        final Drawable d = imageView.getDrawable();
        final int origW = d.getIntrinsicWidth();
        final int origH = d.getIntrinsicHeight();

        // Calculate the actual dimensions
        final int actW = Math.round(origW * scaleX);
        final int actH = Math.round(origH * scaleY);

        ret[2] = actW;
        ret[3] = actH;

        // Get image position
        // We assume that the image is centered into ImageView
        int imgViewW = imageView.getWidth();
        int imgViewH = imageView.getHeight();

        int top = (int) (imgViewH - actH)/2;
        int left = (int) (imgViewW - actW)/2;

        ret[0] = left;
        ret[1] = top;

        return ret;
    }
    private void addImageToImageview(){

        if (ViewcontrollerActivity.workSession.workScreenShot != null) {

            imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
            Log.d("width", String.valueOf(imageViewtest.getWidth()));
        }

        Resources r = getResources();
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());

        for (int i = 0; i < ViewcontrollerActivity.workSession.getShotCount(); i++) {
            PointF p = ViewcontrollerActivity.workSession.getPoint(i);
            TextView t = new TextView(this);
            t.setText("" + (i + 1));
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams((int)px, (int)px);
            relativeLayout_work.addView(t);
            t.setLayoutParams(param);
            t.setGravity(Gravity.CENTER);
            t.setBackgroundResource(R.drawable.circle);
            p = Session.convertPointBitmap2View(p, relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
            t.setX(p.x);
            t.setY(p.y);
            t.setTag(10000 + i);
        }
    }

    public void onConfirm(View v){
        View vv = findViewById(R.id.relativeLayout_work);
        PointF bp = Session.convertPointView2Bitmap(new PointF(tv.getX(), tv.getY()), relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
        ViewcontrollerActivity.workSession.addNewShot(ViewcontrollerActivity.workSession.workScreenShot, bp);
        setResult(Activity.RESULT_OK);
        finish();
    }

    public void onCancel(View v){
        setResult(Activity.RESULT_CANCELED);
        finish();
    }

    @Override
    public void onBackPressed() {

        setResult(Activity.RESULT_CANCELED);

        finish();

    }


    @Override
    public void OnContentClickEvent(int action, float xR, float yR) {
        int[] offset = new int[2];
        int[] rect = new int[4];
        offset = this.getBitmapOffset(imageViewtest, false);
        int original_width = imageViewtest.getDrawable().getIntrinsicWidth();
        int original_height = imageViewtest.getDrawable().getIntrinsicHeight();

        rect = getBitmapPositionInsideImageView(imageViewtest);
        Log.i("OffsetY", String.valueOf(offset[0]));
        Log.i("OffsetX", String.valueOf(offset[1]));
        Log.i( "0", String.valueOf(rect[0]));
        Log.i( "1", String.valueOf(rect[1]));
        Log.i( "2", String.valueOf(rect[2]));
        Log.i( "3", String.valueOf(rect[3]));

        if (xR > rect[0] && xR < rect[0] + rect[2] && yR > rect[1] && yR < rect[1] + rect[3]) {
            tv.setX(xR - tv.getWidth() / 2);
            tv.setY(yR - tv.getHeight() / 2);
        }


//        tv.setX(xR - tv.getWidth() / 2);
//        tv.setY(yR - tv.getHeight() / 2);

    }

    public void onMoveButtonPressed(View v) {
        ImageButton b = (ImageButton)v;

        int mId = b.getId();

        switch (mId) {
            case R.id.imageButtonL:
                tv.setX(tv.getX() - 1);
                break;
            case R.id.imageButtonR:
                tv.setX(tv.getX() + 1);
                break;
            case R.id.imageButtonD:
                tv.setY(tv.getY() + 1);
                break;
            case R.id.imageButtonU:
                tv.setY(tv.getY() - 1);
                break;
        }

    }

    //timer change image
    public void startTimer(){
        t = new Timer();
        task = new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        if (time == 1){
                            imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
//                            tv.setVisibility(View.VISIBLE);
                            tv.setText("" + (ViewcontrollerActivity.workSession.getShotCount() + 1));
                            t.cancel();
                            return;
                        }
                        if (time % 2 == 0) {
                            imageViewtest.setImageBitmap(prevBmp);
                        }
                        else if(time % 2 == 1){
                           imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
                        }
                        time --;
                    }
                });
            }
        };
        t.scheduleAtFixedRate(task, 0, 500);
    }

}

1 个答案:

答案 0 :(得分:0)

您可以使用addView()函数给出子视图的z顺序。

void addView (View child, int index)

离)

private void addImageToImageview(){

    if (ViewcontrollerActivity.workSession.workScreenShot != null) {

        imageViewtest.setImageBitmap(ViewcontrollerActivity.workSession.workScreenShot);
        Log.d("width", String.valueOf(imageViewtest.getWidth()));
    }

    Resources r = getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, r.getDisplayMetrics());

    int currChildrenCount = relativeLayout_work.getChildCount();

    for (int i = 0; i < ViewcontrollerActivity.workSession.getShotCount(); i++) {
        PointF p = ViewcontrollerActivity.workSession.getPoint(i);
        TextView t = new TextView(this);
        t.setText("" + (i + 1));
        RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams((int)px, (int)px);
        relativeLayout_work.addView(t, currChildrenCount+i);   // You can control the order like this
        t.setLayoutParams(param);
        t.setGravity(Gravity.CENTER);
        t.setBackgroundResource(R.drawable.circle);
        p = Session.convertPointBitmap2View(p, relativeLayout_work, ViewcontrollerActivity.workSession.workScreenShot);
        t.setX(p.x);
        t.setY(p.y);
        t.setTag(10000 + i);
    }
}