Drag an image

Source: Internet
Author: User
Package com. test. time; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. paint; import android. util. attributeSet; import android. util. log; import android. view. motionEvent; import android. view. view; public class MyMapView extends View {Paint paint; float initx = 0; // the initial point of the image painting: float inity = 0; float lastx = 0; // The coordinate float lasty = 0 when the record is pressed; float movex = 0; // float movey = 0; float photoWidth; float photoHeight; float X; // float Y; float screenWidth; float screenHeigth; float drawx; // float drawy; WindowManager wm; Bitmap bt; public MyMapView (Context context, AttributeSet attrs) {super (context, attrs); init (context, attrs);} public MyMapView (Context context) {super (co Ntext); init (context, null);} private void init (Context context, AttributeSet attrs) {paint = new Paint (); wm = (Activity) context ). getWindowManager (); File file = new File ("/sdcard/test.jpg"); FileInputStream FCM; ByteArrayOutputStream output = null; byte [] result; try {FCM = new FileInputStream (file); output = new ByteArrayOutputStream (); byte [] buffer = new byte [1024*100]; int n = 0; while (-1! = (N = FCM. read (buffer) {output. write (buffer, 0, n);} result = output. toByteArray (); bt = BitmapFactory. decodeByteArray (result, 0, result. length);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {} Display display = wm. getdefadisplay display (); screenWidth = display. getWidth (); screenHeigth = display. getHeight (); photoWidth = bt. getWidth (); photoHeight = bt. getHeight (); setClickable (true); setFocusableInTouchMode (true); X = screenWidth-photoWidth; Y = screenHeigth-photoHeight; initx = (screenWidth-photoWidth)/2; inity = (screenHeigth-photoHeight)/2;} @ Overridepublic boolean onTouchEvent (MotionEvent event) {// TODO Auto-generated method stubfloat dx; float dy; switch (event. getAction () {case MotionEvent. ACTION_DOWN: lastx = event. getX (); lasty = event. getY (); break; case MotionEvent. ACTION_MOVE: dx = event. getX ()-lastx; dy = event. getY ()-lasty; movex = dx; movey = dy; invalidate (); break; case MotionEvent. ACTION_UP: if (X <= 0) {initx + = movex; if (initx> 0) initx = 0; if (initx <X) {initx = X ;}} if (Y <= 0) {inity + = movey; if (inity> 0) inity = 0; if (inity <Y) {inity = Y ;}} movex = 0; movey = 0; invalidate (); break;} return super. onTouchEvent (event) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); if (X <= 0 & initx + movex> = 0) {// when X> 0, the screen width is sufficient to show the entire image, do not drag drawx = 0; // slide to the right} else if (X <= 0 & initx + movex <= X) {drawx = X ;} else if (X <= 0) {// slide the drawx = initx + movex;} else {drawx = initx ;} if (Y <= 0 & inity + movey> = 0) {drawy = 0;} else if (Y <= 0 & inity + movey <= Y) {drawy = Y;} else if (Y <= 0) {drawy = inity + movey;} else {drawy = inity;} canvas. drawBitmap (bt, drawx, drawy, paint );}

}

 

 

 

 

}

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.