Android image editing and processing (i)

Source: Internet
Author: User
<span id="Label3"></p><p><p>1. Select an image using the built-in gallery application:</p></p><p><p></p></p><pre name="code" class="java"></pre><pre name= "code" class= "java" >package com.example.testphotoedit;import Java.io.filenotfoundexception;import Android.app.activity;import Android.content.intent;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.matrix;import Android.graphics.paint;import Android.net.uri;import Android.os.Bundle;import Android.view.display;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;public class Mainactivity extends Activity implements Onclicklistener {private ImageView chosenimageview,copypicture;private Button choosepicture;private Uri imagefileuri; @Overrideprotected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.fragment_main) choosepicture = (Button) Findviewbyid (r.id.button_chose); chosenimageview = (ImageView) Findviewbyid (r.id.chose_picture); copypicture= (ImageView) findviewbYid (r.id.copy_picture); Choosepicture.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method stubif (v.getid () = = R.id.button_chose) {Intent Choose Intent = new Intent (intent.action_pick,android.provider.mediastore.images.media.external_content_uri);// Launch Gallery application Startactivityforresult (chooseintent, 0);}}</pre><pre name="code" class="java"><pre name="code" class="java"></pre></pre><pre name="code" class="java"><pre name="code" class="java">/* (non-javadoc) * @see android.app.activity#onactivityresult (int, int, android.content.Intent) * * in the intent data returned, Returns the URI of the selected image */@Overrideprotected void onactivityresult (int requestcode, int resultcode, Intent data) {//TODO Auto-genera Ted Method Stubsuper.onactivityresult (requestcode, resultcode, data); if (resultcode = = Result_ok) {imagefileuri = Data.getdata ();D isplay currentdisply = Getwindowmanager (). getdefaultdisplay (); int dw = Currentdisply.getwidth ()/2- 100;int DH = currentdisply.getheight ()/2-100;</pre></pre><pre name="code" class="java"><pre name="code" class="java"><span style= "font-family:arial, Helvetica, sans-serif;" >try {</span></pre></pre><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">Bitmapfactory.options bmpfactory = new Bitmapfactory.options (); bmpfactory.injustdecodebounds = true;// The size of the image is loaded rather than the image itself bitmap bmp = Bitmapfactory.decodestream (getcontentresolver (). openinputstream (imagefileuri), null, bmpfactory); int heightratio = (int) math.ceil (bmpfactory.outheight/(float) dh); int widthRatio = (int) Math.ceil ( Bmpfactory.outwidth/(float) dw); If (heightratio > 1 && widthRatio > 1) {if (heightratio > WidthRatio) {b Mpfactory.insamplesize = heightratio;} else {bmpfactory.insamplesize = widthRatio;}} Bmpfactory.injustdecodebounds = false;//loads the real image bmp = Bitmapfactory.decodestream (getcontentresolver (). Openinputstream (imagefileuri), null, bmpfactory); chosenimageview.setimagebitmap (bmp);} Catch (filenotfoundexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}</pre></pre><p><p></p></p><p><p><br></p></p>2. Plotting bitmaps on bitmap<p><p></p></p><pre name="code" class="java"><pre name="code" class="java">bitmap alteredbitmap = bitmap.createbitmap (bmp.getwidth (), bmp.getwidth (), Bmp.getconfig ()); Canvas Canvas=new Canvas (alteredbitmap); Paint paint=new paint (); canvas.drawbitmap (bmp, 0,0, paint); copypicture.setimagebitmap (alteredbitmap); </pre></pre><pre name="code" class="html"><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "com.example.testphotoedit.mainactivity$placeholderfragment" > <button android:id= "@+id/button_ch OSE "android:layout_width=" 120DP "android:layout_height=" 60dp "android:text=" Select Picture "/> <im Ageview android:id= "@+id/chose_picture" android:layout_below= "@+id/button_chose" android:layout_width = "wrap_content" android:layout_height= "wrap_content"/> <imageview android:id= "@+id/copy_picture" android:layout_below= "@+id/chose_picture" AndroiD:layout_width= "wrap_content" android:layout_height= "wrap_content"/></relativelayout> </pre><br>Note: What you do not understand can leave a message.<br><br><br><p><p></p></p><p><p>Android image editing and processing (i)</p></p></span>

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.