ImageView

作者: CP9 | 来源:发表于2017-02-14 15:32 被阅读59次

    Scale Types

    我们可以利用adjustViewBounds参数来保留此宽高比。 但是,我们必须允许高度和/或宽度可调(即通过使用maxWidth并使用wrap_content)。 否则,不能重新调整尺寸以满足所需的宽高比。

    image-scaletype.png

    图像缩放以适合父视图的宽度,并且应该按比例调整高度。

    public class ResizableImageView extends ImageView {
    
        public ResizableImageView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override 
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
             Drawable d = getDrawable();
    
             if(d!=null){
                     // ceil not round - avoid thin vertical gaps along the left/right edges
                     int width = MeasureSpec.getSize(widthMeasureSpec);
                     int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
                     setMeasuredDimension(width, height);
             }else{
                     super.onMeasure(widthMeasureSpec, heightMeasureSpec);
             }
        }
    
    }
    

    支持多种密度的图像

    image-densities.png

    Final Android Resizer

    这个程序允许我们选择一个高密度图像,该工具将自动生成相应的较小尺寸的图像:

    image-resizer.png

    Scaling a Bitmap——[createScaledBitmap]

    不保留宽高比(http://developer.android.com/reference/android/graphics/Bitmap.html#createScaledBitmap(android.graphics.Bitmap,%20int,%20int,%20boolean))

    // Load a bitmap from the drawable folder
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
    // Resize the bitmap to 150x100 (width x height)
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 150, 100, true);
    // Loads the resized Bitmap into an ImageView
    ImageView image = (ImageView) findViewById(R.id.test_image);
    image.setImageBitmap(bMapScaled);
    

    保留宽高比

    public class BitmapScaler
    {
        // Scale and maintain aspect ratio given a desired width
        // BitmapScaler.scaleToFitWidth(bitmap, 100);
        public static Bitmap scaleToFitWidth(Bitmap b, int width)
        {
            float factor = width / (float) b.getWidth();
            return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
        }
    
    
        // Scale and maintain aspect ratio given a desired height
        // BitmapScaler.scaleToFitHeight(bitmap, 100);
        public static Bitmap scaleToFitHeight(Bitmap b, int height)
        {
            float factor = height / (float) b.getHeight();
            return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
        }
    
        // ...
    }
    

    依据屏幕宽高缩放bitmap

    public Bitmap scaleToActualAspectRatio(Bitmap bitmap) {
     if (bitmap != null) {
     boolean flag = true;
     
    int deviceWidth = getWindowManager().getDefaultDisplay()
     .getWidth();
     int deviceHeight = getWindowManager().getDefaultDisplay()
     .getHeight();
     
    int bitmapHeight = bitmap.getHeight(); // 563
     int bitmapWidth = bitmap.getWidth(); // 900
     
    // aSCPECT rATIO IS Always WIDTH x HEIGHT rEMEMMBER 1024 x 768
     
    if (bitmapWidth > deviceWidth) {
     flag = false;
     
    // scale According to WIDTH
     int scaledWidth = deviceWidth;
     int scaledHeight = (scaledWidth * bitmapHeight) / bitmapWidth;
     
    try {
     if (scaledHeight > deviceHeight)
     scaledHeight = deviceHeight;
     
    bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth,
     scaledHeight, true);
     } catch (Exception e) {
     e.printStackTrace();
     }
     }
     
    if (flag) {
     if (bitmapHeight > deviceHeight) {
     // scale According to HEIGHT
     int scaledHeight = deviceHeight;
     int scaledWidth = (scaledHeight * bitmapWidth)
     / bitmapHeight;
     
    try {
     if (scaledWidth > deviceWidth)
     scaledWidth = deviceWidth;
     
    bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth,
     scaledHeight, true);
     } catch (Exception e) {
     e.printStackTrace();
     }
     }
     }
     }
     return bitmap;
     }
    

    注意:对图像进行任何类型的缩放都会导致EXIF元数据丢失,包括相机,旋转,所拍照片的日期/时间等信息。 虽然在复制映像后传输此数据有一些解决方法,但是存在当前的限制。 如果您需要此信息或希望将其上传到某个网站,您应该发送原始文件。

    SVG

    ImageView中使用SVG

    // Parse an SVG resource from the "raw" resource folder
    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
    // Fix issue with renderer on certain devices
    myImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    // Get a drawable from the parsed SVG and set it as the drawable for the ImageView
    myImageView.setImageDrawable(svg.createPictureDrawable());
    

    Canvas中使用SVG

    // Parse the SVG file from the resource 
    SVG svg =SVGParser.getSVGFromResource(getResources(), R.raw.android); 
    // Get the picture 
    Picture picture = svg.getPicture(); 
    // Draw picture in canvas 
    // Note: use transforms such as translate, scale and rotate to position the picture correctly
    canvas.drawPicture(picture); 
    // ...
    

    Bounds和Limits

    // Parse SVG 
    SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android); 
    // Get the artist-specified bounds (will be null if not specified) 
    RectF bounds = svg.getBounds(); 
    // Get the parser-estimated bounds (could also be null, for example if it is a blank SVG) 
    RectF limits = svg.getLimits();
    

    相关文章

      网友评论

          本文标题:ImageView

          本文链接:https://www.haomeiwen.com/subject/jpzzittx.html