如题,我想问大家的是如何给3套图 或者说1套图  让系统自动根据机型用适合尺寸的图..不知道我表达的清楚不清楚..之前有从网上下过一个例子,上面好像是说要从XML里添加权限 来实现.具体还是不清楚,希望大家能给我讲下,或者告诉我学习哪些东西能实现这个.

解决方案 »

  1.   

     设置图片宽和高为fill_parent就自适应了<ImageView 
            android:layout_height="fill_parent"
            android:layout_width="fill_parent" 
            android:id="@+id/imageView">
            </ImageView>
      

  2.   

    用代码实现,是肯定可以的,获取屏幕的属性getSettings  
      

  3.   

    只需要在XML里面加就可以? 不需要在别的地方加吗? 图片构造或者使用的时候也不用动?
    还有就是你说的是1套图 还是多套图?
      

  4.   


    详细点 . 你说的这个方法不用XML?  直接就全代码?
      

  5.   

    现在android 机型分辨率大概有三种320*480、480*800、480*854.
    你可以看到你的工程里面res里面的drawable里面分别有三个文件夹(sdk1.6以上)drawable-hdpi、drawable-mdpi、drawable-ldpi分辨代表着高中低分辨率,你要做的只是将高分辨率的图片(也就是分辨率为480*800和480*854机型上面显示的图片)放到drawable-hdpi中,320*480机型上面的图片放到drawable-mdpi这个文件夹里面就行了,系统会自动根据机型分辨率的大小去判断到那个文件夹里面拿图片。切记:在俩个文件夹中的同一张图片名称必须完全一样
      

  6.   

    确实,android 系统会自动识别你手机的分辨率,然后根据不同分辨率到不通的文件夹下面去获取资源信息,同样布局也可以有layout-hdpi、layout_mdpi等区分。
      

  7.   

    刚写完, 运行,界面不对。一起写,贴上来,共同学习。  帮我看看。
    public class MainActivity extends Activity 
    {
    ImageView imageView;
    TextView textView;
    TextView m_TextView;
    int image_alpha=10000;  //ImageView's alpha value
    private final static String TAG= "MainActivity";

    Handler mHandler = new Handler();
    boolean isrun=false;
    DisplayMetrics dm=new DisplayMetrics();

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            //取得窗口属性
            getWindowManager().getDefaultDisplay().getMetrics(dm);      
            int screen_Width=dm.widthPixels;
            int screen_Height=dm.heightPixels; 
            
            //设置布局
            LinearLayout main_View=new LinearLayout(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
             LinearLayout.VERTICAL,
             LinearLayout.LayoutParams.FILL_PARENT,
             LinearLayout.LayoutParams.FILL_PARENT);
            
            TextView m_TextView=new TextView(this);
            m_TextView.setWidth(screen_Width);
            m_TextView.setHeight(screen_Height/6);
            m_TextView.setBackgroundColor(Color.WHITE);
            m_TextView.setText("屏幕宽度:"+screen_Width+"\n"+"屏幕高度:"+screen_Height);
            main_View.addView(m_TextView, lp);
            
            final ImageView imageView=new ImageView(this);
            isrun=true;       
            imageView.setImageResource(R.drawable.iamge);
            imageView.setAlpha(image_alpha);
            imageView.setMaxWidth(screen_Width);
            imageView.setMaxHeight((screen_Width/6)*4);       
            main_View.addView(imageView,lp);
            
            final TextView textView =new TextView(this);
            m_TextView.setWidth(screen_Width);
            m_TextView.setHeight(screen_Height/6);
            m_TextView.setBackgroundColor(Color.WHITE);
            main_View.addView(textView,lp);
            
            new Thread(new Runnable()
            {
    @Override
    public void run()
    {
    while (isrun)
    {
    try
    {
    Thread.sleep(100);
    updateAlpha();

    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    }
    }).start();      
     
            //接收消息之后,更新Alpha的值
            mHandler =new Handler()
            {
             @Override
             public void handleMessage(Message msg)
             {
             super.handleMessage(msg);
             imageView.setAlpha(image_alpha);
             Log.i(TAG,"alpha");
             textView.setText("The value of Alpha"+   Integer.toString(image_alpha));
             Log.i(TAG,"setText");
             //更新
             imageView.invalidate();
             Log.i(TAG,"invalidate alpha");
             }
            };
            
        }
           
        public void updateAlpha()
        {
         if (image_alpha-7>=0)
         {
         image_alpha-=7;
         }
         else
         {
         image_alpha=0;
         isrun=false;
         }
         //发送需要更新ImageView的消息
         mHandler.sendMessage(mHandler.obtainMessage());
        }    }
      

  8.   

    还没写出来,到这一步,运行。 发现 图片下方显示的文本,是执行的。 可是为什么会不显示呢?
    我试着用宽度填充, 图片是随着屏幕尺寸不同而填充的,不过图片太小,容易画面失真。 为什么有人说不填充呢? 纵向填充,为什么上下,估计有个差不多一个编辑框大小的高度。   
    想知道,原因,是我XML布局设计出错了吗?   
    package com.android.ImageView;import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.DisplayMetrics;
    import android.util.Log;
    import android.widget.ImageView;
    import android.widget.TextView;public class MainActivity extends Activity 
    {
        ImageView imageView;
        TextView textView;
        TextView m_TextView;
        int image_alpha=10000;  //ImageView's alpha value
        private final static String TAG= "MainActivity";
        
        Handler mHandler = new Handler();
        boolean isrun=false;
        DisplayMetrics dm=new DisplayMetrics();
        
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            //取得窗口属性
            getWindowManager().getDefaultDisplay().getMetrics(dm);      
            int screen_Width=dm.widthPixels;
            int screen_Height=dm.heightPixels;       
            m_TextView=(TextView)findViewById(R.id.TextView02);
            m_TextView.setText("屏幕宽度:"+screen_Width+"\n"+"屏幕高度:"+screen_Height);
            
            isrun=true;       
            imageView = (ImageView)findViewById(R.id.ImageView01);
            textView = (TextView)findViewById(R.id.TextView01);
            
            //set image source
            imageView.setImageResource(R.drawable.iamge);
            imageView.setAlpha(image_alpha);
            
            new Thread(new Runnable()
            {            
                @Override
                public void run()
                {
                    while (isrun)
                    {
                        try
                        {
                            Thread.sleep(100);
                            updateAlpha();
                        } 
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                    
                }
            }).start();      
     
            //接收消息之后,更新Alpha的值
            mHandler =new Handler()
            {
                @Override
                public void handleMessage(Message msg)
                {
                    super.handleMessage(msg);
                    imageView.setAlpha(image_alpha);
                    Log.i(TAG,"alpha");
                    textView.setText("The value of Alpha"+   Integer.toString(image_alpha));
                    Log.i(TAG,"setText");
                    //更新
                    imageView.invalidate();
                    Log.i(TAG,"invalidate alpha");
                }
            };
            
        }
           
        public void updateAlpha()
        {
            if (image_alpha-7>=0)
            {
                image_alpha-=7;
            }
            else
            {
                image_alpha=0;
                isrun=false;
            }
            //发送需要更新ImageView的消息
            mHandler.sendMessage(mHandler.obtainMessage());
        }    }<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView
            android:id="@+id/TextView02"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            />
        <ImageView
            android:id="@+id/ImageView01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            />
        <!--<ImageView
            android:id="@+id/ImageView01"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            >
             </ImageView>-->
        <TextView  
            android:id="@+id/TextView01"
            android:layout_below="@id/ImageView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            />
    </LinearLayout>
      

  9.   

    使用图片的时候不写常量 写fill_parent??
      

  10.   

    不想弄了,弄了个差不多思路出来。  总之i,还是布局的实现问题先不说我这个具体能实现到什么程度,不过是能实现的。  既然,能够获得屏幕的属性。可以吧布局写死。设定所有图片的宽和高都是相同的。  这样肯定可以显示。不过一定要填充的话,我觉得也不符合实际需要。毕竟有的小图片,填充大的屏幕,直接失真。反而弄巧成拙了。我要搞个通讯录备份,这个你看看。欢迎随时,给我留言。我 QQ844917527<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id = "@+id/layout_root"
      android:orientation = "vertical"
      android:layout_width="320dip"
      android:layout_height="480dip">
        <TextView  android:id="@+id/TextView02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF"
         android:textSize = "28dip" android:layout_marginLeft="10dip" android:layout_marginRight="10dip" ></TextView>
    <ImageView
    android:id="@+id/ImageView01"
    android:layout_width="fill_parent"
    android:layout_height="200dip"
    android:background="@drawable/icon"
    android:layout_marginTop="10dip"> </ImageView>
    <LinearLayout android:id="@+id/linearLayout1" android:layout_marginBottom="0dip" android:layout_marginTop="20dip" android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="wrap_content">
    <TextView android:background="#FFFFFF" android:id="@+id/TextView01"  android:layout_marginTop="0dip" android:layout_height="50dip" android:textSize="30dip" android:layout_width="fill_parent"></TextView>
    </LinearLayout>
    </LinearLayout>
      

  11.   

    我有3套图 不能通过xml 来实现吗?
      

  12.   

    在之前的版本中,只有一个drawable,而2.1版本中有drawable-mdpi、drawable-ldpi、drawable-hdpi三个,这三个主要是为了支持多分辨率。  drawable- hdpi、drawable- mdpi、drawable-ldpi的区别:  (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),FWVGA (480x854)  (2)drawable-mdpi里面存放中等分辨率的图片,如HVGA (320x480)  (3)drawable-ldpi里面存放低分辨率的图片,如QVGA (240x320)  系统会根据机器的分辨率来分别到这几个文件夹里面去找对应的图片。  在开发程序时为了兼容不同平台不同屏幕,建议各自文件夹根据需求均存放不同版本图片。
      

  13.   


    只放图片就可以? 不需要在XML里加东西吗?
      

  14.   

    现在android 机型分辨率大概有三种320*480、480*800、480*854.
    你可以看到你的工程里面res里面的 drawable里面分别有三个文件夹(sdk1.6以上)drawable-hdpi、drawable-mdpi、drawable-ldpi分辨代表着高中低分辨率,你要做的只是将高分辨率的图片(也就是分辨率为480*800和480*854机型上面显示的图片)放到drawable-hdpi 中,320*480机型上面的图片放到drawable-mdpi这个文件夹里面就行了,系统会自动根据机型分辨率的大小去判断到那个文件夹里面拿图片。切记:在俩个文件夹中的同一张图片名称必须完全一样
      

  15.   

    ubuntu you zhongwen shuru ma?