Android基礎彙總
一、 功能: 擷取相冊圖檔并手動進行裁切 二、簡介 基于ASOP (Android Open-Source Project:Android 開放源代碼項目) 維護更新頻次不是很高 Github位址: https://github.com/jdamcd/android-crop 三、主要特點 Gradle建構和AAR 現代化的UI 向後相容到SDK 10 配置簡單 四、使用 依賴這個 AAR 釋出在 Maven Central :
1 | compile 'com.soundcloud.android:android-crop:[email protected]' |
在你的 manifest 檔案中申明 CropImageActivity :
1 | <activity android : name = "com.soundcloud.android.crop.CropImageActivity" /> |
選擇 這個庫提供了一個實用的方法期待一個圖像選擇器:
1 | Crop . pickImage ( activity ) |
裁切
1 | Crop . of ( inputUri , outputUri ). asSquare (). start ( activity ) |
監聽裁切的結果:
1 2 3 4 5 6 | @Override protected void onActivityResult ( int requestCode , int resultCode , Intent result ) { if ( requestCode == Crop . REQUEST_CROP && resultCode == RESULT_OK ) { doSomethingWithCroppedImage ( outputUri ); } } |
裁切見面的屬性是可以定制的,詳情請檢視執行個體項目的 主題 。 使用者 使用該庫的應用程式包括: SoundCloud , Depop , Polyvore , TextSecure 五 Demo: 1、 gradle,在dependencies節點下添加: compile 'com.soundcloud.android.crop:lib_crop:1.0.0' 2、 在清單檔案中注冊裁剪功能所在的頁面: < activity android:name = "com.soundcloud.android.crop.CropImageActivity" /> 3、 代碼:
public class MainActivity extends AppCompatActivity{
private ImageViewiv;
@Override
protected voidonCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView)findViewById(R.id.iv);
}
//1、進入相冊
public voidpick(View v){
Crop.pickImage(this);
}
@Override
protected voidonActivityResult(intrequestCode,intresultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==Crop.REQUEST_PICK&&resultCode==RESULT_OK){
//2、從相冊回來,進入裁剪頁
beginCrop(data.getData());
}else if(requestCode==Crop.REQUEST_CROP){
//3、從裁剪頁回來,設定圖檔
handleCrop(resultCode,data);
}
}
private voidhandleCrop(intresultCode,Intent result){
if(resultCode==RESULT_OK){
Uri uri=Crop.getOutput(result);
inth=getResources().getDimensionPixelSize(R.dimen.height);
Bitmap bitmap=UtilsBitmap.decodeUri(this,uri,h,h);
iv.setImageBitmap(bitmap);
}else if(resultCode==Crop.RESULT_ERROR){
Toast.makeText(this,Crop.getError(result).getMessage(),Toast.LENGTH_SHORT).show();
}
}
private voidbeginCrop(Uri source){
Uri destination=Uri.fromFile(new File(getCacheDir(),"cropped_"+System.currentTimeMillis()+".jpg"));
Crop.of(source,destination).asSquare().start(this);
}
}
Utils:
public classUtilsBitmap{
/**
* 讀取一個縮放後的圖檔,限定圖檔大小,避免OOM
* @paramuri圖檔uri,支援“file://”、“content://”
* @parammaxWidth最大允許寬度
* @parammaxHeight最大允許高度
* @return傳回一個縮放後的Bitmap,失敗則傳回null
*/
public staticBitmapdecodeUri(Context context,Uri uri,intmaxWidth,intmaxHeight){
BitmapFactory.Options options=newBitmapFactory.Options();
options.inJustDecodeBounds=true; //隻讀取圖檔尺寸
resolveUri(context,uri,options);
//計算實際縮放比例
intscale=1;
for(inti=0;i<Integer.MAX_VALUE;i++){
if((options.outWidth/scale>maxWidth&&options.outWidth/scale>maxWidth*1.4)||
(options.outHeight/scale>maxHeight&&options.outHeight/scale>maxHeight*1.4)){
scale++;
}else{
break;
}
}
options.inSampleSize=scale;
options.inJustDecodeBounds=false;//讀取圖檔内容
options.inPreferredConfig=Bitmap.Config.RGB_565;//根據情況進行修改
Bitmap bitmap=null;
try{
bitmap=resolveUriForBitmap(context,uri,options);
}catch(Throwable e){
e.printStackTrace();
}
returnbitmap;
}
private static voidresolveUri(Context context,Uri uri,BitmapFactory.Options options){
if(uri==null){
return;
}
String scheme=uri.getScheme();
if(ContentResolver.SCHEME_CONTENT.equals(scheme)||
ContentResolver.SCHEME_FILE.equals(scheme)){
InputStream stream=null;
try{
stream=context.getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(stream,null,options);
}catch(Exception e){
Log.w("resolveUri","Unable to open content: "+uri,e);
}finally{
if(stream!=null){
try{
stream.close();
}catch(IOException e){
Log.w("resolveUri","Unable to close content: "+uri,e);
}
}
}
}else if(ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)){
Log.w("resolveUri","Unable to close content: "+uri);
}else{
Log.w("resolveUri","Unable to close content: "+uri);
}
}
private staticBitmapresolveUriForBitmap(Context context,Uri uri,BitmapFactory.Options options){
if(uri==null){
return null;
}
Bitmap bitmap=null;
String scheme=uri.getScheme();
if(ContentResolver.SCHEME_CONTENT.equals(scheme)||
ContentResolver.SCHEME_FILE.equals(scheme)){
InputStream stream=null;
try{
stream=context.getContentResolver().openInputStream(uri);
bitmap=BitmapFactory.decodeStream(stream,null,options);
}catch(Exception e){
Log.w("resolveUriForBitmap","Unable to open content: "+uri,e);
}finally{
if(stream!=null){
try{
stream.close();
}catch(IOException e){
Log.w("resolveUriForBitmap","Unable to close content: "+uri,e);
}
}
}
}else if(ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)){
Log.w("resolveUriForBitmap","Unable to close content: "+uri);
}else{
Log.w("resolveUriForBitmap","Unable to close content: "+uri);
}
returnbitmap;
}
}