<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#2E3132" > <includeandroid:id="@+id/layout_bar"layout="@layout/layout_title" /> <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_margin="30dp"android:background="@drawable/btn_bg_gray"android:orientation="vertical"android:paddingBottom="20dp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="10dp" > <RelativeLayoutandroid:id="@+id/view_user"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp" > <ImageViewandroid:id="@+id/head"android:layout_width="60.0dip"android:layout_height="60.0dip"android:layout_margin="10dp"android:src="@drawable/head" /> <TextViewandroid:id="@+id/tvname"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="17dp"android:layout_toRightOf="@+id/head"android:text="菲儿"android:textSize="18.0dip" /> <TextViewandroid:id="@+id/tvmsg"style="@style/txt_ContextStyle1"android:layout_below="@+id/tvname"android:layout_marginTop="5dp"android:layout_toRightOf="@+id/head"android:text="微信号:dada20150625"android:textSize="14.0dip" /> <ImageViewandroid:id="@+id/iv_sex"android:layout_toRightOf="@+id/tvname"android:layout_width="20dp"android:layout_height="20dp"android:layout_marginTop="22dp"android:layout_marginLeft="5dp"android:src="@drawable/ic_sex_female" /> </RelativeLayout> <ImageViewandroid:id="@+id/img_code"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginTop="10dp"android:contentDescription="@string/app_name"android:scaleType="centerCrop"android:src="@drawable/default_image" /> <TextViewstyle="@style/txt_ContextStyle1"android:layout_gravity="center"android:layout_marginTop="20dp"android:text="扫一扫上面的二维码图案,加我微信"android:textSize="14.0dip" /> </LinearLayout> </RelativeLayout>
实现代码:
public class MyCodeActivity extends BaseActivity implements View.OnClickListener { private TextView txt_title, txt_right, tvname, tv_accout;private ImageView img_back; @Overridepublic void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_mycode);super.onCreate(savedInstanceState); } @Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.img_back: Utils.finish(MyCodeActivity.this);break;default:break; } } @Overrideprotected void initControl() {txt_title = (TextView) findViewById(R.id.txt_title);txt_title.setText("我的二维码");img_back = (ImageView) findViewById(R.id.img_back);img_back.setVisibility(View.VISIBLE);tvname = (TextView) findViewById(R.id.tvname);tv_accout = (TextView) findViewById(R.id.tvmsg); } @Overrideprotected void initView() {img_back.setOnClickListener(this); } @Overrideprotected void initData() { String id = Utils.getValue(this, Constants.User_ID);tv_accout.setText("微信号:" + id); User user = GloableParams.Users.get(id);if (user != null && !TextUtils.isEmpty(user.getUserName()))tvname.setText(user.getUserName()); Bitmap qrcode = generateQRCode("[email protected]:" + id); ImageView imageView = (ImageView) findViewById(R.id.img_code); imageView.setImageBitmap(qrcode); } @Overrideprotected void setListener() {// TODO Auto-generated method stub} private Bitmap generateQRCode(String content) {try { QRCodeWriter writer = new QRCodeWriter();// MultiFormatWriter writer = new MultiFormatWriter();BitMatrix matrix = writer.encode(content, BarcodeFormat.QR_CODE, 500, 500);return bitMatrix2Bitmap(matrix); } catch (WriterException e) { e.printStackTrace(); }return null; } private Bitmap bitMatrix2Bitmap(BitMatrix matrix) {int w = matrix.getWidth();int h = matrix.getHeight();int[] rawData = new int[w * h];for (int i = 0; i < w; i++) {for (int j = 0; j < h; j++) {int color = Color.WHITE;if (matrix.get(i, j)) { color = Color.BLACK; } rawData[i + (j * w)] = color; } } Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565); bitmap.setPixels(rawData, 0, w, 0, 0, w, h);return bitmap; }}
生成二维码的函数generateQRCode可以重用,使用了zxing的QRCodeWriter。
时间: 2024-09-28 01:12:36