1

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

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

/*

  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,但我设置为墙纸的图像仍然比我的设备屏幕更大。

在我的真机 LG L5 与新的 android 测试后(不确定哪个版本)。图像设置为正确的壁纸(在纵向模式下 - 所有 5 个“滚动屏幕”的 1 个图像没有缩放)。

我如何在其他设备上测试它?意思是……这种壁纸人像模式是否仅在新的安卓版本中可用?

4

3 回答 3

7

您可以使用此 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);
于 2013-08-19T08:26:16.980 回答
0
于 2013-08-19T08:00:48.417 回答
0

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

第一个:

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

    @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
}
于 2016-05-13T10:30:46.437 回答