如何调整图像大小以设置为适合屏幕的壁纸

时间:2013-12-23 12:57:53

标签: android android-imageview wallpaper image-scaling

我有位图图像,当我要设置为壁纸它是模糊的,它不适合屏幕..如何设置适当的屏幕尺寸以及如何获得屏幕尺寸来设置图像...如何解决这个问题......

class MyPagerAdapter extends PagerAdapter {
    int breadset;
    int forwall;
    public int[] images1 = { R.drawable.s_1, R.drawable.s_2, R.drawable.s_3 };
    public int[] images2 = { R.drawable.s_5, R.drawable.s_6, R.drawable.s_4 };

    public MyPagerAdapter(int gotbread) {
        this.breadset = gotbread;
        // TODO Auto-generated constructor stub
    }

    public int setcall() {
        return forwall;

    }

    public int getCount() {
        return images1.length;
    }

    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = null;

        view = inflater.inflate(R.layout.layout_fullscreen_view, null);
        ((ViewPager) collection).addView(view, 0);
        Button btn = (Button) view.findViewById(R.id.wallb);
        ImageView iv = (ImageView) view.findViewById(R.id.imgDisplay);

        switch (breadset) {
        case 0:
            iv.setImageResource(images1[position]);
            break;
        case 1:
            iv.setImageResource(images2[position]);
        }
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    WallpaperManager myWallpaperManager = WallpaperManager
                            .getInstance(getApplicationContext());
                    switch (breadset) {
                    case 0:
                        myWallpaperManager.setResource(images1[forwall]);

                        break;
                    }
                    Context context = getApplicationContext();
                    CharSequence text = "wallpaper has been set to your screen!";
                    int duration = Toast.LENGTH_SHORT;

                    Toast toast = Toast.makeText(context, text, duration);
                    toast.show();

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // TODO Auto-generated method stub

            }

        });

        return view;
    }

    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }

    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }

}

1 个答案:

答案 0 :(得分:1)

您可以使用此功能:

public void decodeFile(String filePath) {

    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 3;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    mPhoto = BitmapFactory.decodeFile(filePath, o2);
    myBitmap = ExifUtils.rotateBitmap(filePath, mPhoto);

    image.setImageBitmap(myBitmap);
}

您也可以使用this