【问题标题】:android scale bitmap to screen dimensionandroid将位图缩放到屏幕尺寸
【发布时间】:2013-08-19 07:58:17
【问题描述】:

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

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

/*

  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 像素,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 个图像没有缩放)。

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

【问题讨论】:

  • 谢谢大家,但我说的是改变图像的大小,它将被设置为手机中的壁纸。我不想把它放到 ImageView :) 例如。在点击其中一个图像后,我有一系列图像(280 x 320),我将其设置为手机壁纸(在主屏幕上 - “桌面”)。喜欢这个应用程序:play.google.com/store/apps/…

标签: android bitmap scale


【解决方案1】:

您可以使用此 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);

【讨论】:

    【解决方案2】:

    试试这个可能有效: ImageView 属性:android:scaleType=“fitXY”

    【讨论】:

    • 好的,我有类似的东西:
    • 查找 3 个帖子(我为我的问题添加新评论:/)。
    【解决方案3】:

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

    第一:

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

        @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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多