Android UI: ImageView for screen adaptation and image Cropping

Source: Internet
Author: User

ImageView allows you to adjust images to the screen size and crop images. main Interface: Adapt to the screen: Crop an image: display the cropped image to ImagView: source code: 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; // declare two static integer variables, private static final int IMAGE_SELECT = 1; // select the image private static final Int IMAGE_CUT = 2; // crop the image @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); setupViews () ;}// my consistent style Haha public void setupViews () {imageSelectBtn = (Button) findViewById (R. id. imageSelectButton); imageSelectBtn. setOnClickListener (this); imageCutBtn = (Button) findViewById (R. id. imageCutButton); imageCutBtn. setOnClickListe Ner (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) {// display the processed image according to the phone screen size if (requestCode = IMAGE_SELECT) {// obtain the image URL Uri = data. getData (); // get the screen width int dw = getWindowManager (). getdefadisplay display (). getWidth (); // Get the screen width int dh = getWindowManager (). getdefadisplay display (). getHeight ()/2; try {// class for cropping images, which is an anonymous internal class BitmapFactory. options factory = new BitmapFactory. options (); // if this parameter is set to true, the memory factory is not allocated by pixels. inJustDecodeBounds = true; Bitmap bmp = BitmapFactory. decodeStream (getContentResolver (). openInputStream (uri), null, factory); // match the height and width of the image on the mobile phone screen. // The ratio of width to int wRatio = (int) Math. ceil (factory. OutWidth/(float) dw); // The ratio of height to int hRatio = (int) Math. ceil (factory. outHeight/(float) dh); // if wRatio is greater than 1, the image width is greater than the screen width, similar to hRatio if (wRatio> 1 | hRatio> 1) {// inSampleSize> 1, if (hRatio> wRatio) {factory. inSampleSize = hRatio;} else {factory. inSampleSize = wRatio; }}// if this attribute is set to false, the caller is allowed to query images without allocating the factory memory for pixels. inJustDecodeBounds = false; // use the BitmapFactory object image again for screen adaptation. bmp = Bitm ApFactory. decodeStream (getContentResolver (). openInputStream (uri), null, factory); imageView. setImageBitmap (bmp);} catch (FileNotFoundException e) {e. printStackTrace () ;}} else if (requestCode = IMAGE_CUT) {// crop the image // it must be consistent with the label "data" returned by "return-data" Bitmap bmp = data. getParcelableExtra ("data"); imageView. setImageBitmap (bmp) ;}}@ Override public void onClick (View v) {switch (v. getId () {ca Se R. id. imageSelectButton: // how to extract the image from the mobile phone and select 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 ;}// method for obtaining the Intent of cropping an image private Intent getImageClipIntent () {Intent intent = new Intent (Intent. ACTION_GET_CONTENT, null); // to crop an image, you must set the image attributes and size intent. setType ("image/*"); // sets attributes to obtain intent images of any type. putExtra ("crop", "true"); // you can slide the property of the selected region. Note that the "true" intent string is used here. putExtra ("aspectX", 1); // you can specify a ratio of to intent. putExtra ("aspectY", 1); intent. putExtra ("outputX", 80); intent. putExtra ("outputY", 80); intent. putExtra ("return-data", true); return intent ;}} Layout file 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 = "select image" android: id = "@ + id/imageSelectButton"/> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" andro Id: text = "select image cropping" android: id = "@ + id/imageCutButton"/> <! -- Used to display the cropped image --> <ImageView android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/imageView"/> </LinearLayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.