您的位置:首页 > 移动开发 > Android开发

Android头像选择器

2016-12-29 10:12 309 查看

    之前的项目中有做过头像上传的功能,这次又有用到,就整理一下。

public class MainActivity extends Activity {
private ImageView ivHead;
private Bitmap head;
private static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZKT/";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initDialog();
}

private void initView() {
ivHead = (ImageView) findViewById(R.id.imageView1);
Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");
if (bt != null) {
Drawable drawable = new BitmapDrawable(bt);
ivHead.setImageDrawable(drawable);
} else {
/**
*从服务器获取
*
*/
}
}

private void initDialog() {
final View view = getLayoutInflater().inflate(R.layout.selectdialog,
null);
final Dialog dialog = new Dialog(MainActivity.this);
//        取消dialog的title栏
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
ivHead.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
dialog.setContentView(view, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
Window window = dialog.getWindow();
window.setWindowAnimations(R.style.anim);
WindowManager.LayoutParams wl = window.getAttributes();
wl.x = 0;
wl.y = getWindowManager().getDefaultDisplay().getHeight();
wl.width = LayoutParams.MATCH_PARENT;
wl.height = LayoutParams.WRAP_CONTENT;
dialog.onWindowAttributesChanged(wl);
dialog.setCanceledOnTouchOutside(true);
view.findViewById(R.id.btn_photochoose_cancle).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
view.findViewById(R.id.btn_photochoose_photobox).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(Intent.ACTION_PICK, null);
intent1.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent1, 1);
dialog.dismiss();
}
});

view.findViewById(R.id.btn_photochoose_take).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new <
f235
/span>File(Environment
.getExternalStorageDirectory(), "head.jpg")));
startActivityForResult(intent2, 2);
dialog.dismiss();
}
});
dialog.show();

}
});

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
cropPhoto(data.getData());
}
break;
case 2:
if (resultCode == RESULT_OK) {
File temp = new File(Environment.getExternalStorageDirectory()
+ "/head.jpg");
cropPhoto(Uri.fromFile(temp));
}
break;
case 3:
if (data != null) {
Bundle extras = data.getExtras();
head = extras.getParcelable("data");
if (head != null) {
/**
* 上传到服务器
*/
saveImage(head);
//                        设置给imageview
ivHead.setImageBitmap(head);
}
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

//裁剪图片
public void cropPhoto(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}

//保存图片到本地
private void saveImage(Bitmap mBitmap) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
return;
}
FileOutputStream b = null;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
String fileName = path + "head.jpg";
try {
b = new FileOutputStream(fileName);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


这是弹出,弹回dialog的动画代码:

  

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="1000"
android:toXDelta="0"
android:toYDelta="0" />

</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:toYDelta="1000" />

</set>


这是头像选择的dialog的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:gravity="bottom"
android:orientation="vertical"
>
<Button
android:id="@+id/btn_photochoose_photobox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingBottom="10dip"
android:paddingTop="10dip"
android:text="图库"
android:textSize="16sp" />

<TextView
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:background="#DAD9DB" />

<Button
android:id="@+id/btn_photochoose_take"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingBottom="10dip"
android:paddingTop="10dip"
android:text="拍照"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="0.5dip"
android:background="#DAD9DB" />
<Button
android:id="@+id/btn_photochoose_cancle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:paddingBottom="10dip"
android:paddingTop="10dip"
android:text="取消"
android:textSize="16sp" />

</LinearLayout>


Mainactivity的布局很简单,只有一个ImageView。希望能对初学者有帮助

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: