Android之UI學習篇:ImageView實現適屏和裁剪圖片的功能

來源:互聯網
上載者:User

ImageView實現圖片適應螢幕大小顯示,和圖片裁剪的功能.實現的效果主介面: 適應螢幕: 裁剪圖片: 顯示裁剪圖片到ImagView: 原始碼:MainActivity.java[html]  www.2cto.compackage com.imageview.activity;    import java.io.FileNotFoundException;  import android.app.Activity;  import android.content.Intent;  import android.graphics.Bitmap;  import android.graphics.BitmapFactory;  import android.net.Uri;  import android.os.Bundle;  import android.util.Log;  import android.view.View;  import android.view.View.OnClickListener;  import android.widget.Button;  import android.widget.ImageView;  import android.widget.Toast;    public class MainActivity extends Activity implements OnClickListener {      private Button imageSelectBtn;      private Button imageCutBtn;      private ImageView imageView;      // 聲明兩個靜態整型變數,用於意圖的返回標誌      private static final int IMAGE_SELECT = 1; // 選擇圖片      private static final int IMAGE_CUT = 2; // 裁剪圖片        @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          setupViews();      }        // 我的一貫作風呵呵      public void setupViews() {          imageSelectBtn = (Button) findViewById(R.id.imageSelectButton);          imageSelectBtn.setOnClickListener(this);          imageCutBtn = (Button) findViewById(R.id.imageCutButton);          imageCutBtn.setOnClickListener(this);          imageView = (ImageView) findViewById(R.id.imageView);      }        @Override      protected void onActivityResult(int requestCode, int resultCode, Intent data) {          super.onActivityResult(requestCode, resultCode, data);          if (resultCode == RESULT_OK) {              // 處理圖片按照手機螢幕大小顯示              if (requestCode == IMAGE_SELECT) {                  // 獲得圖片的路徑                  Uri uri = data.getData();                   // 獲得螢幕寬度                  int dw = getWindowManager().getDefaultDisplay().getWidth();                   // 獲得螢幕寬度                  int dh = getWindowManager().getDefaultDisplay().getHeight() / 2;                   try {                      // 實現對圖片裁剪的類,是一個匿名內部類                      BitmapFactory.Options factory = new BitmapFactory.Options();                      // 如果設定為true,允許查詢圖片不是按照像素分配記憶體                      factory.inJustDecodeBounds = true;                      Bitmap bmp = BitmapFactory.decodeStream(                              getContentResolver().openInputStream(uri), null,                              factory);                      // 對圖片的高度和寬度對應手機螢幕進行匹配                      // 寬度之比                      int wRatio = (int) Math.ceil(factory.outWidth / (float) dw);                       // 高度之比                      int hRatio = (int) Math.ceil(factory.outHeight / (float) dh);                       // 如果wRatio大於1,表示圖片的寬度大於螢幕寬度,類似hRatio                      if (wRatio > 1 || hRatio > 1) {                          // inSampleSize>1則返回比原圖更小的圖片                          if (hRatio > wRatio) {                              factory.inSampleSize = hRatio;                          } else {                              factory.inSampleSize = wRatio;                          }                      }                      // 該屬性為false則允許調用者查詢圖片無需為像素分配記憶體                      factory.inJustDecodeBounds = false;                      // 再次使用BitmapFactory對象映像進行適屏操作                      bmp = BitmapFactory.decodeStream(getContentResolver()                              .openInputStream(uri), null, factory);                      imageView.setImageBitmap(bmp);                  } catch (FileNotFoundException e) {                      e.printStackTrace();                  }              } else if (requestCode == IMAGE_CUT) { // 裁剪圖片                  // 一定要和"return-data"返回的標籤"data"一致                  Bitmap bmp = data.getParcelableExtra("data");                   imageView.setImageBitmap(bmp);              }          }      }        @Override      public void onClick(View v) {          switch (v.getId()) {          case R.id.imageSelectButton:              // 如何提取手機的圖片,並且進行圖片的選擇              Intent intent = new Intent(                      Intent.ACTION_PICK,                      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);              startActivityForResult(intent, IMAGE_SELECT);              break;          case R.id.imageCutButton:              Intent intent2 = getImageClipIntent();              startActivityForResult(intent2, IMAGE_CUT);              break;          default:              break;          }      }        // 擷取裁剪圖片意圖的方法      private Intent getImageClipIntent() {          Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);          // 實現對圖片的裁剪,必須要設定圖片的屬性和大小          intent.setType("image/*"); // 設定屬性,表示擷取任意類型的圖片          intent.putExtra("crop", "true");// 設定可以滑動選選擇地區的屬性,注意這裡是字串"true"          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;      }  }    布局檔案main.xml[html]  <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >        <Button           android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="選擇圖片"          android:id="@+id/imageSelectButton"/>      <Button           android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="選擇圖片進行裁剪"          android:id="@+id/imageCutButton"/>      <!-- 用於顯示裁剪後的圖片 -->      <ImageView           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:id="@+id/imageView"/>    </LinearLayout>    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.