<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" >
<include
android:id="@+id/layout_bar"
layout="@layout/layout_title" />
<LinearLayout
android: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" >
<RelativeLayout
android:id="@+id/view_user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" >
<ImageView
android:id="@+id/head"
android:layout_width="60.0dip"
android:layout_height="60.0dip"
android:layout_margin="10dp"
android:src="@drawable/head" />
<TextView
android: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" />
<TextView
android: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" />
<ImageView
android: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>
<ImageView
android: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" />
<TextView
style="@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;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_mycode);
super.onCreate(savedInstanceState);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.img_back:
Utils.finish(MyCodeActivity.this);
break;
default:
break;
}
}
@Override
protected 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);
}
@Override
protected void initView() {
img_back.setOnClickListener(this);
}
@Override
protected 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("JUNS_WeChat@User:" + id);
ImageView imageView = (ImageView) findViewById(R.id.img_code);
imageView.setImageBitmap(qrcode);
}
@Override
protected 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。