在 android 中显示图片使用的是 ImageView ,这里的代码是从书中的例子来的,使用很简单,现在才觉得 android开发真的好方便呀
代码如下:
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image1"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image1"
/>
</LinearLayout>
java代码
package zziss.android.imagetest;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
public class ImageTestActivity extends Activity {
/** Called when the activity is first created. */
private ImageView iv;
private int img_alpha = 0;
Handler iHandler = new Handler();
private boolean iIsRun = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)this.findViewById(R.id.image1);
iv.setImageResource(R.drawable.q2);
iv.setAlpha(img_alpha);
iIsRun = true;
new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
while (iIsRun)
{
try
{
Thread.sleep(200);
updateAlpha();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
} // end new runnable
).start();// end new thread
iHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
iv.setAlpha(img_alpha);
iv.invalidate();
}
};
}
private void updateAlpha()
{
if (img_alpha+7<=255)
{
img_alpha+=7;
}else
{
img_alpha = 255;
iIsRun = false;
}
iHandler.sendMessage(iHandler.obtainMessage());
}
}
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
public class ImageTestActivity extends Activity {
/** Called when the activity is first created. */
private ImageView iv;
private int img_alpha = 0;
Handler iHandler = new Handler();
private boolean iIsRun = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView)this.findViewById(R.id.image1);
iv.setImageResource(R.drawable.q2);
iv.setAlpha(img_alpha);
iIsRun = true;
new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
while (iIsRun)
{
try
{
Thread.sleep(200);
updateAlpha();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
} // end new runnable
).start();// end new thread
iHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
iv.setAlpha(img_alpha);
iv.invalidate();
}
};
}
private void updateAlpha()
{
if (img_alpha+7<=255)
{
img_alpha+=7;
}else
{
img_alpha = 255;
iIsRun = false;
}
iHandler.sendMessage(iHandler.obtainMessage());
}
}