真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android實現(xiàn)本地上傳圖片并設(shè)置為圓形頭像

我們在做項目的時候會用到圓形的圖片,比如用戶頭像,類似QQ。用戶在用QQ更換頭像的時候,上傳的圖片都是矩形的,但顯示的時候確是圓形的。那么這是如何實現(xiàn)的呢,下面我們就來探討下吧。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供隆陽網(wǎng)站建設(shè)、隆陽做網(wǎng)站、隆陽網(wǎng)站設(shè)計、隆陽網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、隆陽企業(yè)網(wǎng)站模板建站服務(wù),十載隆陽做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。


先從本地把圖片上傳到服務(wù)器,然后根據(jù)URL把頭像處理成圓形頭像。

因為上傳圖片用到bmob的平臺,所以要到bmob(http://www.bmob.cn)申請密鑰。

效果圖:

Android實現(xiàn)本地上傳圖片并設(shè)置為圓形頭像

核心代碼如下:

public class MainActivity extends Activity {
        private ImageView iv;
        private String appKey="";                //填寫你的Application ID
        private String path=Environment.getExternalStorageDirectory()+"/11.jpeg";        //要上傳的圖片的路徑
        public final int SIZE=2*1024;                
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Bmob.initialize(this, appKey);                //初始化bmob SDK
                setContentView(R.layout.activity_main);
                initView();
        }
        private void initView() {
                // TODO Auto-generated method stub
                iv=(ImageView)findViewById(R.id.iv);
        }
        /**
         * 上傳文件到bmob后臺
         * */
        public void upload(View v){
                final BmobFile file=new BmobFile(new File(path));
                file.uploadblock(this, new UploadFileListener() {
                        @Override
                        public void onSuccess() {
                                // TODO Auto-generated method stub
                                Person p=new Person();
                                p.setUrl(file.getFileUrl(MainActivity.this));
                                p.save(MainActivity.this);
                                MyTask task=new MyTask();
                                task.execute(file.getFileUrl(MainActivity.this));
                                toast("上傳成功 ");
                        }
                        @Override
                        public void onFailure(int arg0, String arg1) {
                                // TODO Auto-generated method stub
                                toast("上傳失敗 "+arg1);
                                Log.i("---------", "------error "+arg1);
                        }
                });
        }
        /**
         * 根據(jù)URL獲取Bitmap
         * */
        public Bitmap getHttpBitmap(String url){
                Bitmap bitmap=null;
                URL myUrl;
                try {
                        myUrl=new URL(url);
                        HttpURLConnection conn=(HttpURLConnection)myUrl.openConnection();
                        conn.setConnectTimeout(5000);
                        conn.connect();
                        InputStream is=conn.getInputStream();
                        bitmap=BitmapFactory.decodeStream(is);
                        //把bitmap轉(zhuǎn)成圓形
                        bitmap=toRoundBitmap(bitmap);
                        is.close();
                } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                //返回圓形bitmap
                return bitmap;
        }
        /**
         * 把bitmap轉(zhuǎn)成圓形
         * */
        public Bitmap toRoundBitmap(Bitmap bitmap){
                int width=bitmap.getWidth();
                int height=bitmap.getHeight();
                int r=0;
                //取最短邊做邊長
                if(width                        r=width;
                }else{
                        r=height;
                }
                //構(gòu)建一個bitmap
                Bitmap backgroundBm=Bitmap.createBitmap(width,height,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(Mode.SRC_IN));
                //canvas將bitmap畫在backgroundBmp上
                canvas.drawBitmap(bitmap, null, rect, p);
                return backgroundBm;
        }
        class MyTask extends AsyncTask{
  
                @Override
                protected Bitmap doInBackground(String... arg0) {
                        // TODO Auto-generated method stub
                        String url=arg0[0];
                        Bitmap bm=getHttpBitmap(url);
                        return bm;
                }
                @Override
                protected void onPostExecute(Bitmap result) {
                        // TODO Auto-generated method stub
                        iv.setImageBitmap(result);
                }
        }
        public void toast(String msg){
                Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
}


恩,就是這么多了


網(wǎng)站題目:Android實現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
網(wǎng)站URL:http://weahome.cn/article/jggeis.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部