Examples of use of the Android drag and drop feature

Source: Internet
Author: User

Picture drag function is to handle the picture into a useful and commonly used functions, due to the size of the mobile phone screen, often can not be a one-time display on the phone a larger picture, that is
Said, we can only see part of the picture on the phone at a time, then use the drag function of the picture to drag the picture, and then see the corresponding part of the picture.
Here is an example of how to implement the drag function of the picture:

The instance runs as follows: 1. This initializes the interface. 2. The effect of dragging to the left 3. The effect of dragging up



Knowledge Points:
In anndroid application development, we often use the ScaleType attribute when using ImageView, such as:


<imageview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:scaletype= "Matrix"
android:src= "@drawable/s001"/>


ScaleType attribute values are: Matrix fitxy fitstart fitcenter fitend Center Centercrop centerinside
The differences between them are as follows:
Matrix is drawn with matrices (The matrix region starting from the upper-left corner)
Fitxy the image is not scaled up/down to the view size display (make sure the picture is fully displayed and full of view)
Fitstart enlarges/shrinks the image to the width of the view, which is displayed in the upper part of the view (the picture is displayed in full)
Fitcenter enlarge/Shrink the picture to the width of the view, centered (the picture will be displayed in full)
Fitend enlarge/Shrink the image to the width of the view, displayed in the lower part of the view (the picture will be displayed in full)
Center by the original size of the picture is displayed, when the picture is wider than the width of the view, then the center portion of the image is captured, when the picture is less than the width of the view, the picture is centered
Centercrop proportionally enlarges/shrinks the size of the image so that the image is higher than the view, making the picture wider or larger than the view width
Centerinside the contents of the picture to the full center, so that the picture is scaled down or the original size (picture than view hours) so that the picture width is equal to or less than the width of the view (picture will be full display)

Create a new Android project called Draganddrop, with the following directory structure:


The layout code for the activity_layout.xml of the main interface is as follows:
<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 "    >    <imageview         android:id= "@+id/show_img"        android:layout_width= "Wrap_content"        Android : layout_height= "wrap_content"        android:scaletype= "Matrix"        android:src= "@drawable/s001"        android: Background= "#ff0000"        android:contentdescription= "@string/app_name"/></relativelayout>
Readers need to note that our ImageView control is set to the matrix so that you can easily control the image in the code.
Then, we put a picture (larger than the phone screen) in the drawable-mdpi file.
The code for the main activity is as follows: Mainactivity.java
Package Com.shen.draganddrop;import Android.app.activity;import Android.graphics.matrix;import Android.graphics.pointf;import Android.os.bundle;import Android.view.menu;import Android.view.MenuItem;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.imageview;public class Mainactivity extends Activity {private ImageView ImageView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); ImageView = (ImageView) Findviewbyid (r.id.show_img); Imageview.setontouchlistener (new Imagetouchlistener ());} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up buttOn, so long//as you specify a parent activity in AndroidManifest.xml.int ID = item.getitemid (); if (id = = R.id.action_sett Ings) {return true;} return super.onoptionsitemselected (item);} Private class Imagetouchlistener implements ontouchlistener{//declares a coordinate point private PointF startpoint;// Declare and instantiate a matrix to control the picture private matrix matrix = new Matrix ();//Declare and instantiate the current picture of the matrixprivate matrix Mcurrentmatrix = new Matrix (); @Overridepublic boolean OnTouch (View V, motionevent event) {switch (event.getaction () &motionevent.action_mask) { Case motionevent.action_down://Gets the coordinates of the current pressed point startpoint = new PointF (Event.getx (), event.gety ());// The matrix of the current picture is set to Matrixmcurrentmatrix.set (Imageview.getimagematrix ()) of the slice, break;case motionevent.action_move:// Moving x-coordinate distance of float dx = event.getx ()-startpoint.x;//move the distance of the y-coordinate of float dy = event.gety ()-startpoint.y;// Sets the current Matrixmatrix.set (Mcurrentmatrix) of the Matrix,//tells the matrix the distance matrix.posttranslate (dx, dy) to move the x-axis and the y-axis; break;case Motionevent.action_up:break;default:break;} Move the picture to a location as required by the matrix ImageView. Setimagematrix (matrix);//Returns True to indicate that we will consume the action, do not need the parent control for further processing return true;}} 


The following is the display of the ScaleType property for the same value:

1.fitXY the image is not scaled up/down to the view size display (make sure the picture is fully displayed and full of view)


2.fitStart enlarges/shrinks the image to the width of the view, which is displayed in the upper part of the view (the picture is displayed in full)


3.fitCenter enlarge/Shrink the image to the width of the view, centered (the picture will be displayed in full)


4.fitEnd enlarges/shrinks the image to the width of the view, displayed in the lower part of the view (the picture will be displayed in full)


5.center by the original size of the picture is displayed, when the picture is wider than the width of the view, then the center portion of the image is captured, when the picture is less than the width of the view, the picture is centered


6.centerCrop proportionally enlarges/shrinks the size of the image so that the image is higher than the view, making the picture wider or larger than the view width


7.centerInside Display the contents of the picture in full, so that the picture is scaled down or the original size (the picture is more than the view hour) so that the picture width is equal to or less than the width of the view (the picture will be displayed in full)


In summary, the types of scaletype are divided into three categories, the matrix (default), the Fit-x class, and the center class. The matrix doesn't say much. In the Fit-x class, Fitstart, Fitcenter, and Fitend are designed to adapt the original artwork to Imgview as needed, drawing by matrix, but they differ in their baseline. The base point of the Fitstart is the top-most point (that is, the point at which the matrix starts) Fitcenter The point of the middle (the point in matrix mode where the picture can be centered), and the Fitend datum point is the point in the lower-right corner (that is, the matrix is the last draw point). In the center class, center, Centercrop, and Centerinside are all based on the geometric center point of the original image and the geometric center point of the Imagview, and only the Imagview-sized images are plotted, and the difference is whether the original size and the drawing target are different. The means adopted are different.


Program source code Download


Participation Website:

http://blog.csdn.net/xilibi2003/article/details/6628668

Examples of use of the Android drag and drop feature

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.