再发送给Wallpapermanager前上浆位图 [英] Re sizing bitmap before sending to Wallpapermanager

查看:156
本文介绍了再发送给Wallpapermanager前上浆位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,将其发送到墙纸经理,以便当用户将它作为自己的墙纸,它合理地配合,100%将是preferred之前调整大小的位图。

我使用的墙纸经理和我越来越从ImageView的图像。

我遇到的问题是墙纸真的放大。以前,当我直接从绘制目录设置壁纸,它看起来很好,你可以看到更多的图像,而不是1/4了。我已经改变了我的code可高达从那以后,发现了更多的让我的形象和设置壁纸的有效途径。

我已经看过此链接这里,我试图找出如何实现向您展示如何回答发送到墙纸经理前调整图像大小。

任何帮助将是AP preciated,欢呼声。

相对code到问题:

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

    最终的视图V = inflater.inflate(R.layout.image_detail_fragment,
            集装箱,假);


    INT是measuredWidth = 0;
    INT是measuredHeight = 0;

    窗口管理器,W = getActivity()getWindowManager()。

    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB_MR2){
        。w.getDefaultDisplay()的getSize(大小);
        是measuredWidth = Size.x;
    是measuredHeight = Size.y;
    } 其他 {
        显示D = w.getDefaultDisplay();
    是measuredWidth = d.getWidth();
    是measuredHeight = d.getHeight();
    }




    mImageView =(RecyclingImageView)v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){

            BitmapDrawable绘制=(BitmapDrawable)mImageView
                    .getDrawable();
            点阵位图= drawable.getBitmap();

            WallpaperManager myWallpaperManager = WallpaperManager
                    .getInstance(getActivity());

            尝试 {

                myWallpaperManager.setBitmap(位);
                ;
                Toast.makeText(getActivity(),
                        壁纸设置成功!,Toast.LENGTH_LONG)
                        。显示();
            }赶上(IOException异常E){
                Toast.makeText(getActivity(),错误设置墙纸,
                        Toast.LENGTH_LONG).show();
            }

        }
 

我的全班同学:

 公共类ImageDetailFragment扩展片段{
私有静态最后弦乐IMAGE_DATA_EXTRA =extra_image_data;
私有静态最后的点大小= NULL;
私人字符串mImageUrl;
私人RecyclingImageView mImageView;
私人ImageFetcher mImageFetcher;

公共静态ImageDetailFragment的newInstance(字符串IMAGEURL){
    最后ImageDetailFragment F =新ImageDetailFragment();

    最终捆绑的args =新包();
    args.putString(IMAGE_DATA_EXTRA,IMAGEURL);
    f.setArguments(参数);

    返回F;
}

公共ImageDetailFragment(){
}

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    mImageUrl = getArguments()!= NULL? getArguments()。的getString(
            IMAGE_DATA_EXTRA):空;

}

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
        捆绑savedInstanceState){

    最终的视图V = inflater.inflate(R.layout.image_detail_fragment,
            集装箱,假);


    INT是measuredWidth = 0;
    INT是measuredHeight = 0;

    窗口管理器,W = getActivity()getWindowManager()。

    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB_MR2){
        。w.getDefaultDisplay()的getSize(大小);
        是measuredWidth = Size.x;
    是measuredHeight = Size.y;
    } 其他 {
        显示D = w.getDefaultDisplay();
    是measuredWidth = d.getWidth();
    是measuredHeight = d.getHeight();
    }




    mImageView =(RecyclingImageView)v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(新View.OnClickListener(){

        @覆盖
        公共无效的onClick(查看为arg0){

            BitmapDrawable绘制=(BitmapDrawable)mImageView
                    .getDrawable();
            点阵位图= drawable.getBitmap();

            WallpaperManager myWallpaperManager = WallpaperManager
                    .getInstance(getActivity());

            尝试 {

                myWallpaperManager.setBitmap(位);
                ;
                Toast.makeText(getActivity(),
                        壁纸设置成功!,Toast.LENGTH_LONG)
                        。显示();
            }赶上(IOException异常E){
                Toast.makeText(getActivity(),错误设置墙纸,
                        Toast.LENGTH_LONG).show();
            }

        }

    });

    返回伏;
}

@覆盖
公共无效onActivityCreated(包savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    如果(Batmanark.class.isInstance(getActivity())){
        mImageFetcher =((Batmanark)getActivity())getImageFetcher()。
        mImageFetcher.loadImage(mImageUrl,mImageView);
    }


}

@覆盖
公共无效的onDestroy(){
    super.onDestroy();
    如果(mImageView!= NULL){
        //取消所有待处理的图像工作
        ImageWorker.cancelWork(mImageView);
        mImageView.setImageDrawable(空);
    }
}
}
 

解决方案

如果你想与divice屏幕,以适应壁纸,那么你必须波纹管按照下列步骤:

  1. 获得divice屏幕的高度和宽度
  2. 采样位图图像
  3. 调整位图
  4. 设置位图作为墙纸之前,回收previous位图

code:

第1步:

  INT是measuredWidth = 0;
INT是measuredHeight = 0;

点大小=新的点();
//如果从一个活动做
窗口管理器,W = getWindowManager();
//否则使用
窗口管理器,W = context.getWindowManager();

如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB_MR2){
    。w.getDefaultDisplay()的getSize(大小);
    是measuredWidth = size.x;
是measuredHeight = size.y;
} 其他 {
    显示D = w.getDefaultDisplay();
是measuredWidth = d.getWidth();
是measuredHeight = d.getHeight();
}
 

步骤2 + 3:

 公共位图resizeBitmap(资源RES,INT reqWidth,INT reqHeight,
                           InputStream中的InputStream,INT文件长度){
    点阵位图= NULL;
    在的InputStream = NULL;
    InputStream的IN2 = NULL;
    InputStream的IN3 = NULL;

    尝试 {
        IN3 =的InputStream;

        ByteArrayOutputStream OUT =新ByteArrayOutputStream();
        ByteArrayOutputStream OUT2 =新ByteArrayOutputStream();

        副本(IN3,输出,文件长度);
        OUT2 =输出;
        IN2 =新ByteArrayInputStream的(out.toByteArray());
        在=新ByteArrayInputStream的(out2.toByteArray());

        BitmapFactory.Options选项=新BitmapFactory.Options();
        options.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeStream(在,空,期权);

                    如果(options.outHeight == -1 || options.outWidth == 1 || options.outMimeType == NULL){
            返回null;
    }

        options.inSampleSize = calculateInSampleSize(选项,reqWidth,reqHeight);

        options.inJustDe codeBounds = FALSE;

        位= BitmapFactory.de codeStream(平方英寸,空,期权);

        如果(位图!= NULL){
            位= Bitmap.createScaledBitmap(位图,reqWidth,reqHeight,假);
        }
        附寄();
        in2.close();
        in3.close();
    }赶上(IOException异常E1){
        e1.printStackTrace();
    }
    返回的位图;
}

公众诠释calculateInSampleSize(BitmapFactory.Options选项,诠释reqWidth,诠释reqHeight){
    //原始高度和宽度的图像
    最终诠释身高= options.outHeight;
    最终诠释宽度= options.outWidth;
    INT inSampleSize = 1;

    如果(高度> reqHeight ||宽度GT; reqWidth){

        //计算的高度和宽度的比率要求的高度和宽度
        最终诠释heightRatio = Math.round((浮动)的高度/(浮点)reqHeight);
        最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);

        //选择最小比例为inSampleSize值,这将保证最终图像
        //与两个尺寸大于或等于所请求的高度和宽度。
        inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;

        //这提供了一些额外的逻辑,如果图像有一个奇怪
        //宽高比。例如,全景可以具有大得多的
        //宽度比高度。在这种情况下,总像素可能还是
        //最终会被过大,使之与记忆,所以我们应该
        //更积极与样本下的图像(=大inSampleSize)。

        最终浮动totalPixels =宽*高;

        //任何超过2倍的像素要求,我们将进一步向下采样
        最终浮动totalReqPixelsCap = reqWidth * reqHeight * 2;

        而(totalPixels /(inSampleSize * inSampleSize)> totalReqPixelsCap){
            inSampleSize ++;
        }
    }
    返回inSampleSize;
}

公众诠释副本(输入的InputStream,OutputStream的输出,INT文件长度)抛出IOException异常{
    byte []的缓冲区=新的字节[8 * 1024];
    诠释计数= 0;
    INT N = 0;

    而(-1!=(N = input.read(缓冲液))){
        output.write(缓冲液,0,n)的;
        数+ = N;
        publishProgress((INT)(数* 100 /文件长度));
    }
    返回计数;
}
 

第四步:

回收位图使用说明:

  bitmap.recycle();
位= NULL;
 

调用诸如 resizeBitmap(context.getResources()函数,是measuredWidth,measuredHeight可以, THE_INPUTSTREAM_FROM_WHERE_YOU_ARE_DOWNLOADING_THE_IMAGE, FILELENGTH_FROM_THE_INPUTSTREAM);

如果您是从活动中这样称呼它调用函数: resizeBitmap(getResources(),是measuredWidth,measuredHeight可以, THE_INPUTSTREAM_FROM_WHERE_YOU_ARE_DOWNLOADING_THE_IMAGE,FILELENGTH_FROM_THE_INPUTSTREAM);

该函数将返回调整大小后的位图,这将适合与divice resulation。 如果你已经设置好的一个位图作为背景,那么不要忘记,然后再设置一个新的位图作为壁纸回收位图。

I am needing some help with resizing a bitmap before sending it to the wallpaper manager so that when the user sets it as their wallpaper, it fits reasonably, 100% would be preferred.

I am using wallpaper manager and am getting the image from an ImageView.

The issue I am having is the wallpaper is really zoomed in. Before, when I set the wallpaper straight from the drawable directory, it looked fine and you could see a lot more of the image, not 1/4 of it. I have changed my code up since then and have found a lot more of an effective way to get my images and set the wallpaper.

I have looked at This link here and am trying to figure out how to implement the answer that shows you how to resize the image before sending it to the wallpaper manager.

Any help would be appreciated, cheers.

Relative code to question:

        @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.image_detail_fragment,
            container, false);


    int Measuredwidth = 0;
    int Measuredheight = 0;         

    WindowManager w = getActivity().getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        w.getDefaultDisplay().getSize(Size);
        Measuredwidth = Size.x;
    Measuredheight = Size.y;
    } else {
        Display d = w.getDefaultDisplay();
    Measuredwidth = d.getWidth();
    Measuredheight = d.getHeight();
    }




    mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            BitmapDrawable drawable = (BitmapDrawable) mImageView
                    .getDrawable();
            Bitmap bitmap = drawable.getBitmap();

            WallpaperManager myWallpaperManager = WallpaperManager
                    .getInstance(getActivity());

            try {

                myWallpaperManager.setBitmap(bitmap);
                ;
                Toast.makeText(getActivity(),
                        "Wallpaper Successfully Set!", Toast.LENGTH_LONG)
                        .show();
            } catch (IOException e) {
                Toast.makeText(getActivity(), "Error Setting Wallpaper",
                        Toast.LENGTH_LONG).show();
            }

        }

My whole class:

public class ImageDetailFragment extends Fragment {
private static final String IMAGE_DATA_EXTRA = "extra_image_data";
private static final Point Size = null;
private String mImageUrl;
private RecyclingImageView mImageView;
private ImageFetcher mImageFetcher;

public static ImageDetailFragment newInstance(String imageUrl) {
    final ImageDetailFragment f = new ImageDetailFragment();

    final Bundle args = new Bundle();
    args.putString(IMAGE_DATA_EXTRA, imageUrl);
    f.setArguments(args);

    return f;
}

public ImageDetailFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mImageUrl = getArguments() != null ? getArguments().getString(
            IMAGE_DATA_EXTRA) : null;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View v = inflater.inflate(R.layout.image_detail_fragment,
            container, false);


    int Measuredwidth = 0;
    int Measuredheight = 0;         

    WindowManager w = getActivity().getWindowManager();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        w.getDefaultDisplay().getSize(Size);
        Measuredwidth = Size.x;
    Measuredheight = Size.y;
    } else {
        Display d = w.getDefaultDisplay();
    Measuredwidth = d.getWidth();
    Measuredheight = d.getHeight();
    }




    mImageView = (RecyclingImageView) v.findViewById(R.id.imageView);
    mImageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            BitmapDrawable drawable = (BitmapDrawable) mImageView
                    .getDrawable();
            Bitmap bitmap = drawable.getBitmap();

            WallpaperManager myWallpaperManager = WallpaperManager
                    .getInstance(getActivity());

            try {

                myWallpaperManager.setBitmap(bitmap);
                ;
                Toast.makeText(getActivity(),
                        "Wallpaper Successfully Set!", Toast.LENGTH_LONG)
                        .show();
            } catch (IOException e) {
                Toast.makeText(getActivity(), "Error Setting Wallpaper",
                        Toast.LENGTH_LONG).show();
            }

        }

    });

    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    if (Batmanark.class.isInstance(getActivity())) {
        mImageFetcher = ((Batmanark) getActivity()).getImageFetcher();
        mImageFetcher.loadImage(mImageUrl, mImageView);
    }


}

@Override
public void onDestroy() {
    super.onDestroy();
    if (mImageView != null) {
        // Cancel any pending image work
        ImageWorker.cancelWork(mImageView);
        mImageView.setImageDrawable(null);
    }
}
}

解决方案

if you want to fit the wallpaper with the divice screen, then you have to follow the steps bellow:

  1. get the height and width of the divice screen
  2. sample the bitmap image
  3. resize the bitmap
  4. before setting the bitmap as wallpaper, recycle the previous bitmap

code:

step 1:

int Measuredwidth = 0;
int Measuredheight = 0; 

Point size = new Point();
// if you are doing it from an activity
WindowManager w = getWindowManager();
// otherwise use this
WindowManager w = context.getWindowManager();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    w.getDefaultDisplay().getSize(size);
    Measuredwidth = size.x;
Measuredheight = size.y;
} else {
    Display d = w.getDefaultDisplay();
Measuredwidth = d.getWidth();
Measuredheight = d.getHeight();
}

step 2+3:

public Bitmap resizeBitmap(Resources res, int reqWidth, int reqHeight, 
                           InputStream inputStream, int fileLength) {
    Bitmap bitmap = null;
    InputStream in = null; 
    InputStream in2 = null;
    InputStream in3 = null;  

    try {
        in3 = inputStream;              

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream out2 = new ByteArrayOutputStream();

        copy(in3,out,fileLength);
        out2 = out;
        in2 = new ByteArrayInputStream(out.toByteArray());
        in = new ByteArrayInputStream(out2.toByteArray());

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);

                    if(options.outHeight == -1 || options.outWidth == 1 || options.outMimeType == null){
            return null;
    }                           

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        options.inJustDecodeBounds = false; 

        bitmap = BitmapFactory.decodeStream(in2, null, options);

        if(bitmap != null){
            bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, false);                     
        }
        in.close();
        in2.close();
        in3.close();
    } catch (IOException e1) {          
        e1.printStackTrace();
    }
    return bitmap;   
}

public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee a final image
        // with both dimensions larger than or equal to the requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

        // This offers some additional logic in case the image has a strange
        // aspect ratio. For example, a panorama may have a much larger
        // width than height. In these cases the total pixels might still
        // end up being too large to fit comfortably in memory, so we should
        // be more aggressive with sample down the image (=larger inSampleSize).

        final float totalPixels = width * height;

        // Anything more than 2x the requested pixels we'll sample down further
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;

        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
            inSampleSize++;
        }
    }
    return inSampleSize;
}

public int copy(InputStream input, OutputStream output, int fileLength) throws IOException{
    byte[] buffer = new byte[8*1024];
    int count = 0;
    int n = 0;

    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
        publishProgress((int) (count * 100 / fileLength));
    }
    return count;
}

step 4:

to recycle the bitmap use:

bitmap.recycle();
bitmap = null;

call the function like resizeBitmap(context.getResources(), Measuredwidth, Measuredheight, THE_INPUTSTREAM_FROM_WHERE_YOU_ARE_DOWNLOADING_THE_IMAGE, FILELENGTH_FROM_THE_INPUTSTREAM);.

if you are calling the function from an activity the call it like: resizeBitmap(getResources(), Measuredwidth, Measuredheight, THE_INPUTSTREAM_FROM_WHERE_YOU_ARE_DOWNLOADING_THE_IMAGE, FILELENGTH_FROM_THE_INPUTSTREAM);

the function will return resized bitmap which will fit with the divice resulation. if you have already setted a bitmap as wallpaper, then don't forget to recycle the bitmap before you set a new bitmap as wallpaper.

这篇关于再发送给Wallpapermanager前上浆位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆