Android project notepad (14), android Project
This article is my learning notes, welcome to reprint, but please note the Source: http://blog.csdn.net/jesson20121020
In the previous section, the image viewing and recording functions are implemented. To view images, you can call the image library of the system to view images or customize the Activity to view images, scale and drag images with gestures.
You must have used the image library of the system. You can zoom in or out the image by using gestures, rotate the image, and drag and drop the image, we also added the gesture scaling and drag-and-drop functions for our custom Activity for image viewing. The effects are as follows:
The above four figures show how to zoom in, zoom in, and drag an image by means of gestures (multi-touch.
Multi-touch is used here, so we need to know the difference between multi-touch and single-point.
Single finger operation process: ACTION_DOWN-ACTION_MOVE-ACTIOIN_UP
Multi finger operation process: ACTION_DOWN-ACTION_POINTER_DOWN-ACTION_MOVE-ACTION_POINTER_UP-ACTION_UP
Generally, the Matrix postScale method is used to scale images. Therefore, it is no exception to scale images through gestures (multiple points, the difference is that you can use the sliding fingers to determine the scaling ratio and center position. The specific method is as follows:
Steps for scaling images with gestures:
1. Set the scaleType attribute of ImageView to matrix.
Matrix is used to scale images. Therefore, this attribute is required. You can set ImageView settings in xml, android: scaleType = "matrix", or set imageView in code. setScaleType (ScaleType. matrix)
2. Bind a touch listener to the ImageView
// Touch event img. setOnTouchListener (new TouchEvent ());
3. Implement Multi-finger scaling and drag and drop images in a touch event
This is the core of this Section, mainly to determine the MotionEvent type, whether it is a single finger or multiple fingers, you can use the event. getActionMasked () is obtained, and three logos are set up to determine whether the current operation is dragging, scaling, or not. If it is dragging, You need to record the start position and end position of the finger, then, the Matrix postTranslate method is used to move the image. For scaling, you must first calculate the scale and position of the image. When calculating the scale, you must first know the diameter of the Multi-finger movement, you can zoom in and out by moving multiple fingers. to calculate the zoom position of an image, you only need to calculate the midpoint before and after the pull.
4. Adjust the scaling ratio
In fact, you can use gestures to control the scaling and movement of images in the first three steps, but you will find that images can be enlarged infinitely or infinitely small, in addition, whether the image is in or out of the zoom state, it will move to any place as you move your finger. This is obviously not suitable for actual use, so we need to control the scaling ratio of the image. The main code is as follows:
// Control the scaling ratio private void controlScale () {float values [] = new float [9]; matrix. getValues (values); if (mode = ZOOM) {if (values [0] <MINSCALER) matrix. setScale (MINSCALER, MINSCALER); else if (values [0]> MAXSCALER) matrix. setScale (MAXSCALER, MAXSCALER );}}
5. Set the image center display
You can control the scaling ratio of the image in step 1. In this way, the image will have a maximum and minimum bloom ratio. When the calculated scaling ratio is smaller than the minimum scaling ratio, the current scaling ratio is set to the minimum scaling ratio. When the scaling ratio is greater than the maximum, the scaling ratio of the current image is set to the maximum.
However, there is another problem, that is, whether the image is enlarged or reduced, it will move with your fingers, but in the actual process, we usually need to zoom in and out the image at the center of the control, that is, to set the image center display, the Code is as follows:
// Automatically center the left and right sides, and both the left and right sides of the center. protected void center () {center (true, true);} private void center (boolean horizontal, boolean vertical) {Matrix m = new Matrix (); m. set (matrix); RectF rect = new RectF (0, 0, bm. getWidth (), bm. getHeight (); m. mapRect (rect); float height = rect. height (); float width = rect. width (); float deltaX = 0, deltaY = 0; if (vertical) {int screenHeight = dm. heightPixels; // cell phone screen resolution height // int screenHeight = 400; if (height <screenHeight) {deltaY = (screenHeight-height)/2-rect. top;} else if (rect. top> 0) {deltaY =-rect. top;} else if (rect. bottom <screenHeight) {deltaY = screenHeight-rect. bottom ;}}if (horizontal) {int screenWidth = dm. widthPixels; // phone screen resolution width // int screenWidth = 400; if (width <screenWidth) {deltaX = (screenWidth-width)/2-rect. left;} else if (rect. left> 0) {deltaX =-rect. left;} else if (rect. right <screenWidth) {deltaX = screenWidth-rect. right ;}} matrix. postTranslate (deltaX, deltaY );}
After the center setting, when the screen is not full, the image cannot be moved to another location, only when the image is larger than the screen, you can also move an image to view its location.
Basically, through these five steps, the image gesture (multi-point) scaling function can be realized, and the scaling ratio and center display of the image can also be controlled. The entire code is provided below:
Public class ShowPicture extends Activity {private ImageView img; private Bitmap bm; private DisplayMetrics dm; private Matrix matrix = new Matrix (); private Matrix savedMatrix = new Matrix (); private PointF mid = new PointF (); private PointF start = new PointF (); private static int DRAG = 2; private static int ZOOM = 1; private static int NONE = 0; private int mode = 0; private float oldDist = 1f; private static float MINSCALER = 0.3f; private static float MAXSCALER = 3.0f; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_CUSTOM_TITLE); setContentView (R. layout. activity_show_picture); getWindow (). setFeatureInt (Window. FEATURE_CUSTOM_TITLE, R. layout. title_add); // set the title TextView TV _title = (TextView) findViewById (R. id. TV _title); TV _title.setText ("view image"); Button bt_back = (Button) findViewById (R. id. bt_back); bt_back.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {ShowPicture. this. finish () ;}}); Button bt_del = (Button) findViewById (R. id. bt_save); bt_del.setBackgroundResource (R. drawable. paint_icon_delete); dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); // get the resolution img = (ImageView) findViewById (R. id. iv_showPic); Intent intent = this. getIntent (); String imgPath = intent. getStringExtra ("imgPath"); bm = BitmapFactory. decodeFile (imgPath); // you can specify to center the savedMatrix. setTranslate (dm. widthPixels-bm. getWidth ()/2, (dm. heightPixels-bm. getHeight ()/2); img. setImageMatrix (savedMatrix); // bind the image to img. setImageBitmap (bm); // touch the event img. setOnTouchListener (new TouchEvent ();} // Add a touch event to zoom in and out the class TouchEvent implements OnTouchListener {@ Overridepublic boolean onTouch (View view, MotionEvent) {switch (event. getActionMasked () {// click touch to drag the case MotionEvent. ACTION_DOWN: matrix. set (img. getImageMatrix (); savedMatrix. set (matrix); start. set (event. getX (), event. getY (); mode = DRAG; break; // multi-touch, case MotionEvent when pressed. ACTION_POINTER_DOWN: oldDist = getSpacing (event); savedMatrix. set (matrix); getMidPoint (mid, event); mode = ZOOM; break; // multi-touch, case MotionEvent when lifted. ACTION_POINTER_UP: mode = NONE; break; case MotionEvent. ACTION_MOVE: if (mode = DRAG) {matrix. set (savedMatrix); matrix. postTranslate (event. getX ()-start. x, event. getY ()-start. y);} // scale else if (mode = ZOOM) {// obtain the diameter of the Multi-finger movement. if it is greater than 10, the scaling gesture float newDist = getSpacing (event); if (newDist> 10) {matrix. set (savedMatrix); float scale = newDist/oldDist; matrix. postScale (scale, scale, mid. x, mid. y) ;}} break;} img. setImageMatrix (matrix); controlScale (); // setCenter (); center (); return true ;}// calculate the distance from private float getSpacing (MotionEvent event) {float x = event. getX (0)-event. getX (1); float y = event. getY (0)-event. getY (1); return FloatMath. sqrt (x * x + y * y);} // calculate the point private void getMidPoint (PointF mid, MotionEvent event) {float x = event. getX (0) + event. getX (1); float y = event. getY (0) + event. getY (1); mid. set (x/2, y/2);} // control the scaling ratio private void controlScale () {float values [] = new float [9]; matrix. getValues (values); if (mode = ZOOM) {if (values [0] <MINSCALER) matrix. setScale (MINSCALER, MINSCALER); else if (values [0]> MAXSCALER) matrix. setScale (MAXSCALER, MAXSCALER) ;}// automatically center the left and right sides and both the up and down sides. protected void center () {center (true, true);} private void center (boolean horizontal, boolean vertical) {Matrix m = new Matrix (); m. set (matrix); RectF rect = new RectF (0, 0, bm. getWidth (), bm. getHeight (); m. mapRect (rect); float height = rect. height (); float width = rect. width (); float deltaX = 0, deltaY = 0; if (vertical) {int screenHeight = dm. heightPixels; // cell phone screen resolution height // int screenHeight = 400; if (height <screenHeight) {deltaY = (screenHeight-height)/2-rect. top;} else if (rect. top> 0) {deltaY =-rect. top;} else if (rect. bottom <screenHeight) {deltaY = screenHeight-rect. bottom ;}}if (horizontal) {int screenWidth = dm. widthPixels; // phone screen resolution width // int screenWidth = 400; if (width <screenWidth) {deltaX = (screenWidth-width)/2-rect. left;} else if (rect. left> 0) {deltaX =-rect. left;} else if (rect. right <screenWidth) {deltaX = screenWidth-rect. right ;}} matrix. postTranslate (deltaX, deltaY );}}
Note:
Another problem is that the image is initially centered. In ImageView, you can set the attribute android: scaleType = "fitCenter" to center the image, however, to scale the image with a gesture, you must set this attribute to android: scaleType = "matrix". However, at the same time, this poses a problem, the image cannot be centered.
I have found a solution on the Internet. The first method is to set the scaleType attribute of ImageVIew to fitCenter in the XML file, and then set setScaleType (ScaleType) before the setOnTouchListener () method of ImageVIew. MATRIX); but this method is not successful, and the image is still in the upper left corner. Another method is to calculate the height and width of the mobile phone screen and find the point in the center of the image (x, y), and then use the translation of the Matrix to reach this position. Finally, use setImageMatrix () to bind the Matrix to the ImageView to realize the initial center display of the image. The second method is used at the end. The Code is as follows:
Private DisplayMetrics dm ;...... dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); // get the resolution ...... // set savedMatrix to center. setTranslate (dm. widthPixels-bm. getWidth ()/2, (dm. heightPixels-bm. getHeight ()/2); img. setImageMatrix (savedMatrix );
If you have a better way to center the image at the beginning, please share it with us.
Who will be responsible for the Android notepad project? We are going to take the Android course. We need to prepare an Android notepad project at the end of the course.
Android Notepad program source code
Blog.csdn.net/..096216
Android notepad software download
There are a lot of Android mobile phone notepad software. Which of the following Landlords prefer to use it on their own. I know several easy-to-use notepad software and recommend it to the landlord.
Some software can be downloaded in [Android park]. You only need to enter the notepad to find it.
[Handwritten notepad]
HandCalendar is a notebook, notebook, and notebook that supports handwriting. It also supports saving recorded content to the calendar. The handwriting function of the software is relatively powerful. It supports 10 colors and 3 pen styles, and supports a maximum of three erasers to cancel the function.
[AK Notepad]
AK notepad is a powerful text document editing software. In addition to editing, reading, and writing documents and viewing TXT documents, the software also allows you to share event reminders with others through text messages, emails, and mobile phone alarms.
[With handwriting]
Hand-writing is definitely the most convenient convenience software, and the original smart scaling. Supports both handwriting (Original Handwriting) and keyboard input. Features: 1. You can remember big things on a small screen and use the original handwriting keyboard mixed-mode.
2. task management, notes, calendar, alarm clock, MMS, and schedule 3. Free graffiti, illustrations, photo notes, and voice notes on photos 4. widgets read the current notebook directly. Agree with 14 | comment