如何在当前活动中显示Canvas在android中的图像视图

时间:2015-04-20 11:59:47

标签: android

public class AndroidBarcodeView extends ImageView {

    public AndroidBarcodeView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        try {

            testQRCode(canvas);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private static void testQRCode(Canvas canvas) throws Exception {
        QRCode barcode = new QRCode();

        /*
         * QRCode Valid data char set: numeric data (digits 0 - 9); alphanumeric
         * data (digits 0 - 9; upper case letters A -Z; nine other characters:
         * space, $ % * + - . / : ); byte data (default: ISO/IEC 8859-1); Kanji
         * characters
         */
        // BIZCARD:N:Kelly;X:Goto;T:Design Ethnographer;C:gotomedia LLC;A:2169
        // Folsom Street
        // M302;B:4158647007;F:4158647004;M:4159907005;E:kelly@gotomedia.com;;
        // barcode.setData("BIZCARD:N:Kelly;X:Goto;T:Design Ethnographer;C:gotomedia LLC;A:2169 Folsom Street M302;B:4158647007;F:4158647004;M:4159907005;E:kelly@gotomedia.com;;");

        barcode.setData("StudentId:11;SectionId:A2;TimeStamp:20-04-2015 12:18PM;;");

        barcode.setDataMode(QRCode.M_AUTO);
        barcode.setVersion(1);
        barcode.setEcl(QRCode.ECL_L);

        // if you want to encode GS1 compatible QR Code, you need set FNC1 mode
        // to IBarcode.FNC1_ENABLE
        barcode.setFnc1Mode(IBarcode.FNC1_NONE);

        // Set the processTilde property to true, if you want use the tilde
        // character "~" to specify special characters in the input data.
        // Default is false.
        // 1-byte character: ~ddd (character value from 0 ~ 255)
        // ASCII (with EXT): from ~000 to ~255
        // 2-byte character: ~6ddddd (character value from 0 ~ 65535)
        // Unicode: from ~600000 to ~665535
        // ECI: from ~7000000 to ~7999999
        // SJIS: from ~9ddddd (Shift JIS 0x8140 ~ 0x9FFC and 0xE040 ~ 0xEBBF)
        barcode.setProcessTilde(false);

        // unit of measure for X, Y, LeftMargin, RightMargin, TopMargin,
        // BottomMargin
        barcode.setUom(IBarcode.UOM_PIXEL);
        // barcode module width in pixel
        barcode.setX(3f);

        barcode.setLeftMargin(50f);
        barcode.setRightMargin(50f);
        barcode.setTopMargin(50f);
        barcode.setBottomMargin(50f);
        // barcode image resolution in dpi
        barcode.setResolution(72);

        // barcode bar color and background color in Android device
        barcode.setForeColor(AndroidColor.black);
        barcode.setBackColor(AndroidColor.white);

        /*
         * specify your barcode drawing area
         */
        RectF bounds = new RectF(0, 0, 0, 0);
        barcode.drawBarcode(canvas, bounds);
    }

}

使用它可以生成条形码:

这是我的代码:

 Imageview img;
@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.textView3) {
            AndroidBarcodeView view = new AndroidBarcodeView(
                    StudentManinPage.this);
            setContentView(view);
        }

    }  

我必须在Imageview上显示条形码但是当我点击生成条形码然后它显示条形码但它没有显示在当前的动作imageview它显示在另一个视图请帮我在imageview上显示当前活动我有img imageview

1 个答案:

答案 0 :(得分:0)

我不会为你编写代码。这只是你问题的答案:

使用RectF bounds = new RectF(0, 0, 0, 0);

查找并替换RectF bounds = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
public class AndroidBarcodeView extends ImageView {
    //...
    private static void testQRCode(Canvas canvas) throws Exception {
      //...
      RectF bounds = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
      barcode.drawBarcode(canvas, bounds);
    }
}

public class StudentManinPage extends Activity {
  //...
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      AndroidBarcodeView bar_code_view = new AndroidBarcodeView(this);
      bar_code_view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

      setContentView(bar_code_view);
      //...
  }
  //...
}