Android [advanced tutorial] the Android program calls local images and draws them.

Source: Internet
Author: User

Previous Article

 


 



 

First, let's take a look at the layout. There is only one button and one image.


[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
 
<Button
Android: id = "@ + id/button1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "select image"/>
 
 
<ImageView
Android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: src = "@ drawable/ic_launcher"/>
 
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">

<Button
Android: id = "@ + id/button1"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: text = "select image"/>


<ImageView
Android: id = "@ + id/imageView1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: src = "@ drawable/ic_launcher"/>


</LinearLayout>
Next, click the button to call the local image program [java] // call the local image program
Intent choosePictureIntent = new Intent (Intent. ACTION_PICK,
Android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI );
StartActivityForResult (choosePictureIntent, 0 );
// Call the local image program
Intent choosePictureIntent = new Intent (Intent. ACTION_PICK,
Android. provider. MediaStore. Images. Media. EXTERNAL_CONTENT_URI );
StartActivityForResult (choosePictureIntent, 0 );
The next step is to process the selected image.


[Java] @ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
 
If (resultCode = RESULT_ OK ){
 
// Retrieve the Uri of the returned data
Uri imageFileUri = data. getData ();
// Obtain the current display area
Display currentDisplay = getWindowManager (). getdefadisplay Display ();
 
Float dw = currentDisplay. getWidth ();
Float dh = currentDisplay. getHeight ();
 
Try {
 
// Scale the image
BitmapFactory. Options BMP factoryoptions = new BitmapFactory. Options ();
BMP factoryoptions. inJustDecodeBounds = true;
Bmp = BitmapFactory
. DecodeStream (
GetContentResolver (). openInputStream (
ImageFileUri), null, BMP factoryoptions );
 
Int heightRatio = (int) Math. ceil (BMP factoryoptions. outHeight
/(Float) dh );
Int widthRatio = (int) Math. ceil (BMP factoryoptions. outWidth
/(Float) dw );
 
If (heightRatio> 1 & widthRatio> 1 ){
 
If (heightRatio> widthRatio ){
 
BMP factoryoptions. inSampleSize = heightRatio;
} Else {
BMP factoryoptions. inSampleSize = widthRatio;
}
 
}
 
BMP factoryoptions. inJustDecodeBounds = false;
// Scaled Image
Bmp = BitmapFactory
. DecodeStream (
GetContentResolver (). openInputStream (
ImageFileUri), null, BMP factoryoptions );
 
// Create a new image
AlteredBitmap = Bitmap. createBitmap (bmp. getWidth (),
Bmp. getHeight (), bmp. getConfig ());
 
Canvas = new Canvas (alteredBitmap );
Paint = new Paint ();
Paint. setColor (Color. GREEN );
// Adjust the paint width
Paint. setStrokeWidth (5 );
Matrix = new Matrix ();
Canvas. drawBitmap (bmp, matrix, paint );
 
ChoosenImageView. setImageBitmap (alteredBitmap );
ChoosenImageView. setOnTouchListener (this );
 
} Catch (Exception e ){
 
}
 
}
 
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );

If (resultCode = RESULT_ OK ){

// Retrieve the Uri of the returned data
Uri imageFileUri = data. getData ();
// Obtain the current display area
Display currentDisplay = getWindowManager (). getdefadisplay Display ();

Float dw = currentDisplay. getWidth ();
Float dh = currentDisplay. getHeight ();

Try {

// Scale the image
BitmapFactory. Options BMP factoryoptions = new BitmapFactory. Options ();
BMP factoryoptions. inJustDecodeBounds = true;
Bmp = BitmapFactory
. DecodeStream (
GetContentResolver (). openInputStream (
ImageFileUri), null, BMP factoryoptions );

Int heightRatio = (int) Math. ceil (BMP factoryoptions. outHeight
/(Float) dh );
Int widthRatio = (int) Math. ceil (BMP factoryoptions. outWidth
/(Float) dw );

If (heightRatio> 1 & widthRatio> 1 ){

If (heightRatio> widthRatio ){

BMP factoryoptions. inSampleSize = heightRatio;
} Else {
BMP factoryoptions. inSampleSize = widthRatio;
}

}

BMP factoryoptions. inJustDecodeBounds = false;
// Scaled Image
Bmp = BitmapFactory
. DecodeStream (
GetContentResolver (). openInputStream (
ImageFileUri), null, BMP factoryoptions );

// Create a new image
AlteredBitmap = Bitmap. createBitmap (bmp. getWidth (),
Bmp. getHeight (), bmp. getConfig ());

Canvas = new Canvas (alteredBitmap );
Paint = new Paint ();
Paint. setColor (Color. GREEN );
// Adjust the paint width
Paint. setStrokeWidth (5 );
Matrix = new Matrix ();
Canvas. drawBitmap (bmp, matrix, paint );

ChoosenImageView. setImageBitmap (alteredBitmap );
ChoosenImageView. setOnTouchListener (this );

} Catch (Exception e ){

}

}

} Finally, the touch screen event.


[Java] float downx = 0;
Float downy = 0;
Float upx = 0;
Float upy = 0;
 
// Touch screen events
@ Override
Public boolean onTouch (View v, MotionEvent event ){
 
Int action = event. getAction ();
 
Switch (action ){
Case MotionEvent. ACTION_DOWN:
 
Downx = event. getX ();
Downy = event. getY ();
 
Break;
 
Case MotionEvent. ACTION_MOVE:
 
Upx = event. getX ();
Upy = event. getY ();
 
// Draw lines
Canvas. drawLine (downx, downy, upx, upy, paint );
// Refresh the ImageView
ChoosenImageView. invalidate ();
 
Downx = upx;
Downy = upy;
 
Break;
 
Case MotionEvent. ACTION_UP:
 
Upx = event. getX ();
Upy = event. getY ();
 
Canvas. drawLine (downx, downy, upx, upy, paint );
ChoosenImageView. invalidate ();
 
Break;
 
Case MotionEvent. ACTION_CANCEL:
 
Break;
 
Default:
Break;
}
 
Return true;
}
Float downx = 0;
Float downy = 0;
Float upx = 0;
Float upy = 0;

// Touch screen events
@ Override
Public boolean onTouch (View v, MotionEvent event ){

Int action = event. getAction ();

Switch (action ){
Case MotionEvent. ACTION_DOWN:

Downx = event. getX ();
Downy = event. getY ();

Break;

Case MotionEvent. ACTION_MOVE:

Upx = event. getX ();
Upy = event. getY ();

// Draw lines
Canvas. drawLine (downx, downy, upx, upy, paint );
// Refresh the ImageView
ChoosenImageView. invalidate ();

Downx = upx;
Downy = upy;

Break;

Case MotionEvent. ACTION_UP:

Upx = event. getX ();
Upy = event. getY ();

Canvas. drawLine (downx, downy, upx, upy, paint );
ChoosenImageView. invalidate ();

Break;

Case MotionEvent. ACTION_CANCEL:

Break;

Default:
Break;
}

Return true;
} In fact, there are not many codes. You can basically understand them. If you want to use the source code, please leave a message and take the time to write your own learning code. I hope it will help you.

From the column kangkangz4

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.