android缩放位图到屏幕尺寸

时间:2013-08-19 07:58:18

标签: android bitmap scale

实际上我正试图用壁纸创建简单的应用程序。

我尝试将图片缩放到用户设备屏幕尺寸。 我使用的代码如下:

/*

  DisplayMetrics metrics = new DisplayMetrics(); 

  getWindowManager().getDefaultDisplay().getMetrics(metrics); 

  int height = metrics.heightPixels; 
  int width = metrics.widthPixels; 

*/


Display metrics = getWindowManager().getDefaultDisplay();

int height = metrics.getHeight();
int width = metrics.getWidth(); 
float fourToThree;
int wymiar;
Bitmap mbitmap; //definicja zmiennej przechowującej bitmapę
try {

    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), cardBase.cardImages[position]);
    //UP - zapisanie obrazu z tablicy do zmiennej bitmapy
    /*if(height>=width) {
        fourToThree = height * 0.75f;   //height
        wymiar = (int)Math.round(fourToThree);
        mbitmap = Bitmap.createScaledBitmap(myBitmap, height, wymiar, true);
    } else {    
        fourToThree = height * 0.75f;
        wymiar = (int)Math.round(fourToThree);
        mbitmap = Bitmap.createScaledBitmap(myBitmap, width, wymiar, true);
    }*/
    mbitmap = Bitmap.createScaledBitmap(myBitmap, width, height, true);
    myWallpaper.setBitmap(mbitmap);
    Toast.makeText(SelectedCard.this, "Wallpaper changed", Toast.LENGTH_LONG).show();
}
catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(SelectedCard.this, "Sorry, an error occurred", Toast.LENGTH_LONG).show();
}

我正在尝试许多其他方法,但仍然让我的图像大于设备屏幕:(

我正在virtual phone上对其进行测试,我试图将图片dit 160 dpi创建为设备屏幕,但它也无法正常工作。

任何人都可以告诉我,我如何缩放我的图像(jpg - 320 x 480 px,300 dpi)将其设置为设备上的壁纸?

有什么想法吗?

谢谢:)

聚苯乙烯。对不起文字错误,英语是我的第二语言; p


好的,我有类似的东西:

imgView.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            WallpaperManager myWallpaper = WallpaperManager.getInstance(getApplicationContext());
            try
            /*{
                myWallpaper.setResource(HdImageBase.HdImages[position]);
                Toast.makeText(ImageMini.this, "Wallpaper changed", Toast.LENGTH_LONG).show();
            }*/
            {
                DisplayMetrics metrics = new DisplayMetrics(); 

                getWindowManager().getDefaultDisplay().getMetrics(metrics); 

                int height = metrics.heightPixels; 
                int width = metrics.widthPixels; 
                float fourToThree;
                int wymiar;
                Bitmap mbitmap; //definicja zmiennej przechowującej bitmapę

                Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), cardBase.cardImages[position]);

                mbitmap = Bitmap.createScaledBitmap(myBitmap, width, height, true);
                myWallpaper.setBitmap(mbitmap);

                Toast.makeText(SelectedCard.this, "Wallpaper changed \n" + width + "\n" + height, Toast.LENGTH_LONG).show();
                }
            catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(SelectedCard.this, "Sorry, an error occurred", Toast.LENGTH_LONG).show();
            }
        }
    });

宽度和高度值是正确的320 x 480,但我设置为壁纸的图像仍然比我的设备scren更大。

在我的真实手机LG L5上测试了新的android(不确定是哪个版本)。图像被设置为墙纸正确(在纵向模式下 - 所有5&#34的1个图像;滚动屏幕和#34;没有缩放)。

我如何在其他设备上测试它? 意思是......这个壁纸的肖像模式只在新的Android版本中可用吗?

3 个答案:

答案 0 :(得分:6)

您可以使用此For Scale位图..

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Bitmap bitmapOrg = new BitmapDrawable(getResources(), new  ByteArrayInputStream(imageThumbnail)).getBitmap();

int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();

float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;

// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);

答案 1 :(得分:0)

试试这可行: ImageView属性:android:scaleType=“fitXY”

答案 2 :(得分:0)

有两种方法可以缩放位图以适应屏幕:

<强>第一

follow函数绘制指定的位图,自动缩放/平移以填充目标矩形:

    @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Rect src, dst;
    int widthOfBitmapSrc, heightOfBitmapSrc;

    widthOfBitmapSrc = mBitmapSrc.getWidth();
    heightOfBitmapSrc = mBitmapSrc.getHeight();
    src = new Rect(0, 0, widthOfBitmapSrc, heightOfBitmapSrc);//size of bitmap

    dst = new Rect(0,0,this.getWidth(),this.getHeight());//size of this view

    canvas.drawBitmap(mBitmapSrc, src, dst, null);//scale bitmap from src to dst
}

请参阅此链接:http://developer.android.com/reference/android/graphics/Canvas.html

<强>第二

    @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    int widthOfNewBitmap, heightOfNewBitmap;

    widthOfNewBitmap = this.getWidth();//width of this view
    heightOfNewBitmap = this.getHeight();//height of this view

    Bitmap newBitmap = Bitmap.createScaledBitmap(mBitmapSrc, widthOfNewBitmap,heightOfNewBitmap,true);

    canvas.drawBitmap(mBitmapSrc, 0, 0, null);//copy bitmap to canvas of this view without scale
}