Android Multimedia Programming--drawing board

Source: Internet
Author: User
Tags gety

 PackageCom.example.huahuaban;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;Importandroid.os.Environment;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.CompressFormat;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Matrix;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PorterDuff.Mode;ImportAndroid.graphics.PorterDuffXfermode;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.View.OnTouchListener;ImportAndroid.widget.ImageView; Public classMainactivityextendsActivityImplementsonclicklistener{PrivateImageView img;    Bitmap bitmapcopy;    Paint paint;    Bitmap bitmapsrc; floatStartX =0; floatStarty = 0; PrivateCanvas Canvas; PrivateMatrix MT; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); IMG=(ImageView) Findviewbyid (r.id.img); Findviewbyid (r.id.red). Setonclicklistener ( This); Findviewbyid (R.id.gereen). Setonclicklistener ( This); Findviewbyid (R.id.blue). Setonclicklistener ( This); Findviewbyid (R.id.shua). Setonclicklistener ( This); Findviewbyid (R.id.qingkong). Setonclicklistener ( This); Findviewbyid (R.id.save). Setonclicklistener ( This); BITMAPSRC=Bitmapfactory.decoderesource (Getresources (), r.drawable.bg); Bitmapcopy=Bitmap.createbitmap (Bitmapsrc.getwidth (), Bitmapsrc.getheight (), Bitmapsrc.getconfig ()); //BrushesPaint =NewPaint (); Canvas=NewCanvas (bitmapcopy); Mt=NewMatrix ();            Canvas.drawbitmap (BITMAPSRC, MT, paint); Img.setontouchlistener (NewOntouchlistener () {@Override Public BooleanOnTouch (View arg0, motionevent event) {intAction =event.getaction (); Switch(action) { CaseMotionevent.action_down://set the start coordinate of a segmentStartX =Event.getx (); Starty=event.gety ();  Break;  CaseMotionevent.action_move://End coordinates                    floatx =Event.getx (); floaty =event.gety ();                    Canvas.drawline (StartX, Starty, x, y, paint);                    Img.setimagebitmap (bitmapcopy); //assigns the value of the end coordinate to the start coordinateStartX =x; Starty=y;  Break;  Casemotionevent.action_up: Break; }                return true;    }        }); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.red:paint.setcolor (color.red);  Break;  CaseR.id.gereen:paint.setcolor (Color.green);  Break;  CaseR.id.blue:paint.setcolor (Color.Blue);  Break;  CaseR.id.qingkong:paint.setxfermode (NewPorterduffxfermode (mode.clear));             Canvas.drawbitmap (BITMAPSRC, MT, paint); Paint.setxfermode (NewPorterduffxfermode (MODE.SRC));            Img.invalidate ();  Break;  CaseR.id.shua:paint.setstrokewidth (3.5f);  Break;  Caser.id.save:file File=NewFile ("Sdcard/ok1.png"); FileOutputStream out=NULL; Try{ out=Newfileoutputstream (file); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); }            //Save the file, the parameters are format, compression quality 0-100, output streamBitmapcopy.compress (compressformat.png, 100, out); //send a broadcast that updates a multimedia databaseIntent Intent =NewIntent ();            Intent.setaction (intent.action_media_mounted);            Intent.setdata (Uri.fromfile (Environment.getexternalstoragedirectory ()));            Sendbroadcast (Intent);  Break; }            }}
<LinearLayoutxmlns: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"Tools:context=". Mainactivity "android:orientation= "vertical">    <ImageViewAndroid:id= "@+id/img"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/bg"/>    <LinearLayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal"        >        <ButtonAndroid:id= "@+id/red"Android:background= "#FF0000"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"                        />        <ButtonAndroid:id= "@+id/gereen"Android:background= "#00FF00"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"                        />        <ButtonAndroid:id= "@+id/blue"Android:background= "#3366FF"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"                        />        <ButtonAndroid:id= "@+id/shua"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Bold"            />        <ButtonAndroid:id= "@+id/qingkong"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Empty"            />    </LinearLayout>         <ButtonAndroid:id= "@+id/save"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Save"            /></LinearLayout>

Android Multimedia Programming--drawing board

Related Article

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.