个人信息页面
弹窗页面
图库选择之后
拍照之后
1.个人信息xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_back"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="9dp"
android:src="@drawable/back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="个人信息"
android:textColor="#080808"
android:textSize="24sp" />
<RelativeLayout
android:id="@+id/rl_head"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:text="头像"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:text="未设置"
android:textColor="@color/Qblue"
android:textSize="14dp" />
</LinearLayout>
<ImageView
android:id="@+id/iv_head"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/me" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@color/view_color" />
<RelativeLayout
android:id="@+id/rl_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:text="昵称"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:text="未设置"
android:textColor="@color/Qblue"
android:textSize="14dp" />
</LinearLayout>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/in" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="@color/view_color" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:text="我的等级"
android:textColor="@color/black"
android:textSize="16sp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/in" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="@color/view_color" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:text="我的积分"
android:textColor="@color/black"
android:textSize="16sp" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:src="@drawable/in" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="@color/view_color" />
</LinearLayout>
弹窗xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0"
android:orientation="vertical">
<TextView
android:id="@+id/tv_camera"
android:layout_width="match_parent"
android:layout_height="42dp"
android:background="@color/white"
android:gravity="center"
android:text="拍照"
android:textColor="@color/black"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="@color/view_color" />
<TextView
android:id="@+id/tv_chose"
android:layout_width="match_parent"
android:layout_height="42dp"
android:background="@color/white"
android:gravity="center"
android:text="从手机相册选择"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_cancle"
android:layout_width="match_parent"
android:layout_height="42dp"
android:layout_marginTop="8dp"
android:background="@color/white"
android:gravity="center"
android:text="取消"
android:textColor="@color/Qblue"
android:textSize="16sp" />
</LinearLayout>
2.java代码
package com.fb.hckjfb.activity;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.fb.hckjfb.R;
import com.fb.hckjfb.RoundTools;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 个人信息
*/
public class MeActivity extends BaseActivity implements View.OnClickListener {
private ImageView iv_back, iv_head;
private RelativeLayout rl_name, rl_head;
private int SELECT_PICTURE = 0x00;
private int SELECT_CAMER = 0x01;
private Bitmap bitmap;
private RoundTools roundTools;
private Dialog bottomDialog;
private TextView tv_tip, tv_name;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me);
initView();
//获取从上个页面传来的昵称并显示本页
Intent intent = getIntent();
String name = intent.getStringExtra("name");
}
@Override
protected int getLayoutID() {
return R.layout.activity_me;
}
@Override
protected void initListener() {
}
@Override
protected void initView() {
iv_back = findViewById(R.id.iv_back);
rl_name = findViewById(R.id.rl_name);
rl_head = findViewById(R.id.rl_head);
iv_head = findViewById(R.id.iv_head);
tv_tip = findViewById(R.id.tv_tip);
tv_name = findViewById(R.id.tv_name);
iv_back.setOnClickListener(this);
rl_name.setOnClickListener(this);
rl_head.setOnClickListener(this);
}
@Override
protected void initData() {
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.iv_back:
finish();
break;
case R.id.rl_name:
String name = tv_name.getText().toString();
intent = new Intent(this, SetNameActivity.class);
startActivity(intent);
break;
case R.id.rl_head:
//选择对话框
bottomDialog = new Dialog(this, R.style.BottomDialog);
View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_content_normal, null);
//获取Dialog的监听
TextView tv_camera = (TextView) contentView.findViewById(R.id.tv_camera);
TextView tv_chose = (TextView) contentView.findViewById(R.id.tv_chose);
TextView tv_cancle = (TextView) contentView.findViewById(R.id.tv_cancle);
tv_camera.setOnClickListener(this);
tv_chose.setOnClickListener(this);
tv_cancle.setOnClickListener(this);
bottomDialog.setContentView(contentView);
ViewGroup.LayoutParams layoutParams = contentView.getLayoutParams();
layoutParams.width = getResources().getDisplayMetrics().widthPixels;
contentView.setLayoutParams(layoutParams);
bottomDialog.getWindow().setGravity(Gravity.BOTTOM);//弹窗位置
bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);//弹窗样式
bottomDialog.show();//显示弹窗
break;
case R.id.tv_camera://自定义Doalog的点击事件
//通过相机拍摄照片
Intent intentCamer = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCamer, SELECT_CAMER);
bottomDialog.dismiss();//取消弹窗
//设置头像后提示字体不显示
tv_tip.setText("");
break;
case R.id.tv_chose:
//通过相册选择图片
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent, "选择图片"), SELECT_PICTURE);
bottomDialog.dismiss();//取消弹窗
//设置头像后提示字体不显示
tv_tip.setText("");
break;
case R.id.tv_cancle:
bottomDialog.dismiss(); //取消弹窗
break;
default:
break;
}
}
/**
* 返回结果处理
*
* @param requestCode 请求代码
* @param resultCode 结果代码
* @param data 返回数据
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE) {
Log.i(TAG, "相册");
handle(resultCode, data);
roundTools = new RoundTools();
Bitmap roundAlbum = roundTools.toRoundBitmap(bitmap);
iv_head.setImageBitmap(roundAlbum);
} else if (requestCode == SELECT_CAMER) {
Log.i(TAG, "相机");
if (data.getData() == null) {
bitmap = (Bitmap) data.getExtras().get("data");
Log.i(TAG, "BitData " + bitmap);
roundTools = new RoundTools();
Bitmap roundCamer = roundTools.toRoundBitmap(bitmap);
iv_head.setImageBitmap(roundCamer);
} else try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
if (bitmap != bitmap) {//主要是防止handle处理出错,就会将先前获取相册的照片show出来
iv_head.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 数据处理 共同点提取
*
* @param resultCode
* @param data
*/
private void handle(int resultCode, Intent data) {
if (resultCode == RESULT_OK) {//结果代码是Ok的
Uri uri = data.getData();
if (uri != null && data.getData() != null) {
Log.i(TAG, "uri 和 data.getData()不为空");
ContentResolver contentResolver = this.getContentResolver();
if (bitmap != null) {
bitmap.recycle();
}
try {
bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));//出错
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} else {
Log.i(TAG, "uri为空或者data为空 " + "数据:" + data.getData() + " uri: " + uri);
}
}
}
}
3.style弹窗样式
<!--底部弹窗样式-->
<style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
<style name="BottomDialog.Animation" parent="Animation.AppCompat.Dialog">
<item name="android:windowEnterAnimation">@anim/translate_dialog_in</item>
<item name="android:windowExitAnimation">@anim/translate_dialog_out</item>
</style>
anim进出效果
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="100%"
android:toXDelta="0"
android:toYDelta="0">
</translate>
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="100%">
</translate>
4.最后别忘记添加权限
<!-- 相机权限 -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- 读写SD卡权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />