編輯:關於Android編程
從系統相冊中選擇照片或則調用系統相機。大部分的項目都會用到,我進行了一下封裝,仿ios的效果。
效果如下:
package com.zhang.test.picture; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.provider.MediaStore; import java.io.File; /** * Camera的基類 * @author zhang * @time 2016/7/13 15:03 */ public class CameraBase { //拍照裁剪的Code public static final int REQUEST_TAKE_PHOTO_CODE = 1001; //調用系統圖庫裁剪Code public static final int REQUEST_TAKE_PICTRUE_CODE = 1002; //裁剪的Code public static final int REQUEST_TAKE_CROP_CODE = 1003; //截取圖片的高度 public static final int REQUEST_HEIGHT = 100; //截取圖片的寬度 public static final int REQUEST_WIDTH = 200; //用來存儲照片的URL private Uri photoURL; //調用照片的Activity private Activity activity; //回調函數 private CameraResult02 cameraResult; public CameraBase(CameraResult02 cameraResult, Activity activity) { this.cameraResult = cameraResult; this.activity = activity; } //調用系統照相機,對Intent參數進行封裝 protected Intent startTakePhoto(Uri photoURL) { this.photoURL = photoURL; Intent intent = new Intent(); //設置Action為拍照 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); //將拍取的照片保存到指定URI intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURL); return intent; } //調用系統圖庫,對Intent參數進行封裝 protected Intent startTakePicture(Uri photoURL) { this.photoURL = photoURL; Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); intent.setType("image/*");//從所有圖片中進行選擇 return intent; } //調用系統裁剪圖片,對Intent參數進行封裝 protected Intent takeCropPicture(Uri photoURL, int with, int height) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(photoURL, "image/*"); intent.putExtra("crop", "true"); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁圖片的寬高 intent.putExtra("outputX", with); intent.putExtra("outputY", height); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true);//黑邊 intent.putExtra("return-data", true); return intent; } //拍照並裁剪 public void getPhotoCameraCrop(Uri uri) { Intent intent = startTakePhoto(uri); activity.startActivityForResult(intent, REQUEST_TAKE_PHOTO_CODE); } //獲取系統相冊後裁剪 public void getPhotoAlbumCrop(Uri uri) { activity.startActivityForResult(startTakePicture(uri), REQUEST_TAKE_PICTRUE_CODE); } //處理onActivityResult public void onResult(int requestCode, int resultCode, Intent intent) { if (resultCode == Activity.RESULT_OK) { switch (requestCode) { //選擇系統圖庫.裁剪 case REQUEST_TAKE_PICTRUE_CODE: photoURL = intent.getData(); // Log.i("info", "photoURL: 111" + photoURL); activity.startActivityForResult(takeCropPicture(photoURL, REQUEST_HEIGHT, REQUEST_WIDTH), REQUEST_TAKE_CROP_CODE); break; //調用相機,裁剪 case REQUEST_TAKE_PHOTO_CODE: // Log.i("info", "onResult: 111" + REQUEST_TAKE_PHOTO_CODE); activity.startActivityForResult(takeCropPicture(Uri.fromFile(new File (photoURL.getPath())), REQUEST_HEIGHT, REQUEST_WIDTH), REQUEST_TAKE_CROP_CODE); break; //裁剪之後的回調 case REQUEST_TAKE_CROP_CODE: // Log.i("info", "onResult: 222" + REQUEST_TAKE_CROP_CODE); Bitmap bitmap = intent.getParcelableExtra("data"); if (bitmap != null) { cameraResult.onSuccess(bitmap); } else { cameraResult.onFail("圖片為空"); } break; default: break; } } } }
package com.zhang.test.picture; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Environment; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; /** * 對圖片操作的代理 * * @author zhang * @time 2016/7/13 14:59 */ public class CameraImg { //相機核心類 private CameraBase CameraBase; // 創建一個以當前時間為名稱的文件 File tempFile = new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera", getPhotoFileName()); public CameraImg(CameraResult02 cameraResult, Activity activity) { CameraBase = new CameraBase(cameraResult, activity); } //拍照並截圖 public void getPhotoCameraCrop() { // //將File對象轉換為Uri並啟動照相程序 Uri uri = Uri.fromFile(tempFile); // Log.i("info", "getPhotoCamera" + tempFile); CameraBase.getPhotoCameraCrop(uri); } //選擇照片,並截圖 public void getPhotoAlbumCrop() { Uri uri = Uri.fromFile(tempFile); // Log.i("info", "getPhotoAlbumCrop>>>>>>" +tempFile); CameraBase.getPhotoAlbumCrop(uri); } //接受ActivityResult public void onResult(int requestCode, int resultCode, Intent data) { CameraBase.onResult(requestCode, resultCode, data); } // 使用系統當前日期加以調整作為照片的名稱 private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; } }
package com.zhang.test.picture; import android.graphics.Bitmap; /** * zhang */ public interface CameraResult02 { //失敗並返回message public abstract void onFail(String message) ; //成功並返回Bitmap public abstract void onSuccess(Bitmap bitmap); }
package com.zhang.test.picture; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.ImageView; import android.widget.PopupWindow; import com.zhang.test.R; /** * 模仿蘋果拍照的效果 * * @author zhang * @time 2016/7/13 14:50 */ public class PhotoActivity extends Activity implements CameraResult02 { private CameraImg cameraImg; private PopupWindow popupWindow; private Button popCameraBt, popPhotorBt, popCancelBt; private Button btn;// private ImageView img; private View mainView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.photo); cameraImg = new CameraImg(this, PhotoActivity.this); initPopWindow(); initView(); setPopListen(); } private void initView() { mainView = findViewById(R.id.main); btn = (Button) findViewById(R.id.photo_btn); img = (ImageView) findViewById(R.id.photo_img); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //第一個參數是要將PopupWindow放到的View,第二個參數是位置,第三第四是偏移值 popupWindow.showAtLocation(mainView, Gravity.BOTTOM, 0, 0); } }); } public void initPopWindow() { View popView = getLayoutInflater().inflate(R.layout.popwindow, null); // popupWindow = new PopupWindow(popView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.popAnimStyle); // popupWindow.setFocusable(false); popCameraBt = (Button) popView.findViewById(R.id.tv01_pop); popPhotorBt = (Button) popView.findViewById(R.id.tv02_pop); popCancelBt = (Button) popView.findViewById(R.id.tv03_pop); } public void setPopListen() { //拍照截圖 popCameraBt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //關閉popupWindow popupWindow.dismiss(); //拍照 cameraImg.getPhotoCameraCrop(); } }); //圖庫選擇 popPhotorBt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); cameraImg.getPhotoAlbumCrop(); } }); //取消 popCancelBt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); cameraImg.onResult(requestCode, resultCode, data); } @Override public void onFail(String message) { Log.i("info", "onFail----" + message); } @Override public void onSuccess(Bitmap bitmap) { Log.i("info", "Bitmap----" + bitmap); img.setImageBitmap(bitmap); popupWindow.dismiss(); } }
Android中自定義View的實現比較簡單,無非就是繼承父類,然後重載方法,即便如此,在實際編碼中難免會遇到一些坑,我把自己遇到的一些問題和解決方法總結一下,希望對廣大
雖然QQ的功能更多,但越來越多的用戶都習慣使用微信作為聊天和分享的工具。問題來了,在我們用微信給好友傳圖時總有最多9張的數量限制,如何才能破解這個限制呢?令
本文實例講述了Android編程實現調用系統圖庫與裁剪圖片功能。分享給大家供大家參考,具體如下:在Android開發中,調用系統圖庫和裁剪照片是很常見的需求。相對於自己實
第一步,打開android studio輸入你的項目名稱“Hello World”.“Company Domian”:暫時
實現功能:實現NetMusicListAdapter(網絡音樂列表適配器