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)系作者
  • 序言:七十年代末掂骏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子厚掷,更是在濱河造成了極大的恐慌弟灼,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,406評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件冒黑,死亡現(xiàn)場離奇詭異田绑,居然都是意外死亡,警方通過查閱死者的電腦和手機薛闪,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,395評論 3 398
  • 文/潘曉璐 我一進店門辛馆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人豁延,你說我怎么就攤上這事昙篙。” “怎么了诱咏?”我有些...
    開封第一講書人閱讀 167,815評論 0 360
  • 文/不壞的土叔 我叫張陵苔可,是天一觀的道長。 經(jīng)常有香客問我袋狞,道長焚辅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,537評論 1 296
  • 正文 為了忘掉前任苟鸯,我火速辦了婚禮同蜻,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘早处。我一直安慰自己湾蔓,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,536評論 6 397
  • 文/花漫 我一把揭開白布砌梆。 她就那樣靜靜地躺著默责,像睡著了一般贬循。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上桃序,一...
    開封第一講書人閱讀 52,184評論 1 308
  • 那天杖虾,我揣著相機與錄音,去河邊找鬼媒熊。 笑死奇适,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的泛释。 我是一名探鬼主播滤愕,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼怜校!你這毒婦竟也來了间影?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,668評論 0 276
  • 序言:老撾萬榮一對情侶失蹤茄茁,失蹤者是張志新(化名)和其女友劉穎魂贬,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體裙顽,經(jīng)...
    沈念sama閱讀 46,212評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡付燥,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,299評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了愈犹。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片键科。...
    茶點故事閱讀 40,438評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖漩怎,靈堂內(nèi)的尸體忽然破棺而出勋颖,到底是詐尸還是另有隱情,我是刑警寧澤勋锤,帶...
    沈念sama閱讀 36,128評論 5 349
  • 正文 年R本政府宣布饭玲,位于F島的核電站,受9級特大地震影響叁执,放射性物質(zhì)發(fā)生泄漏茄厘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,807評論 3 333
  • 文/蒙蒙 一谈宛、第九天 我趴在偏房一處隱蔽的房頂上張望次哈。 院中可真熱鬧,春花似錦吆录、人聲如沸亿乳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,279評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽葛假。三九已至,卻和暖如春滋恬,著一層夾襖步出監(jiān)牢的瞬間聊训,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,395評論 1 272
  • 我被黑心中介騙來泰國打工恢氯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留带斑,地道東北人。 一個月前我還...
    沈念sama閱讀 48,827評論 3 376
  • 正文 我出身青樓勋拟,卻偏偏與公主長得像勋磕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子敢靡,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,446評論 2 359

推薦閱讀更多精彩內(nèi)容

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