Experience of android ImageView

Source: Internet
Author: User
Tags gety

I have encountered some problems in the display of images recently.
1)You can use ImageSwicher and ImageView in combination, so that setfecloud () is used.
The worst thing is that if the image size exceeds the screen size, it is difficult to implement. Currently, no method is available.

2)The simplest method is to use ImageView. The image is FIT_CENTER. android automatically adjusts the image size.
Maintain the image proportion. If the image resolution exceeds the screen, android will automatically adjust it to the screen to put down the entire picture.
You can use the SetFrame () and setScale () Methods of ImageView to enlarge an image.
When the screen is exceeded, the principle is to enlarge the ImageView, and the image is zoomed in. You can also add various animations.
It is roughly as follows:

Copy codeThe Code is as follows: Animation animation = AnimationUtils. loadAnimation (Main. this, R. anim. my_scale_action );
ImageView. setLayoutParams (new Gallery. LayoutParams (206,206 ));
ImageView. startAnimation (animation );

Write your own MyImageView class, the Code is as follows, you can directly useCopy codeThe Code is as follows: package com. practice. imageviewpic;

Import android. app. Activity;
Import android. content. Context;
Import android. graphics .*;
Import android. graphics. drawable. BitmapDrawable;
Import android. OS. Bundle;
Import android. util. AttributeSet;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. view. View;
Import android. view. ViewGroup. LayoutParams;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. ImageView. ScaleType;
Import android. widget. LinearLayout;
// Create your own ImageView class
Class MyImageView extends ImageView {
Private float scale = 0.1f;

// The length between two touch screens
Private float beforeLenght;
Private float afterLenght;

// The coordinate value before and after a single point of movement
Private float afterX, afterY;
Private float beforeX, beforeY;

Public MyImageView (Context context ){
Super (context );
}
Public MyImageView (Context context, AttributeSet attrs ){
This (context, attrs, 0 );

}
Public MyImageView (Context context, AttributeSet attrs, int defStyle ){
Super (context, attrs, defStyle );
}
// Used to set the location of the ImageView
Private void setLocation (int x, int y ){
This. setFrame (this. getLeft () + x, this. getTop () + y, this. getRight () + x, this. getBottom () + y );
}

/*
* Used to zoom in and out the ImageView
* Because the image is filled with ImageView, the image can be zoomed in or out.
* If the flag is 0, the image is enlarged. If the flag is 1, the image size is smaller than the image size.
*/
Public void setScale (float temp, int flag ){

If (flag = 0 ){
This. setFrame (this. getLeft ()-(int) (temp * this. getWidth ()),
This. getTop ()-(int) (temp * this. getHeight ()),
This. getRight () + (int) (temp * this. getWidth ()),
This. getBottom () + (int) (temp * this. getHeight ()));
} Else {
This. setFrame (this. getLeft () + (int) (temp * this. getWidth ()),
This. getTop () + (int) (temp * this. getHeight ()),
This. getRight ()-(int) (temp * this. getWidth ()),
This. getBottom ()-(int) (temp * this. getHeight ()));
}
}

// Draw a border
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Rect rec = canvas. getClipBounds ();
Rec. left ++;
Rec. top ++;
Rec. bottom --;
Rec. right --;
Paint paint = new Paint ();
Paint. setColor (Color. RED );
Paint. setStyle (Paint. Style. STROKE );
Canvas. drawRect (rec, paint );
}

/* Move the image following the touch screen of your finger
* BeforeX and Y are used to save the coordinates of the previous position.
* AfterX and Y are coordinates used to save the current position.
* Their difference is the increase or decrease of the ImageView coordinates.
*/
Public void moveWithFinger (MotionEvent event ){

Switch (event. getAction ()){

Case MotionEvent. ACTION_DOWN:
// Log. d (TAG, "down ..");
BeforeX = event. getX ();
BeforeY = event. getY ();
Break;
Case MotionEvent. ACTION_MOVE:

// Log. d (TAG, "move ..");
AfterX = event. getX ();
AfterY = event. getY ();

This. setLocation (int) (afterX-beforeX), (int) (afterY-beforeY ));

BeforeX = afterX;
BeforeY = afterY;
Break;

Case MotionEvent. ACTION_UP:
// Log. d (TAG, "up ..");
Break;
}
}

/*
* Use a multi-point touch screen to zoom in or out the image
* BeforeLenght is used to save the distance between two points in the previous time.
* AfterLenght is used to save the distance between two points of the current time.
*/
Public void scaleWithFinger (MotionEvent event ){
Float moveX = event. getX (1)-event. getX (0 );
Float moveY = event. getY (1)-event. getY (0 );

Switch (event. getAction ()){
Case MotionEvent. ACTION_DOWN:
BeforeLenght = (float) Math. sqrt (moveX * moveX) + (moveY * moveY ));
Break;
Case MotionEvent. ACTION_MOVE:
// Obtain the length between two points
AfterLenght = (float) Math. sqrt (moveX * moveX) + (moveY * moveY ));

Float gapLenght = afterLenght-beforeLenght;

If (gapLenght = 0 ){
Break;
}

// If the distance between the two points in the current time is greater than the distance between the two points in the previous time, 0 is passed; otherwise, 1 is passed.
If (gapLenght> 0 ){
This. setScale (scale, 0 );
} Else {
This. setScale (scale, 1 );
}

BeforeLenght = afterLenght;
Break;
}
}

// Monitors the screen touch time.
@ Override
Public boolean onTouchEvent (MotionEvent event ){

/*
* Determine whether the user has touched the image
* If it is a single touch, the method of controlling image movement is called.
* If it is a 2-point touch, the method of controlling the image size is called.
*/
If (event. getY ()> this. getTop () & event. getY () <this. getBottom ()
& Event. getX ()> this. getLeft () & event. getX () <this. getRight ()){
If (event. getPointerCount () = 2 ){
This. scaleWithFinger (event );
} Else if (event. getPointerCount () = 1 ){
This. moveWithFinger (event );
}
}
Return true;
}

}

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.