概述
在前面博学谷
项目的基础上进行改善和增加新功能
源码资源下载:https://download.csdn.net/download/hyh17808770899/19844673
博学谷
的第五个新功能:个人资料界面点击用户头像可以通过拍照和相册选择更换头像
1、个人资料界面添加更换头像的点击事件和头像显示代码
china.ynyx.heyunhui.activity
包中的UserInfoActivity.java
文件中添加代码
①58行:定义私有变量
private ImageView iv_head_icon;
private static String path = "/sdcard/DemoHead/";//sd路径
private Bitmap head;//头像Bitmap
private Context context = this;
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");//从Sd中找头像,转换成Bitmap
②78行:调动头像显示
imagehead();
③93行:头像显示代码
private void imagehead() {
// TODO Auto-generated method stub
if(bt!=null){
iv_head_icon.setImageBitmap(bt);
}else{
/**
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中
*/
iv_head_icon.setImageResource(R.drawable.default_icon);
}
rl_head.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ActionSheetDialog();
}
});
}
④120行:初始化控件
iv_head_icon =(ImageView) findViewById(R.id.iv_head_icon);
⑤268行:拍照和相册选择的点击事件:
//相机拍照后的返回结果
case 3:
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ "/head.jpg");
cropPhoto(Uri.fromFile(temp));//裁剪图片
}
break;
//从相册里面取相片的返回结果
case 4:
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());//裁剪图片
}
break;
//调用系统裁剪图片后
case 5:
if (data != null) {
Bundle extras = data.getExtras();
head = extras.getParcelable("data");
if (head != null) {
/**
* 上传服务器代码
*/
setPicToView(head);//保存在SD卡中
iv_head_icon.setImageBitmap(head);//用ImageView显示出来
}
}
break;
303行:
super.onActivityResult(requestCode, resultCode, data);
⑥308行:拍照、相册、系统裁剪的具体实现代码:
private void ActionSheetDialog() {
final String[] stringItems = {"拍照", "从相册中选择", "图片商城"};
final ActionSheetDialog dialog = new ActionSheetDialog(context, stringItems, null);
dialog.title("请选择头像更换方式")//
.titleTextSize_SP(14.5f)//
.show();
dialog.setOnOperItemClickL(new OnOperItemClickL() {
@Override
public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {//调用相机拍照
T.showShort(context, stringItems[position]);
//最好用try/catch包裹一下,防止因为用户未给应用程序开启相机权限,而使程序崩溃
try {
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//开启相机应用程序获取并返回图片(capture:俘获)
intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"head.jpg")));//指明存储图片或视频的地址URI
startActivityForResult(intent1, 3);//采用ForResult打开
} catch (Exception e) {
Toast.makeText(UserInfoActivity.this, "相机无法启动,请先开启相机权限", Toast.LENGTH_LONG).show();
}
dialog.dismiss();
}else if (position == 1) {//从相册里面取照片
T.showShort(context, stringItems[position]);
Intent intent2 = new Intent(Intent.ACTION_PICK, null);//返回被选中项的URI
intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");//得到所有图片的URI
startActivityForResult(intent2, 4);
dialog.dismiss();
}else if (position == 2) {
T.showShort(context, stringItems[position]);
Intent intent2 = new Intent(UserInfoActivity.this,PhotoWall.class);
startActivityForResult(intent2, 0);
dialog.dismiss();
}
}
});
}
/**
* 调用系统的裁剪
*
* @param uri
*/
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
//找到指定URI对应的资源图片
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是裁剪框宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);// 输出图片大小
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
//进入系统裁剪图片的界面
startActivityForResult(intent, 5);
}
private void setPicToView(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd卡是否可用
return;
}
FileOutputStream b = null;
File file = new File(path);
file.mkdirs();// 创建以此File对象为名(path)的文件夹
String fileName = path + "head.jpg";//图片名字
try {
b = new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件(compress:压缩)
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
//关闭流
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、“我”的界面头像显示
china.ynyx.heyunhui.view
包中的MyInfoView.java
文件中添加代码
①36行:定义
private static String path = "/sdcard/DemoHead/";//sd路径
②130行:设置“我”的界面头像显示,登陆成功后显示修改后的头像
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");//从Sd中找头像,转换成Bitmap
if(bt!=null){
iv_head_icon.setImageBitmap(bt);
}else{
/**
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中
*/
iv_head_icon.setImageResource(R.drawable.default_icon);
}
145行:没有登录则显示默认头像
iv_head_icon.setImageResource(R.drawable.default_icon);
3、修改配置文件
在AndroidManifest.xml包中的第14行添加如下代码:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
在AndroidManifest.xml包中更换App主题:
<application
中将android:theme
换成
android:theme="@style/Theme.AppCompat.NoActionBar"
4、弹窗逻辑代码
在src目录下新建china.ynyx.heyunhui.dialog
包,再在该包中新建更换头像时弹窗需要用到的java文件:
由于代码太多,这里就不一一展示了,需者自取:
https://yuyunyaohui.lanzoui.com/ilhz6qpnyve
添加库文件:更换头像弹窗所需要的库文件
https://yuyunyaohui.lanzoui.com/iKe6Xqpnole
5、弹窗样式
res目录下新建anim文件夹,在该文件夹中新建两个xml文件
dialog_enter.xml
具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- %p指相对于父容器 -->
<translate
android:duration="500"
android:fromYDelta="-100%p" />
</set>
dialog_exit.xml
具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="500"
android:toYDelta="-100%p" />
</set>
res目录下的layout文件夹中新建ac_dialog_home.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/elv"
android:background="#ffffff"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="@null"
android:fadingEdge="none"
android:groupIndicator="@null"
android:listSelector="#00000000"
android:scrollbars="none" />
res目录下的values文件夹中的styles.xml
中20行添加代码:
<style name="myDialogAnim" parent="android:Animation">
<!-- 进入时的动画 -->
<item name="@android:windowEnterAnimation">@anim/dialog_enter</item>
<!-- 退出时的动画 -->
<item name="@android:windowExitAnimation">@anim/dialog_exit</item>
</style>
UserInfoActivity.java
完整代码:
package china.ynyx.heyunhui.activity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import china.ynyx.heyunhui.PhotoWall;
import china.ynyx.heyunhui.R;
import china.ynyx.heyunhui.bean.UserBean;
import china.ynyx.heyunhui.dialog.ActionSheetDialog;
import china.ynyx.heyunhui.dialog.OnOperItemClickL;
import china.ynyx.heyunhui.dialog.T;
import china.ynyx.heyunhui.utils.AnalysisUtils;
import china.ynyx.heyunhui.utils.DBUtils;
public class UserInfoActivity extends AppCompatActivity implements OnClickListener{
private TextView tv_back;
private TextView tv_main_title;
private TextView tv_nickName, tv_signature, tv_user_name, tv_sex;
private RelativeLayout rl_nickName, rl_sex, rl_signature,
rl_title_bar,rl_head;
private String spUserName;
private static final int CHANGE_NICKNAME = 1;//修改昵称的自定义常量
private static final int CHANGE_SIGNATURE = 2;//修改个性签名的自定义常量
/*完成头像修改功能的代码---创新功能一*/
private ImageView iv_head_icon;
private static String path = "/sdcard/DemoHead/";//sd路径
private Bitmap head;//头像Bitmap
private Context context = this;
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");//从Sd中找头像,转换成Bitmap
/*完成头像修改功能的代码---创新功能一*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
//设置此界面为竖屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//从SharedPreferences中获取登录时的用户名
spUserName = AnalysisUtils.readLoginUserName(this);
init();
initData();
setListener();
imagehead();
}
/*头像显示代码*/
private void imagehead() {
// TODO Auto-generated method stub
if(bt!=null){
iv_head_icon.setImageBitmap(bt);
}else{
/**
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中
*/
iv_head_icon.setImageResource(R.drawable.default_icon);
}
rl_head.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ActionSheetDialog();
}
});
}
/*头像显示代码*/
/**
* 初始化控件
*/
private void init() {
tv_back = (TextView) findViewById(R.id.tv_back);
tv_main_title = (TextView) findViewById(R.id.tv_main_title);
tv_main_title.setText("个人资料");
rl_title_bar = (RelativeLayout) findViewById(R.id.title_bar);
rl_title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
rl_nickName = (RelativeLayout) findViewById(R.id.rl_nickName);
rl_sex = (RelativeLayout) findViewById(R.id.rl_sex);
rl_signature = (RelativeLayout) findViewById(R.id.rl_signature);
tv_nickName = (TextView) findViewById(R.id.tv_nickName);
tv_user_name = (TextView) findViewById(R.id.tv_user_name);
tv_sex = (TextView) findViewById(R.id.tv_sex);
tv_signature = (TextView) findViewById(R.id.tv_signature);
rl_head = (RelativeLayout) findViewById(R.id.rl_head);
/*完成头像修改功能的代码---创新功能一*/
iv_head_icon =(ImageView) findViewById(R.id.iv_head_icon);
/*完成头像修改功能的代码---创新功能一*/
}
/**
* 获取数据
*/
private void initData() {
UserBean bean = null;
bean = DBUtils.getInstance(this).getUserInfo(spUserName);
// 首先判断一下数据库是否有数据
if (bean == null) {
bean = new UserBean();
bean.userName=spUserName;
bean.nickName="雨云耀辉";
bean.sex="男";
bean.signature="雨云耀辉";
// bean.sign_in="无";
//保存用户信息到数据库
DBUtils.getInstance(this).saveUserInfo(bean);
}
setValue(bean);
}
/**
* 为界面控件设置值
*/
private void setValue(UserBean bean) {
tv_nickName.setText(bean.nickName);
tv_user_name.setText(bean.userName);
tv_sex.setText(bean.sex);
tv_signature.setText(bean.signature);
}
/**
* 设置控件的点击监听事件
*/
private void setListener() {
tv_back.setOnClickListener(this);
rl_nickName.setOnClickListener(this);
rl_sex.setOnClickListener(this);
rl_signature.setOnClickListener(this);
}
/**
* 控件的点击事件
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tv_back://返回键的点击事件
UserInfoActivity.this.finish();
break;
case R.id.rl_nickName://昵称的点击事件
String name = tv_nickName.getText().toString();//获取昵称控件上的数据
Bundle bdName = new Bundle();
bdName.putString("content", name);//传递界面上的昵称数据
bdName.putString("title", "昵称");
bdName.putInt("flag", 1);//flag传递1时表示是修改昵称
enterActivityForResult(ChangeUserInfoActivity.class,
CHANGE_NICKNAME, bdName);//跳转到个人资料修改界面
break;
case R.id.rl_sex://性别的点击事件
String sex = tv_sex.getText().toString();//获取性别控件上的数据
sexDialog(sex);
break;
case R.id.rl_signature://签名的点击事件
String signature = tv_signature.getText().toString();//获取签名控件上的数据
Bundle bdSignature = new Bundle();
bdSignature.putString("content", signature);//传递界面上的签名数据
bdSignature.putString("title", "签名");
bdSignature.putInt("flag", 2);//flag传递2时表示是修改签名
enterActivityForResult(ChangeUserInfoActivity.class,
CHANGE_SIGNATURE, bdSignature);//跳转到个人资料修改界面
break;
default:
break;
}
}
/**
* 设置性别的弹出框
*/
private void sexDialog(String sex){
int sexFlag=0;
if("男".equals(sex)){
sexFlag=0;
}else if("女".equals(sex)){
sexFlag=1;
}
final String items[]={"男","女"};
AlertDialog.Builder builder=new AlertDialog.Builder(this); //先得到构造器
builder.setTitle("性别"); //设置标题
builder.setSingleChoiceItems(items,sexFlag,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {//第二个参数是默认选中的哪个项
dialog.dismiss();
Toast.makeText(UserInfoActivity.this,items[which],Toast.LENGTH_SHORT).show();
setSex(items[which]);
}
});
builder.create().show();
}
/**
* 更新界面上的性别数据
*/
private void setSex(String sex){
tv_sex.setText(sex);
// 更新数据库中的性别字段
DBUtils.getInstance(UserInfoActivity.this).updateUserInfo("sex",
sex, spUserName);
}
/**
* 获取回传数据时需使用的跳转方法,
* 第一个参数to表示需要跳转到的界面,第二个参数requestCode表示一个请求码,第三个参数b表示跳转时传递的数据
*/
public void enterActivityForResult(Class<?> to, int requestCode, Bundle b) {
Intent i = new Intent(this, to);
i.putExtras(b);
startActivityForResult(i, requestCode);
}
/**
* 回传数据
*/
private String new_info;//最新数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CHANGE_NICKNAME://个人资料修改界面回传过来的昵称数据
if (data != null) {
new_info = data.getStringExtra("nickName");
if (TextUtils.isEmpty(new_info) || new_info == null) {
return;
}
tv_nickName.setText(new_info);
// 更新数据库中的昵称字段
DBUtils.getInstance(UserInfoActivity.this).updateUserInfo(
"nickName", new_info, spUserName);
}
break;
case CHANGE_SIGNATURE://个人资料修改界面回传过来的签名数据
if (data != null) {
new_info = data.getStringExtra("signature");
if (TextUtils.isEmpty(new_info) || new_info == null) {
return;
}
tv_signature.setText(new_info);
// 更新数据库中的签名字段
DBUtils.getInstance(UserInfoActivity.this).updateUserInfo(
"signature", new_info, spUserName);
}
break;
/*完成头像修改功能的代码---创新功能一*/
//相机拍照后的返回结果
case 3:
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ "/head.jpg");
cropPhoto(Uri.fromFile(temp));//裁剪图片
}
break;
//从相册里面取相片的返回结果
case 4:
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());//裁剪图片
}
break;
//调用系统裁剪图片后
case 5:
if (data != null) {
Bundle extras = data.getExtras();
head = extras.getParcelable("data");
if (head != null) {
/**
* 上传服务器代码
*/
setPicToView(head);//保存在SD卡中
iv_head_icon.setImageBitmap(head);//用ImageView显示出来
}
}
break;
/*完成头像修改功能的代码---创新功能一*/
default:
break;
}
/*完成头像修改功能的代码---创新功能一*/
super.onActivityResult(requestCode, resultCode, data);
/*完成头像修改功能的代码---创新功能一*/
};
/*完成头像修改功能的代码---创新功能一*/
private void ActionSheetDialog() {
final String[] stringItems = {"拍照", "从相册中选择", "图片商城"};
final ActionSheetDialog dialog = new ActionSheetDialog(context, stringItems, null);
dialog.title("请选择头像更换方式")//
.titleTextSize_SP(14.5f)//
.show();
dialog.setOnOperItemClickL(new OnOperItemClickL() {
@Override
public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {//调用相机拍照
T.showShort(context, stringItems[position]);
//最好用try/catch包裹一下,防止因为用户未给应用程序开启相机权限,而使程序崩溃
try {
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//开启相机应用程序获取并返回图片(capture:俘获)
intent1.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"head.jpg")));//指明存储图片或视频的地址URI
startActivityForResult(intent1, 3);//采用ForResult打开
} catch (Exception e) {
Toast.makeText(UserInfoActivity.this, "相机无法启动,请先开启相机权限", Toast.LENGTH_LONG).show();
}
dialog.dismiss();
}else if (position == 1) {//从相册里面取照片
T.showShort(context, stringItems[position]);
Intent intent2 = new Intent(Intent.ACTION_PICK, null);//返回被选中项的URI
intent2.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");//得到所有图片的URI
startActivityForResult(intent2, 4);
dialog.dismiss();
}else if (position == 2) {
T.showShort(context, stringItems[position]);
Intent intent2 = new Intent(UserInfoActivity.this,PhotoWall.class);
startActivityForResult(intent2, 0);
dialog.dismiss();
}
}
});
}
/**
* 调用系统的裁剪
*
* @param uri
*/
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
//找到指定URI对应的资源图片
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// aspectX aspectY 是裁剪框宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);// 输出图片大小
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
//进入系统裁剪图片的界面
startActivityForResult(intent, 5);
}
private void setPicToView(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd卡是否可用
return;
}
FileOutputStream b = null;
File file = new File(path);
file.mkdirs();// 创建以此File对象为名(path)的文件夹
String fileName = path + "head.jpg";//图片名字
try {
b = new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件(compress:压缩)
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
//关闭流
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*完成头像修改功能的代码---创新功能一*/
}
MyInfoView.java
完整代码:
package china.ynyx.heyunhui.view;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import china.ynyx.heyunhui.R;
import china.ynyx.heyunhui.activity.CheckActivity;
import china.ynyx.heyunhui.activity.LoginActivity;
import china.ynyx.heyunhui.activity.PlayHistoryActivity;
import china.ynyx.heyunhui.activity.SettingActivity;
import china.ynyx.heyunhui.activity.UserInfoActivity;
import china.ynyx.heyunhui.adapter.PlayHistoryAdapter;
import china.ynyx.heyunhui.utils.AnalysisUtils;
public class MyInfoView{
public ImageView iv_head_icon = null;
private LinearLayout ll_head;
private RelativeLayout rl_course_history,rl_setting;
private TextView tv_user_name,tv_sign_in;
private Activity mContext;
private LayoutInflater mInflater;
private View mCurrentView;
/*完成头像修改功能的代码---创新功能一*/
private static String path = "/sdcard/DemoHead/";//sd路径
/*完成头像修改功能的代码---创新功能一*/
public MyInfoView(Activity context){
mContext = context;
//为以后将Layout转换为view时用
mInflater = LayoutInflater.from(mContext);
}
private void createView(){
initView();
}
//获取界面控件
private void initView() {
// TODO Auto-generated method stub
//设置布局文件
mCurrentView = mInflater.inflate(R.layout.main_view_myinfo,null);
ll_head = (LinearLayout) mCurrentView.findViewById(R.id.ll_head);
iv_head_icon = (ImageView)mCurrentView.findViewById(R.id.iv_head_icon);
rl_course_history = (RelativeLayout)mCurrentView.findViewById(R.id.rl_course_history);
rl_setting = (RelativeLayout)mCurrentView.findViewById(R.id.rl_setting);
tv_user_name = (TextView) mCurrentView.findViewById(R.id.tv_user_name);
tv_sign_in = (TextView) mCurrentView.findViewById(R.id.tv_sign_in);
mCurrentView.setVisibility(View.VISIBLE);
setLoginParams(readLoginStatus());//设置登录时界面控件的状态
//签到的点击事件
tv_sign_in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断是否已经登录
if(readLoginStatus()){
//已登录跳转到签到界面
Intent intent = new Intent(mContext,CheckActivity.class);
mContext.startActivity(intent);
}else{
//未登录跳转到登录界面
Toast.makeText(mContext,"您还未登录,请先登录",Toast.LENGTH_SHORT).show();
}
}
});
//设置头像和用户名的点击事件
ll_head.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断是否已经登录
if(readLoginStatus()){
//已登录跳转到个人资料界面
Intent intent = new Intent(mContext,UserInfoActivity.class);
mContext.startActivity(intent);
}else{
//未登录跳转到登录界面
Intent intent = new Intent(mContext,LoginActivity.class);
mContext.startActivityForResult(intent,1);
}
}
});
//设置播放条目的点击事件
rl_course_history.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(readLoginStatus()){
//跳转到用户播放界面
Intent intent = new Intent(mContext,PlayHistoryActivity.class);
mContext.startActivityForResult(intent,1);
}else{
Toast.makeText(mContext,"您还未登录,请先登录",Toast.LENGTH_SHORT).show();
}
}
});
//设置的点击事件
rl_setting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(readLoginStatus()){
//跳转到设置界面
Intent intent = new Intent(mContext,SettingActivity.class);
mContext.startActivityForResult(intent,1);
}else{
Toast.makeText(mContext,"您还未登录,请先登录",Toast.LENGTH_SHORT).show();
}
}
});
}
//登录成功后设置我的界面
public void setLoginParams(boolean isLogin) {
if (isLogin) {
tv_user_name.setText(AnalysisUtils.readLoginUserName(mContext));
/*完成头像修改功能的代码---创新功能一*/
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");//从Sd中找头像,转换成Bitmap
if(bt!=null){
iv_head_icon.setImageBitmap(bt);
}else{
/**
* 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中
*/
iv_head_icon.setImageResource(R.drawable.default_icon);
}
/*完成头像修改功能的代码---创新功能一*/
} else {
tv_user_name.setText("点击登录");
/*完成头像修改功能的代码---创新功能一*/
iv_head_icon.setImageResource(R.drawable.default_icon);
/*完成头像修改功能的代码---创新功能一*/
}
}
//获取当前导航栏上方显示对应的View
public View getView(){
if (mCurrentView == null){
createView();
}
return mCurrentView;
}
//显示当前导航栏上方显示对应的View界面
public void showView() {
if(mCurrentView == null){
createView();
}
mCurrentView.setVisibility(View.VISIBLE);
}
//从SharedPreferences中读取登录状态
private boolean readLoginStatus(){
SharedPreferences sp=mContext.getSharedPreferences("loginInfo", Context.MODE_PRIVATE);
boolean isLogin=sp.getBoolean("isLogin",false);
return isLogin;
}
}
最后
以上就是重要火龙果为你收集整理的基于eclipse的android项目实战—博学谷(新功能五)更换头像的全部内容,希望文章能够帮你解决基于eclipse的android项目实战—博学谷(新功能五)更换头像所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复