zxing片段不起作用

时间:2014-08-26 12:30:35

标签: android fragment zxing

我正在尝试在SimpleStretchedActivity项目中自定义zxingfragmentlib-master

以下是activity_stretchd_sample.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

<fragment
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:name="com.welcu.android.zxingfragmentlibsample.SampleFragment"
    android:id="@+id/scanner_fragment"
    android:layout_gravity="center"
    tools:layout="@layout/capture"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="0dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp"
    />

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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="350dp"
        android:id="@+id/layout_content"
        android:background="#FFF"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Fragment"
            android:id="@+id/textView"
            android:textColor="#000"/>
    </LinearLayout>
</LinearLayout>

以下是SampleStretchedActivity.java

   package com.welcu.android.zxingfragmentlibsample;

import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;

import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.Display;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.welcu.android.zxingfragmentlib.BarCodeScannerFragment;

/**
 * Created by joyarzun on 4/8/14.
 */
public class SampleStretchedActivity extends Activity {
  boolean torchState = false;

  Button mToggleButton;
  LinearLayout layoutContent;
  BarCodeScannerFragment mScannerFragment;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(com.welcu.android.zxingfragmentlibsample.R.layout.activity_stretched_sample);

    FragmentManager fm = getFragmentManager();
    mScannerFragment = (BarCodeScannerFragment) fm.findFragmentById(R.id.scanner_fragment);

    layoutContent = (LinearLayout) findViewById(R.id.layout_content);

    final ViewTreeObserver observer = layoutContent.getViewTreeObserver();

    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        // We're assuming that the other layout is under the scanner
        int activityWidth = layoutContent.getWidth();
        int activityHeight = findViewById(R.id.scanner_fragment).getHeight();

        int usableWidth = layoutContent.getWidth();
        int usableHeight = activityHeight - layoutContent.getHeight();

        int desiredHeight = (int) (usableHeight * 0.8);
        int desiredWidth = (int) (usableWidth * 0.75);

        Rect framingRect = new Rect(
            (usableWidth - desiredWidth) / 2, // left
            (usableHeight - desiredHeight) / 2, // top
            (usableWidth - desiredWidth) / 2 + desiredWidth, // right
            (usableHeight - desiredHeight) / 2 + desiredHeight// bottom
        );
        Log.v("RECT", "left: " + framingRect.left + " top: " + framingRect.top + " right: " + framingRect.right + " bottom: " + framingRect.bottom + " activityHeight: " + activityHeight + " activitiWidth: " + activityWidth);
        mScannerFragment.setFramingRect(framingRect);
      }
    });

//    mToggleButton = (Button) findViewById(R.id.button_flash);
//    mToggleButton.setOnClickListener(createToggleFlashListener());

  }

  private View.OnClickListener createToggleFlashListener() {
    return new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        torchState = !torchState;
        mScannerFragment.setTorch(torchState);
      }
    };
  }
}

以下是SimpleFragment.java

package com.welcu.android.zxingfragmentlibsample;

import android.os.Bundle;
import android.widget.Toast;

import com.google.zxing.Result;
import com.welcu.android.zxingfragmentlib.BarCodeScannerFragment;

/**
 * Created by mito on 9/17/13.
 */
public class SampleFragment extends BarCodeScannerFragment {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setmCallBack(new IResultCallback() {
            @Override
            public void result(Result lastResult) {
                Toast.makeText(getActivity(), "Scan: " + lastResult.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public SampleFragment() {

    }
}

我唯一的改变是我已将内部LinearLayout中的layout_height从227dp更改为350dp。原始代码工作正常但是这个小小的改变我的片段无法扫描任何条形码并烘烤它。

有什么问题?我真的需要你的帮助

谢谢

0 个答案:

没有答案