Android 頭像上傳

首先,大家要了解頭像上傳需要怎么做。第一。我們要選擇圖片或者從照相機照一張。第二,需要對頭像圖片進行處理,第三就是要把頭像顯示到ImageButton上了。

1.布局文件

<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"

? ? tools:context=".MainActivity" >

? ? ? ? android:id="@+id/iv_head"

? ? ? ? android:layout_width="120dp"

? ? ? ? android:layout_height="120dp"

? ? ? ? android:layout_centerHorizontal="true"

? ? ? ? android:layout_marginTop="10dp"

? ? ? ? android:background="@null"

? ? ? ? android:scaleType="fitXY"

? ? ? ? android:src="@drawable/ic_launcher_background" />

</RelativeLayout>

2.MainActivity

public class MainActivityextends AppCompatActivityimplements View.OnClickListener {

private ImageButtonivHead;

private Buttonbtn_picture,btn_photo,btn_cancle;

private Bitmaphead;// 頭像Bitmap

? ? @SuppressLint("SdCardPath")

private static Stringpath ="/sdcard/myHead/";// sd路徑

? ? protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ivHead = (ImageButton) findViewById(R.id.iv_head);

ivHead.setOnClickListener(this);

requestWindowFeature(Window.FEATURE_NO_TITLE);

Bitmap bt = BitmapFactory.decodeFile(path +"head.jpg");// 從Sd中找頭像,轉(zhuǎn)換成Bitmap

? ? ? ? if (bt !=null) {

@SuppressWarnings("deprecation")

Drawable drawable =new BitmapDrawable(toRoundBitmap(bt));// 轉(zhuǎn)換成drawable

? ? ? ? ? ? ivHead.setImageDrawable(drawable);

}else {

/**

* 如果SD里面沒有則需要從服務(wù)器取頭像,取回來的頭像再保存在SD中

*

*/

? ? ? ? }

}

public void onClick(View v) {

showDialog();

}

private void showDialog() {

View view = getLayoutInflater().inflate(R.layout.photo_choose_dialog,null);

final Dialog dialog =new Dialog(this, R.style.transparentFrameWindowStyle);

dialog.setContentView(view,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Window window = dialog.getWindow();

// 設(shè)置顯示動畫

? ? ? ? window.setWindowAnimations(R.style.main_menu_animstyle);

WindowManager.LayoutParams wl = window.getAttributes();

wl.x =0;

wl.y = getWindowManager().getDefaultDisplay().getHeight();

// 以下這兩句是為了保證按鈕可以水平滿屏

? ? ? ? wl.width = ViewGroup.LayoutParams.MATCH_PARENT;

wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;

// 設(shè)置顯示位置

? ? ? ? dialog.onWindowAttributesChanged(wl);

// 設(shè)置點擊外圍解散

? ? ? ? dialog.setCanceledOnTouchOutside(true);

dialog.show();

btn_picture = (Button) window.findViewById(R.id.btn_picture);

btn_photo = (Button) window.findViewById(R.id.btn_photo);

btn_cancle = (Button) window.findViewById(R.id.btn_cancle);

btn_picture.setOnClickListener(new View.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();

}

});

btn_photo.setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View v) {

Intent intent2 =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"head.jpg")));

startActivityForResult(intent2,2);// 采用ForResult打開

? ? ? ? ? ? ? ? dialog.dismiss();

}

});

btn_cancle.setOnClickListener(new View.OnClickListener() {

@Override

? ? ? ? ? ? public void onClick(View v) {

dialog.dismiss();

}

});

}

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) {

/**

* 上傳服務(wù)器代碼

*/

? ? ? ? ? ? ? ? ? ? ? ? setPicToView(head);// 保存在SD卡中

? ? ? ? ? ? ? ? ? ? ? ? ivHead.setImageBitmap(toRoundBitmap(head));// 用ImageView顯示出來

? ? ? ? ? ? ? ? ? ? }

}

break;

default:

break;

}

super.onActivityResult(requestCode, resultCode, data);

};

/**

* 調(diào)用系統(tǒng)的裁剪

*

? ? * @param uri

? ? */

? ? public void cropPhoto(Uri uri) {

Intent intent =new Intent("com.android.camera.action.CROP");

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,3);

}

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();// 創(chuàng)建文件夾

? ? ? ? String fileName =path +"head.jpg";// 圖片名字

? ? ? ? try {

b =new FileOutputStream(fileName);

mBitmap.compress(Bitmap.CompressFormat.JPEG,100, b);// 把數(shù)據(jù)寫入文件

? ? ? ? }catch (FileNotFoundException e) {

e.printStackTrace();

}finally {

try {

// 關(guān)閉流

? ? ? ? ? ? ? ? b.flush();

b.close();

}catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 把bitmap轉(zhuǎn)成圓形

* */

? ? public Bitmap toRoundBitmap(Bitmap bitmap) {

int width = bitmap.getWidth();

int height = bitmap.getHeight();

int r =0;

// 取最短邊做邊長

? ? ? ? if (width < height) {

r = width;

}else {

r = height;

}

// 構(gòu)建一個bitmap

? ? ? ? Bitmap backgroundBm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

// new一個Canvas,在backgroundBmp上畫圖

? ? ? ? Canvas canvas =new Canvas(backgroundBm);

Paint p =new Paint();

// 設(shè)置邊緣光滑,去掉鋸齒

? ? ? ? p.setAntiAlias(true);

RectF rect =new RectF(0,0, r, r);

// 通過制定的rect畫一個圓角矩形,當(dāng)圓角X軸方向的半徑等于Y軸方向的半徑時,

// 且都等于r/2時,畫出來的圓角矩形就是圓形

? ? ? ? canvas.drawRoundRect(rect, r /2, r /2, p);

// 設(shè)置當(dāng)兩個圖形相交時的模式,SRC_IN為取SRC圖形相交的部分,多余的將被去掉

? ? ? ? p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

// canvas將bitmap畫在backgroundBmp上

? ? ? ? canvas.drawBitmap(bitmap,null, rect, p);

return backgroundBm;

}

}

3.photo_choose_dialog.xml


? <LinearLayout?

????android:layout_width="match_parent"

? ? android:layout_height="match_parent"

? ? android:background="#00000000"

? ? android:gravity="bottom"

? ? android:orientation="vertical"

? ? android:padding="5dip" >

? ? ? ? android:id="@+id/btn_picture"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:background="@drawable/photo_gallery_selector"

? ? ? ? android:paddingBottom="10dip"

? ? ? ? android:paddingTop="10dip"

? ? ? ? android:text="圖庫"

? ? ? ? android:textSize="16sp" />

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="0.5dip"

? ? ? ? android:background="#DAD9DB" />

? ? ? ? android:id="@+id/btn_photo"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:background="@drawable/photo_camera_selector"

? ? ? ? android:paddingBottom="10dip"

? ? ? ? android:paddingTop="10dip"

? ? ? ? android:text="拍照"

? ? ? ? android:textSize="16sp" />

? ? ? ? android:id="@+id/btn_cancle"

? ? ? ? android:layout_width="match_parent"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:layout_marginTop="5dip"

? ? ? ? android:background="@drawable/photo_cancel_selector"

? ? ? ? android:paddingBottom="10dip"

? ? ? ? android:paddingTop="10dip"

? ? ? ? android:text="取消"

? ? ? ? android:textSize="16sp" />

</LinearLayout>

4.在res目錄下創(chuàng)建anim文件夾,在anim下寫photo_dialog_in_anim.xml,photo_dialog_out_anim.xml

photo_dialog_in_anim.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

? ? <translate

? ? ? ? android:duration="500"

? ? ? ? android:fromXDelta="0"

? ? ? ? android:fromYDelta="1000"

? ? ? ? android:toXDelta="0"

? ? ? ? android:toYDelta="0" />

</set>


photo_dialog_out_anim.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

????<translate

? ? ? ? android:duration="500"

? ? ? ? android:fromXDelta="0"

? ? ? ? android:fromYDelta="0"

? ? ? ? android:toXDelta="0"

? ? ? ? android:toYDelta="1000" />

</set>

5.在values的styles中加入

<style name="transparentFrameWindowStyle" parent="android:style/Theme.Dialog">

? ? <item name="android:windowBackground">@drawable/photo_choose_bg></item>

</style>

<style name="main_menu_animstyle">

<item name="android:windowEnterAnimation">@anim/photo_dialog_in_anim</item>

<item name="android:windowExitAnimation">@anim/photo_dialog_out_anim</item>

</style>


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,391評論 0 17
  • 本人初學(xué)Android,最近做了一個實現(xiàn)安卓簡單音樂播放功能的播放器,收獲不少,于是便記錄下來自己的思路與知識總結(jié)...
    落日柳風(fēng)閱讀 19,479評論 2 41
  • 終于見到你,那個 暗戀了30年的姑娘 酒杯舉起 看你 我已醉的不知 身在家鄉(xiāng) 只想回到我們的 青蔥時光 終于見到你...
    流星給的心愿閱讀 161評論 0 0
  • (九) 第二天,武剛把兩個孩子送到學(xué)校后,就帶著母親坐車往沭城縣城。楊玉珍心里惶惶的,覺得自己像一只待宰的羔羊...
    甜蜜果閱讀 418評論 0 1
  • 本周作業(yè): 瑜伽可以提高我們的覺知度,要學(xué)會轉(zhuǎn)變自己的行為模式。 給時間做合理規(guī)劃,排排座,生命時間有限,智慧...
    如是無痕閱讀 301評論 0 1

友情鏈接更多精彩內(nèi)容