High imitation Sina click to enlarge the image (you can drag, dynamic zoom, and click again to disappear and save the image function), high imitation drag

Source: Internet
Author: User

High imitation Sina click to enlarge the image (you can drag, dynamic zoom, and click again to disappear and save the image function), high imitation drag

There is a picture with the truth:

Recently, when I used the Click image to enlarge the project, I started to implement it. I thought it was quite simple and I still encountered a lot of small problems:

1: Only click the image to zoom in and click the image to disappear again;

Second: only images can be dragged, and images can be dynamically zoomed in or out;

Third: the first and second synchronization implementations have problems:

The specific problem is:

(1) When setOnClickListener and setOnTouchListener are set at the same time, if setOnTouchListener returns true, setOnClickListener is not executed. If the return value is false, setOnClickListener is executed.

(2) After setting the setOnTouchListener return value to false, run the project. The project disappears after the image is zoomed in and enlarged, that is, the setOnClickListener is executed directly, but the project requirement is, the image is moved or zoomed in or out, and the image is still displayed. The image disappears only when you click it, so I used a variable to control the finger and clicked the image when the screen size is smaller than 1, so that the image disappears. I will not talk about it much, but see the image and code:


Public class extends Activity extends {private String mImageUrl; private ImageWorker mImageWorker; private int mImageWidth; private int mImageHeight; private ImageView mFullImageView; private LinearLayout mImageLayout; private ImageView mSaveImage; private Boolean isShutDownPic = true; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (sa VedInstanceState); setContentView (R. layout. image_to_full_screen); mImageUrl = getIntent (). getStringExtra ("imageUrl"); init ();} private void init () {mImageLayout = (LinearLayout) findViewById (R. id. image_layout); mFullImageView = (ImageView) findViewById (R. id. full_image); mSaveImage = (ImageView) findViewById (R. id. save_image); mSaveImage. setOnClickListener (this); mImageLayout. setOnClickListener (this); mFullI MageView. setOnClickListener (this); mFullImageView. setOnTouchListener (new TouchListener (); WindowManager wm = (WindowManager) getSystemService (Context. WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics (); wm. getdefadisplay display (). getMetrics (outMetrics); mImageWidth = (int) (outMetrics. widthPixels * 1.0d); mImageHeight = mImageWidth; ImageCacheParams cacheParams = new ImageCacheParams (); cachePa Rams. loadingResId = R. drawable. image_full_screen; cacheParams. memCacheSize = ImageCache. DEFAULT_MEM_CACHE_SIZE_BIG; cacheParams. reqWidth = mImageWidth; cacheParams. reqHeight = mImageHeight; mImageWorker = new ImageWorker (this, (HealthApplication) getApplication (); mImageWorker. addParams (cacheParams); mImageWorker. loadBitmap (mImageUrl, mFullImageView); LayoutParams params = new LayoutParams (LayoutParams. F ILL_PARENT, LayoutParams. FILL_PARENT); mFullImageView. setLayoutParams (params) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. save_image: SaveImageToSysAlbum (); break; case R. id. image_layout: if (isShutDownPic) {ImageToFullScreenActivity. this. finish () ;} isShutDownPic = true; break; case R. id. full_image: if (isShutDownPic) {ImageToFullScreenActivity. this. fin Ish ();} isShutDownPic = true; break; default: break;}/*** Save the ghost slices in the ImageView to the system album */private void SaveImageToSysAlbum () {if (FileUtil. isSdCardExist () {BitmapDrawable BMP drawable = (BitmapDrawable) mFullImageView. getDrawable (); Bitmap bmp = bmp drawable. getBitmap (); if (bmp! = Null) {try {/** ContentResolver cr = getContentResolver (); String url = * MediaStore. images. media. insertImage (cr, bmp, * String. valueOf (System. currentTimeMillis (), ""); */File tempFile = new File (Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_DCIM ). getPath () + "/" + String. valueOf (System. currentTimeMillis () + ". png "); if (tempFile. exists () {tempFile. delete ();} try {tempFile. createNewFile ();} catch (IOException e) {e. printStackTrace ();} FileOutputStream fOut = null; try {fOut = new FileOutputStream (tempFile);} catch (FileNotFoundException e) {e. printStackTrace ();} bmp. compress (Bitmap. compressFormat.. PNG, 100, fOut); try {fOut. flush (); fOut. close ();} catch (IOException e) {// TODO: handle finished tione. printStackTrace ();} Toast. makeText (this, getString (R. string. save_succ), Toast. LENGTH_SHORT ). show ();} catch (Exception e) {e. printStackTrace () ;}} else {Toast. makeText (this, getString (R. string. no_iamge_save_fail), Toast. LENGTH_SHORT ). show () ;}} else {Toast. makeText (this, getString (R. string. no_sdcard_save_fail), Toast. LENGTH_SHORT ). show () ;}string release = android. OS. build. VERSION. RELEASE; String tempID = release. substring (0, 3); if (Double. parseDouble (tempID)> = 4.4) {// This is used for Android 4.4 and later versions. The following uses MediaScannerConnection in the else statement. scanFile (this, new String [] {Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_DCIM ). getPath () + "/"}, null, null);} else {sendBroadcast (new Intent (Intent. ACTION_MEDIA_MOUNTED, Uri. parse ("file: //" + Environment. getExternalStorageDirectory (); MediaScannerConnection. scanFile (this, new String [] {Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_DCIM ). getPath () + "/"}, null, null) ;}} private class TouchListener implements OnTouchListener {private PointF startPoint = new PointF (); private Matrix matrix = new Matrix (); private Matrix currentMaritx = new Matrix (); private int mode = 0; // used to mark mode private static final int DRAG = 1; // DRAG private static final int ZOOM = 2; // enlarge private float startDis = 0; private PointF midPoint; // center point @ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () & MotionEvent. ACTION_MASK) {case MotionEvent. ACTION_DOWN: mode = DRAG; // DRAG currentMaritx. set (mFullImageView. getImageMatrix (); // record the current position startPoint of the ImageView. set (event. getX (), event. getY (); // start point break; case MotionEvent. ACTION_MOVE: // move event float tempX = event. getX ()-startPoint. x; float tempY = event. getY ()-startPoint. y;/*** when the movement is controlled to be less than 1.0, the Click Event */if (Math. abs (tempX) <1.0 & Math. abs (tempY) <1.0) {isShutDownPic = true;} else {isShutDownPic = false;} mFullImageView. setScaleType (ImageView. scaleType. MATRIX); if (mode = DRAG) {// image DRAG event float dx = event. getX ()-startPoint. x; // float dy = event. getY ()-startPoint. y; matrix. set (currentMaritx); // move the matrix based on the current position. postTranslate (dx, dy);} else if (mode = ZOOM) {// float endDis = distance (event); // if (endDis> 10f) {float scale = endDis/startDis; // magnification matrix. set (currentMaritx); matrix. postScale (scale, scale, midPoint. x, midPoint. y) ;}} break; case MotionEvent. ACTION_UP: mode = 0; break; // exit the screen with a finger, but the screen also has a contact (finger) case MotionEvent. ACTION_POINTER_UP: mode = 0; break; // when there are contacts (fingers) on the screen, there is another finger pressing the screen case MotionEvent. ACTION_POINTER_DOWN: mode = ZOOM; startDis = distance (event); if (startDis> 10f) {// avoid two midPoint = mid (event) on your finger; currentMaritx. set (mFullImageView. getImageMatrix (); // record the current zoom factor} break;} // display the scaled image mFullImageView. setImageMatrix (matrix); return false ;}/ *** calculate the distance between two points ** @ param event * @ return */public static float distance (MotionEvent event) {float dx = event. getX (1)-event. getX (0); float dy = event. getY (1)-event. getY (0); return FloatMath. sqrt (dx * dx + dy * dy);}/*** calculate the intermediate point between two points ** @ param event * @ return */public static PointF mid (MotionEvent event) {float midX = (event. getX (1) + event. getX (0)/2; float midY = (event. getY (1) + event. getY (0)/2; return new PointF (midX, midY );}}


If you have a better implementation method, please leave a link for everyone to learn from each other!


Can I zoom in and out an image with two fingers on the four generations of high imitation apple?

Since it is a high imitation, some functions certainly do not exist.
 
How can I set the photo to zoom in and out with a high imitation Apple 4? Tell me what the experts know ·

The puller size is not a problem of setting. It is related to the touch screen of the mobile phone. It can be split into capacitive screen and Resistive screen. The size of the capacitive screen can be pulled. However, the price of this capacitive screen iPhone 4 is around 800.

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.