首页 文章

Android:处理多个屏幕的图像大小

提问于
浏览
7

我觉得用Android处理图像真的很难,我认为这是Android开发中最困难的部分......

1)我有一个图像,我希望它是我的应用程序的背景,所以我这样做

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" 
    android:background="@drawable/accueil">
</RelativeLayout>

背景应该填满整个屏幕,但它不是cas . 某些(小)屏幕上仅显示部分背景 . 我怎么能说:我希望图像能够填满所有的屏幕,即使图像比图像大,屏幕上的图像应该减少,所以我看到了所有的背景?

也许我应该把我的背景放在背景上而不是用它作为背景

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity">


       <ImageView
           android:id="@+id/imageView1"
           android:layout_width="500dp"
           android:layout_height="500dp"
           android:adjustViewBounds="false"
           android:src="@drawable/accueil" />

</RelativeLayout>

然而在某些设备中,图像有点裁剪 .

2)我必须在另一个布局中放置一个布局,但我希望它的位置精确,其宽度相对于父布局 . 但如果我这样做

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fond">

 <LinearLayout
     android:layout_width="300dp"
     android:layout_height="350dp"
     android:layout_marginTop="20dp"
     android:background="@drawable/fondshare"
     android:orientation="vertical" >

你可以看到结果不是我的预期(线性布局是带有笑脸和所有按钮的矩形)
enter image description here

6 回答

  • 0

    Android使用两个常规属性对设备屏幕进行分类:大小和密度 . 您应该期望您的应用程序将安装在屏幕尺寸和密度都不同的设备上 . 因此,您应该包含一些替代资源,以针对不同的屏幕尺寸和密度优化应用的外观 . 有四种广义尺寸:小,普通,大,xlarge和四种广义密度:低(ldpi),中(mdpi),高(hdpi),超高(xhdpi)

    要声明要用于不同屏幕的不同布局和位图,必须将这些替代资源放在不同的目录中,类似于对不同语言字符串的操作方式 .

    另请注意,屏幕方向(横向或纵向)被视为屏幕大小的变体,因此许多应用程序应修改布局以优化每个方向的用户体验 .

    有关更多信息,请参阅android.com's "Supporting Different Screens"文章 .

  • 2

    我想如果你想使用Bitmap作为你的背景,有一些解决方法:

    • 为不同设备设计多种尺寸的图像 . (drawable-hdpi,drawable-xhdpi ...)

    • 使用不同的布局并将它们放入使用限定符的不同资源包中 . (layout-sw600dp ...)

  • 0

    我建议你调整每个布局文件夹和drawable文件夹的布局 .

    布局布局-800x480布局 - 土地等参考这里http://developer.android.com/guide/practices/screens_support.html

    或参考此链接 .

    Designing UI for different screen resolutions

  • 0

    试试这个:

    我遇到了很多时间让我的布局设计得很好 . 最后我得到了一个类似的解决方案

    我已经为不同的屏幕创建了布局文件夹,如小型和大型 . 原始布局文件夹xml文件仅适用于HVGA的常规布局因此,为小布局创建文件夹_2835427,并将xml文件放在此处 . 然后为此布局创建文件夹 name=layout-long 的文件夹,用于大型布局和xd文件 .

  • 0

    完成此任务的最佳方法是使用线性布局和使用

    <LinearLayout
     android:layout_width="300dp"
     android:layout_height="350dp"
     android:layout_marginTop="20dp"
     android:weightSum="3"
     android:background="@drawable/fondshare"
     android:orientation="vertical" >
    
      <View
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />
    
      </LinearLayout>
    

    在上面的例子中

    android:weightSum="number of parts you want to divide layout in"

    android:layout_weight="1"

    视图占用的总分数 . 假设线性布局的权重为3因此视图所覆盖的部分将是

    Width of view

    (layout_width / WeightSum)*重量

    Height of view

    (layout_height / WeightSum)*重量

    在您的情况下,您的视图最终高度将是= 119dp

    最终宽度为= 100dp

  • 0

    我最近不得不做类似的事情 . 所以这里有一些技巧 .

    如果要保留图像的纵横比,请在相对布局中使用它 . 注意:相对布局的布局宽度和高度应为match_parent或fill_parent .

    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    
            android:id="@+id/bgloginpage"
    
            android:scaleType="centerCrop" />
    

    这将从中心裁剪图像 . 还有其他选择 . 使用 fitXY 而不是 centerCrop 来适应设备而不考虑宽高比 . 为了获得最佳效果,请使用相当大的图像 .

    另一种选择是为图像充分添加纯色背景颜色并增加其尺寸 . 将其加载到drawables文件夹中并使用此类 .

    public class ReturnBackGroundImage {
    
    
    
    //To avoid java.lang.OutOfMemory exceptions, check the dimensions of a    bitmap before decoding it,
    // unless you absolutely trust the source to provide you with predictably sized image data that
    // comfortably fits within the available memory.
    
    public static Bitmap decodeSampledBitmapFromResource(Resources resources,    int resId, int reqWidth, int reqHeight, boolean UseDeviceHeight, Activity  activity) {
    
    
        if(UseDeviceHeight){
            DisplayMetrics metrics = new DisplayMetrics();
    
            activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            reqHeight = metrics.heightPixels;
            reqWidth = metrics.widthPixels;
        }
    
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(resources, resId, options);
        return ResizeImage(resources,options,resId,reqWidth,reqHeight);
    }
    
    public static Bitmap ResizeImage(Resources resources,BitmapFactory.Options options, int resId, int reqWidth, int reqHeight) {
    
        double imageHeight = options.outHeight;
        double imageWidth = options.outWidth;
        double ratio = reqHeight / imageHeight;
        int newImageWidth = (int) (imageWidth * ratio);
        Bitmap bMap = BitmapFactory.decodeResource(resources, resId);
        return  getResizedBitmap(bMap, reqHeight, newImageWidth);
       }
    }
    

    使用方法如下

    ImageView v = (ImageView)findViewById(R.id.bgloginpagew);
      v.setImageBitmap(ReturnBackGroundImage.decodeSampledBitmapFromResource(getResources(),R.drawable.mainbgforapp,0,0,true,walkThroughActivity.this));
    

    这是来自android网站的示例的修改版本 .

    如果有人需要比率来保存不同大小的图像,那就是

    • xxxhdpi / xxhdpi = 4/3

    • xxxhdpi / xhdpi = 2/1

    • xxhdpi / hdpi = 2/1

    • xhdpi / mdpi = 2/1

    afaik这些比率仅适用于方形图像 . 我以为我会把它放在那里需要它们的人,就像我一样 .

相关问题