【问题标题】:how to resize image to set as wallpaper which fit with screen如何调整图像大小以设置为适合屏幕的壁纸
【发布时间】:2014-01-11 16:55:23
【问题描述】:

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

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);
    }

}

【问题讨论】:

    标签: android android-imageview wallpaper image-scaling


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-10
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      • 2014-05-18
      • 2011-02-03
      • 1970-01-01
      相关资源
      最近更新 更多