Android將圖片打成圓形
阿新 • • 發佈:2019-01-24
package com.example.hejingzhou.getbitmapdemo; import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.FileNotFoundException; import java.io.IOException; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private String TAG = getClass().getSimpleName(); private Button btnGetAlbum, btnGetCamera; private ImageView imgAlbum, imgCamer,imgRoundAlbum,imgRoundCamer; private int SELECT_PICTURE = 0x00; private int SELECT_CAMER = 0x01; private Bitmap bitmap; private RoundTools roundTools; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(); } /** * 初始化控制元件 */ private void findViewById() { btnGetAlbum = (Button) findViewById(R.id.buttonGetAlbum); btnGetAlbum.setOnClickListener(this); btnGetCamera = (Button) findViewById(R.id.buttonGetCamera); btnGetCamera.setOnClickListener(this); imgAlbum = (ImageView) findViewById(R.id.imageViewAlbum); imgCamer = (ImageView) findViewById(R.id.imageViewCamera); imgRoundAlbum = (ImageView)findViewById(R.id.imageViewRoundAlbum); imgRoundCamer = (ImageView)findViewById(R.id.imageViewRoundCamera); } /** * Button監聽 * * @param v */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonGetAlbum://通過相簿選擇圖片 Intent intentAlbum = new Intent(Intent.ACTION_GET_CONTENT); intentAlbum.addCategory(intentAlbum.CATEGORY_OPENABLE); intentAlbum.setType("image/*"); startActivityForResult(intentAlbum.createChooser(intentAlbum, "選擇圖片"), SELECT_PICTURE); break; case R.id.buttonGetCamera://通過相機選取照片 Intent intentCamer = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intentCamer, SELECT_CAMER); break; } } /** * 返回結果處理 * * @param requestCode 請求程式碼 * @param resultCode 結果程式碼 * @param data 返回資料 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == SELECT_PICTURE) { Log.i(TAG, "相簿"); handle(resultCode, data); imgAlbum.setImageBitmap(bitmap); roundTools = new RoundTools(); Bitmap roundAlbum = roundTools.toRoundBitmap(bitmap); imgRoundAlbum.setImageBitmap(roundAlbum); } else if (requestCode == SELECT_CAMER) { Log.i(TAG, "相機"); /** * 一般手機data會完美的有資料,但是我的魅族就不行,網上查了查,還有一些手機都有這樣的 * 問題比如三星和小米 。那麼我們就用 這種方式獲取bitmap * * if (data.getData() == null) { * bitmap = (Bitmap) data.getExtras().get("data"); * } else try { * bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData()); * } catch (IOException e) { * e.printStackTrace(); * } */ if (data.getData() == null) { bitmap = (Bitmap) data.getExtras().get("data"); Log.i(TAG, "BitData " + bitmap); imgCamer.setImageBitmap(bitmap); roundTools = new RoundTools(); Bitmap roundCamer = roundTools.toRoundBitmap(bitmap); imgRoundCamer.setImageBitmap(roundCamer); } else try { bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData()); if (bitmap != bitmap) {//主要是防止handle處理出錯,就會將先前獲取相簿的照片show出來 imgCamer.setImageBitmap(bitmap); } } catch (IOException e) { e.printStackTrace(); } } } /** * 資料處理 共同點提取 * * @param resultCode * @param data */ private void handle(int resultCode, Intent data) { if (resultCode == RESULT_OK) {//結果程式碼是Ok的 Uri uri = data.getData(); if (uri != null && data.getData() != null) { Log.i(TAG, "uri 和 data.getData()不為空"); ContentResolver contentResolver = this.getContentResolver(); if (bitmap != null) { bitmap.recycle(); } try { bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));//出錯 } catch (FileNotFoundException e) { e.printStackTrace(); } } else { Log.i(TAG, "uri為空或者data為空 " + "資料:" + data.getData() + " uri: " + uri); } } } }
佈局檔案就不寫了,開程式碼能才出來。