本文介绍了缩放图像与截击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图伸展我跟凌空加载图像。 XML是没有太大的帮助,同时也可以缩小他们来说,这并不能帮助他们扩大。我的话题探索使我这个只能通过编程实现的结论。

I am trying to stretch images that I load with volley. XML isn't much help, while it can shrink them it doesn't help enlarging them. My exploration of the topic led me to the conclusion that this can be achieved only programmatically.

什么是我的主意?

要延长凌空库和方法压倒一切的,下载后显示它之前正确的调整位图。

To extend the volley library and overriding one of the methods, resizing the bitmap right after download and before displaying it.

我用这个code来调整那些已经在手机上的图像,但是这并不能帮助我很多从网络随机图像。

I used this code to resize images that were already on the phone, but this doesn't help me much with random images from the internet.

        Point outSize=new Point();
        float  destWidth=1;
        float  destHeight=1;

        @SuppressWarnings("deprecation")
        @TargetApi(13)
        private Bitmap scaleBitmap(int mFile){
            Display display = getActivity().getWindowManager().getDefaultDisplay();
            if (android.os.Build.VERSION.SDK_INT >= 13){
                display.getSize(outSize);
                destWidth = outSize.x;
                destHeight = outSize.y;
            }else{
                destWidth=display.getWidth();
                destWidth=display.getHeight();
            }

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            Bitmap orig = BitmapFactory.decodeResource(getResources(), mFile);
            float srcWidth = orig.getWidth();
            float srcHeight = orig.getHeight();
            float inSampleSize = 1;
            if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
                inSampleSize=(destHeight/2)/srcHeight;
            }else{
            inSampleSize=(destHeight/4)/srcHeight;}
            options = new BitmapFactory.Options();
            Bitmap resized=Bitmap.createScaledBitmap(orig, (int)Math.round(orig.getWidth()*inSampleSize), (int)Math.round(orig.getHeight()*inSampleSize), true);
            destWidth=1;
            destHeight=1;
            return resized;
        }

基本上我想分配到原稿被下载的图像,然后调整其大小,然后显示它。
我的问题是:我什么扩展类?我接过来一看有,但因为我没有经验的我无法弄清楚到底去寻找。它是ImageLoader的?更具体地说:我应该重写getBitmap()方法,并添加code的编辑版本缩放?

Basically I want to assign to orig the image that is downloaded, then resize it and then display it.My question is: What class do I extend ? I took a look there but since I am inexperienced I couldn't figure out what exactly to look for. Is it ImageLoader ? And more specifically: should i Override the getBitmap() method and add an edited version of the code for scaling ?

免责声明:我很没有经验,我会接受过其他的想法。

Disclaimer: I am very inexperienced and I would accept other ideas too.

推荐答案

我解决了前几天。下面是正规的ImageView处理图像的调整:如果您正在使用Match_parent宽度和WRAP_CONTENT身高(我与两个测试作为match_parent,结果都是一样的),它不会缩放图片,如果你的图片的高度较低比宽度(这是相当典型的)。这意味着基于较小的一面,图像视图比例。这意味着,有2的解决方案:

I solved it a few days ago. Here is how the regular ImageView handles resizing of images: If you are using Match_parent as width and wrap_content as height(I tested with both as match_parent and the results were the same) it will NOT scale the image IF the Height of your picture is lower than the width (which is quite typical) . Meaning that image view scales based on the smaller side. This meant that there are 2 solutions:


  • 请了我想做的事本来这涉及到编写扩展的ImageView / networkimageview然后以编程方式添加code,我列出的放大图片的新类。

  • 或者干脆编程方式指定的观点所期望的高度,然后ImageView的将简单地缩放图片以适应新的视图的高度。此选项为无效的宽度和WRAP_CONTENT简单地把尽可能多的,因为你需要。

下面是code我用:

 Display display = getActivity().getWindowManager().getDefaultDisplay();
 Point outSize=new Point();
 int destWidth;
 if (android.os.Build.VERSION.SDK_INT >= 13){
    display.getSize(outSize);
    destWidth = outSize.x;
 }else{
    destWidth=display.getWidth();
 }
 img.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT,destWidth*5625/10000));

在我来说,我需要适应640x360的图像,所以我用的0,5625的比率。此方法将工作,如果你不知道图片的参数,但你必须事先得到的参数,并把他们在的地方,所以我想这可能是使用第一种策略更实用,但因为我解决我的问题我不知道对第一种方法的例子。

In my case I needed to fit a 640x360 image so I used a ratio of 0,5625. This method will work if you do not know the picture's parameters BUT you have to get the parameters beforehand and put them in place, so I guess it might be more useful to use the first strategy, but since I solved my problem I don't have an example for the first method.

这篇关于缩放图像与截击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 16:01