How to upscale an image without it becoming blurry

て烟熏妆下的殇ゞ 提交于 2019-12-08 02:49:48

问题


I have an ImageView that is 32x32. Its a sprite basically. But when I go to upscale the image, it blurs like this:

But I want it to scale up in the application like this:

This is the code I have tried, but it doesn't work. I want the final image to be put on an ImageView.

@Override
        public View getView(int i, View view, ViewGroup viewGroup)
        {
            View v = view;
            ImageView picture;
            TextView name;

            if(v == null)
            {
               v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
               v.setTag(R.id.picture, v.findViewById(R.id.picture));
               v.setTag(R.id.text, v.findViewById(R.id.text));
            }

            picture = (ImageView)v.getTag(R.id.picture);
            name = (TextView)v.getTag(R.id.text);

            Item item = (Item)getItem(i);
            Options options = new BitmapFactory.Options();
            options.inDither = false; //I THOUGHT THAT TURNING THIS TO FALSE WOULD MAKE IT NOT BLUR, BUT IT STILL BLURRED
            options.inScaled = false;
            Bitmap source = BitmapFactory.decodeResource(getResources(), item.drawableId, options); //THIS IS THE BITMAP. THE RESOURCE IS THE ID OF THE DRAWABLE WHICH IS IN AN ARRAY IN ANOTHER PLACE IN THIS FILE
            picture.setImageBitmap(source); //THIS SETS THE IMAGEVIEW AS THE BITMAP DEFINED ABOVE
            name.setText(item.name);

            return v;
        }

        private class Item
        {
            final String name;
            final int drawableId;

            Item(String name, int drawableId)
            {
                this.name = name;
                this.drawableId = drawableId;
            }
        }
    }

If you can help me, that would be great! Thanks!


回答1:


Use Bitmap#createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter). One of the parameters is a boolean for filtering: make sure you set that to false.




回答2:


This is how I got it working with the help of the other answer on this question:

This is the code:

Options options = new BitmapFactory.Options();
            options.inDither = false;
            options.inScaled = false;
            Bitmap source = BitmapFactory.decodeResource(getResources(), R.drawable.your_image, options);

            final int maxSize = 960; //set this to the size you want in pixels
            int outWidth;
            int outHeight;
            int inWidth = source.getWidth();
            int inHeight = source.getHeight();
            if(inWidth > inHeight){
                outWidth = maxSize;
                outHeight = (inHeight * maxSize) / inWidth; 
            } else {
                outHeight = maxSize;
                outWidth = (inWidth * maxSize) / inHeight; 
            }

            Bitmap resizedBitmap = Bitmap.createScaledBitmap(source, outWidth, outHeight, false);
            picture = (ImageView)v.getTag(R.id.picture);
            picture.setImageBitmap(resizedBitmap);


来源:https://stackoverflow.com/questions/22183432/how-to-upscale-an-image-without-it-becoming-blurry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!