ScaleType Setup Diagram
graphics and text to match very clearly see the effect of each property,
Feel center_crop more useful, long width automatically adapt to ImageView, the entire picture automatically abbreviated fill the entire area and center display (width is not necessarily the view size), previously used in the Web page JS, in
Android was still trying to use Java to do this on its own. It's too much trouble, but it's a lot easier with the system.
ImageView's scaletype determines how the picture is displayed on view, such as what scale is scaled, whether the picture is displayed as a whole or part, and so on.
The ways you set up include:
1. Define android:scaletype= "CENTER" in Layout xml
2. or call Imageview.setscaletype (ImageView.ScaleType.CENTER) in the code;
Next, the value of the ScaleType and the corresponding display effect in the most intuitive way-Real diagram demo method, to explain.
First, the original picture used for the test:
(DIMENSIONS:128 * 128)
(dimensions:640 * 428)
OK, start the following test:
1.
Setscaletype (ImageView.ScaleType.CENTER);
Centered by the original size of the picture, when the picture is longer/wider than the view's length/width, the center portion of the picture is displayed
2.
Setscaletype (ImageView.ScaleType.CENTER_CROP);
Proportionally enlarge the size of the picture to be centered so that the picture is long (wide) equal to or greater than the view's length (width)
3.
Setscaletype (ImageView.ScaleType.CENTER_INSIDE);
Center the contents of the picture completely, by scaling or the original size to make the picture long/wide equal to or less than the length/width of the view
4.
Setscaletype (ImageView.ScaleType.FIT_CENTER);
Enlarge/Reduce the picture to the width of view, centered
5. Fit_start, fit_end in the picture scaling effect and fit_center, just show the position is different, Fit_start is placed at the top, fit_center centered, Fit_end placed at the bottom.
No example will be shown here.
6. Fit_xy
Do not scale the picture proportionally, the goal is to fill the picture with the entire view.
Achieve picture width 100%imageview width and height scaling automatically
in order to achieve the picture width of listview 100% imageview container width, so that the height of automatic scaling function, check a lot of information, did not find a ready-made afternoon, but seems to have a conclusion, is:
Android can't do this with its own hands and clothing
Use the ScaleType property, set the picture in view of the scaling way, originally thought Fit_center can be achieved, the result is very silent, inexplicable, failed to achieve width fitting container width, both sides are blank areas.
Workaround:
1. Or to take advantage of the scaletype center_crop effect, so that the picture can always be proportional to fill the container, without leaving a blank area.
2. The remaining main function is to dynamically set the height of the ImageView container, so that the picture can be as complete as possible, but also can achieve maxheight effect, if the picture height ratio is too large to pull the list too long.
int maxheight = zui.dp2px (mcontext, MB);
int height = (int) ((float) view.getwidth ()/drawable.getminimumwidth () * drawable.getminimumheight ());
if (height > maxheight) height = maxheight;
View.setlayoutparams (New Relativelayout.layoutparams (RelativeLayout.LayoutParams.MATCH_PARENT, height));
Where view is the ImageView Picture View container object, drawable is the picture resource to display.
Drawable.getminimumwidth () and Drawable.getminimumheight () are the visual height width of the picture on the screen, not necessarily the original high width of the picture, which is related to the pixel density of the screen, which is probably the case. Note that you want to use a floating-point numeric type, or you might get the integer to be a multiple of 1.
Converts a DP value to a pixel value, because the width of the operation in the Java code are pixel values, and the style is a dip to compatible the screen
public static int dp2px (context context, int dp)
{
float scale = context.getresources (). Getdisplaymetrics (). Density;
return (int) (DP * scale + 0.5f);
}
OK, that's the way it's done, and get a test result chart.
In fact, Center_crop can easily achieve this function, but he will not automatically expand the height, he should use the picture's visual height for the container height and then the container coverage display, that is, the container height will not be proportional to increase, resulting in almost half of the picture is not shown, if the picture is relatively small, It is almost impossible to use, or must be dynamic adjustment, if the picture size is relatively large, especially highly suitable words can be used directly