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);
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/imageView">
</ImageView>
还有就是你说的是1套图 还是多套图?
详细点 . 你说的这个方法不用XML? 直接就全代码?
你可以看到你的工程里面res里面的drawable里面分别有三个文件夹(sdk1.6以上)drawable-hdpi、drawable-mdpi、drawable-ldpi分辨代表着高中低分辨率,你要做的只是将高分辨率的图片(也就是分辨率为480*800和480*854机型上面显示的图片)放到drawable-hdpi中,320*480机型上面的图片放到drawable-mdpi这个文件夹里面就行了,系统会自动根据机型分辨率的大小去判断到那个文件夹里面拿图片。切记:在俩个文件夹中的同一张图片名称必须完全一样
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());
} }
我试着用宽度填充, 图片是随着屏幕尺寸不同而填充的,不过图片太小,容易画面失真。 为什么有人说不填充呢? 纵向填充,为什么上下,估计有个差不多一个编辑框大小的高度。
想知道,原因,是我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>
<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>
只放图片就可以? 不需要在XML里加东西吗?
你可以看到你的工程里面res里面的 drawable里面分别有三个文件夹(sdk1.6以上)drawable-hdpi、drawable-mdpi、drawable-ldpi分辨代表着高中低分辨率,你要做的只是将高分辨率的图片(也就是分辨率为480*800和480*854机型上面显示的图片)放到drawable-hdpi 中,320*480机型上面的图片放到drawable-mdpi这个文件夹里面就行了,系统会自动根据机型分辨率的大小去判断到那个文件夹里面拿图片。切记:在俩个文件夹中的同一张图片名称必须完全一样