最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能

 更新時(shí)間:2017年02月24日 17:17:06   作者:shuang_bing  
這篇文章主要介紹了Android ImageView實(shí)現(xiàn)圖片裁剪和顯示功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

首先在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);
  // 注冊(cè)監(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)對(duì)圖片的裁剪,必須要設(shè)置圖片的屬性和大小
  intent.setType("image/*");//獲取任意的圖片類型 Set an explicit MIME data type.每個(gè)MIME類型由兩部分組成,前面是數(shù)據(jù)的大類別,例如聲音audio、圖象image等,后面定義具體的種類。
  intent.putExtra("crop", "true");//滑動(dòng)選中圖片區(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)閉后會(huì)向前面的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平臺(tái)會(huì)調(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的值是自定義的,用于識(shí)別跳轉(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)對(duì)圖片的裁剪的類,是一個(gè)匿名內(nèi)部類
     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = false;
     //對(duì)圖片的寬度和高度對(duì)應(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è)圖片,如果太大,就會(huì)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)圖片裁剪和顯示功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

休宁县| 石柱| 沁源县| 四平市| 阳西县| 达孜县| 广宗县| 锡林浩特市| 治多县| 漾濞| 会东县| 商南县| 临桂县| 石渠县| 荣昌县| 汝城县| 伊金霍洛旗| 津市市| 耒阳市| 宣城市| 民勤县| 龙陵县| 团风县| 绩溪县| 库尔勒市| 读书| 将乐县| 沾益县| 五莲县| 开原市| 庆阳市| 商水县| 宜兰县| 沅陵县| 桦甸市| 洛宁县| 临汾市| 黄平县| 凌海市| 威海市| 鄢陵县|