Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能
首先在layout布局中設(shè)置按鈕和一個(gè)ImageView
<Button android:id="@+id/selectimagebtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選擇圖片" /> <Button android:id="@+id/cutimagebtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="選擇圖片進(jìn)行裁剪" /> <!-- 用于顯示圖片的信息 --> <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" />
在Activity上寫代碼
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button selectImageBtn, cutImageBtn; private ImageView imageView; // 聲明兩個(gè)靜態(tài)的整型變量,主要用于意圖的返回的標(biāo)志 private static final int IMAGE_SELECT = 1;// 選擇圖片 private static final int IMAGE_CUT = 2;// 裁剪圖片 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); selectImageBtn = (Button) findViewById(R.id.selectimagebtn); cutImageBtn = (Button) findViewById(R.id.cutimagebtn); imageView = (ImageView) findViewById(R.id.imageview); // 注冊監(jiān)聽事件 selectImageBtn.setOnClickListener(this); cutImageBtn.setOnClickListener(this); }
實(shí)現(xiàn)OnClickListener的方法,和設(shè)置裁剪圖片的方法
@Override public void onClick(View v) { switch (v.getId()) { case R.id.selectimagebtn: //如何提取手機(jī)的圖片庫,并且進(jìn)行選擇圖片的功能 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//打開手機(jī)的圖片庫 startActivityForResult(intent, IMAGE_SELECT); break; case R.id.cutimagebtn: Intent intent2 = getImageClipIntent(); startActivityForResult(intent2, IMAGE_CUT); } } private Intent getImageClipIntent() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);//不指定URL //實(shí)現(xiàn)對圖片的裁剪,必須要設(shè)置圖片的屬性和大小 intent.setType("image/*");//獲取任意的圖片類型 Set an explicit MIME data type.每個(gè)MIME類型由兩部分組成,前面是數(shù)據(jù)的大類別,例如聲音audio、圖象image等,后面定義具體的種類。 intent.putExtra("crop", "true");//滑動選中圖片區(qū)域 intent.putExtra("aspectX", 1);//表示剪切框的比例1:1的效果 intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80);//指定輸出圖片的大小 intent.putExtra("outputY", 80); intent.putExtra("return-data", true);//有返回值 return intent; }
如果你想在Activity中得到新打開Activity關(guān)閉后返回的數(shù)據(jù),你需要使用系統(tǒng)提供的startActivityForResult(Intent intent,int requestCode)方法打開新的Activity,新的Activity關(guān)閉后會向前面的Activity傳回?cái)?shù)據(jù),為了得到傳回的數(shù)據(jù),你必須在前面的Activity中重寫onActivityResult(int requestCode, int resultCode,Intent data)方法
當(dāng)新Activity關(guān)閉后,新Activity返回的數(shù)據(jù)通過Intent進(jìn)行傳遞,Android平臺會調(diào)用前面Activity的onActivityResult()方法,把存放了返回?cái)?shù)據(jù)的Intent作為第三個(gè)輸入?yún)?shù)傳入,在onActivityResult()方法中使用第三個(gè)輸入?yún)?shù)可以取出新Activity返回的數(shù)據(jù)。
需要返回?cái)?shù)據(jù)或結(jié)果的,則使用startActivityForResult (Intent intent, intrequestCode)
,requestCode的值是自定義的,用于識別跳轉(zhuǎn)的目標(biāo)Activity。
覆蓋onActivityResult方法
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { //處理圖片按照手機(jī)的屏幕大小顯示 if (requestCode == IMAGE_SELECT) { Uri uri = data.getData();//獲得圖片的路徑 Display display = getWindowManager().getDefaultDisplay(); Point point = new Point(); display.getSize(point); int width = point.x;//獲得屏幕的寬度 int height = point.y ;//屏幕高度 try { //實(shí)現(xiàn)對圖片的裁剪的類,是一個(gè)匿名內(nèi)部類 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; //對圖片的寬度和高度對應(yīng)手機(jī)的屏幕進(jìn)行匹配 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); //如果大于1表示圖片的高度大于手機(jī)屏幕的高度 int hRatio = (int) Math.ceil(options.outHeight / (float) height);//(int)Math.ceil是下取整 //如果大于1表示圖片的寬度大于手機(jī)屏幕的寬度 int wRatio = (int) Math.ceil(options.outWidth / (float) width); //如果hRatio或wRatio大于1,則把圖片縮放到1/radio的尺寸和1/radio^2的像素 if (hRatio > 1 || wRatio > 1) { if (hRatio > wRatio) { options.inSampleSize = hRatio; } else { options.inSampleSize = wRatio; } bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); imageView.setImageBitmap(bitmap); }else{ //如果hRatio與wRatio為0,直接輸出 imageView.setImageBitmap(bitmap); } } catch (Exception e) { } //表示裁剪圖片 } else if (requestCode == IMAGE_CUT) { Bitmap bitmap = data.getParcelableExtra("data"); imageView.setImageBitmap(bitmap); } } }
options.inJustDecodeBounds = false/true;
我們?nèi)ソ馕鲆粋€(gè)圖片,如果太大,就會OOM,我們可以設(shè)置壓縮比例inSampleSize,但是這個(gè)壓縮比例設(shè)置多少就是個(gè)問題,所以我們解析圖片可以分為倆個(gè)步驟,第一步就是獲取圖片的寬高,這里要設(shè)置Options.inJustDecodeBounds=true
,這時(shí)候decode的bitmap為null,只是把圖片的寬高放在Options里。
然后第二步就是設(shè)置合適的壓縮比例inSampleSize,inSampleSize為原來的1/ratio,這時(shí)候獲得合適的Bitmap。
再設(shè)置options.inJustDecodeBounds = false;重新讀出圖片bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
以上所述是小編給大家介紹的Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題的解決方法
- Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
- android調(diào)用原生圖片裁剪后圖片尺寸縮放的解決方法
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android圖片裁剪功能實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)相機(jī)拍攝、選擇、圖片裁剪功能
- Android開發(fā)從相機(jī)或相冊獲取圖片裁剪
- 使用Java代碼在Android中實(shí)現(xiàn)圖片裁剪功能
- android實(shí)現(xiàn)圖片裁剪的兩種方法
相關(guān)文章
Android NDK開發(fā)簡單程序分享(Hello Word!)
本文主要對Android NDK開發(fā)簡單程序(Hello Word!)的實(shí)現(xiàn)步驟及方法進(jìn)行詳細(xì)介紹。具有很好的參考價(jià)值,需要的朋友一起來看下吧2016-12-12Android拼接實(shí)現(xiàn)動態(tài)對象方法詳解
這篇文章主要為大家介紹了Android拼接實(shí)現(xiàn)動態(tài)對象方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Android自定義View實(shí)現(xiàn)五子棋游戲
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Android Map新用法:MapFragment應(yīng)用介紹
MapView ,MapActivity 這種的局限在于,必須要繼承MapActivity,否則無法使用MapView,但是,MapFragment 這種的局限在于,必須要安裝Google Play Service ,也就是說必須是原生rom。而且sdk要在12以上2013-01-01android實(shí)現(xiàn)文字水印效果 支持多行水印
這篇文章主要為大家詳細(xì)介紹了android添加文字水印,并支持多行水印,自定義角度和文字大小,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android?AccessibilityService?事件分發(fā)原理分析總結(jié)
這篇文章主要介紹了Android?AccessibilityService?事件分發(fā)原理分析總結(jié),AccessibilityService有很多用來接收外部調(diào)用事件變化的方法,這些方法封裝在內(nèi)部接口Callbacks中,文章圍繞AccessibilityService相關(guān)資料展開詳情,需要的朋友可以參考一下2022-06-06Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例
本篇文章主要介紹了Android 掃描附近的藍(lán)牙設(shè)備并連接藍(lán)牙音響的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09