package com.example.activity01;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class Activity01 extends Activity
{
//声明ImageView对象
ImageView imageview;
TextView textview;
//ImageView的alpha值,
int image_alpha = 255;
Handler mHandler = new Handler();
//控件线程
boolean isrung = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isrung = true;
//获得ImageView的对象
imageview = (ImageView) this.findViewById(R.id.ImageView01);
textview = (TextView) this.findViewById(R.id.TextView01);
//设置imageview的图片资源。同样可以再xml布局中像下面这样写
//android:src="@drawable/logo"
imageview.setImageResource(R.drawable.logo);
//设置imageview的Alpha值
imageview.setAlpha(image_alpha);
//开启一个线程来让Alpha值递减
new Thread(new Runnable() {
public void run()
{
while (isrung)
{
try
{
Thread.sleep(200);
//更新Alpha值
updateAlpha();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
//接受消息之后更新imageview视图
mHandler = new Handler() {
@Override
public void handleMessage(Message msg)
{
image_alpha = msg.arg1;
imageview.setAlpha(image_alpha);
textview.setText("现在alpha值是:"+Integer.toString(image_alpha));
}
};
}
public void updateAlpha()
{
if (image_alpha - 7 >= 0)
{
image_alpha -= 7;
}
else
{
image_alpha = 0;
isrung = false;
}
//发送需要更新imageview视图的消息
Message msg = mHandler.obtainMessage();
msg.arg1 = image_alpha;
mHandler.sendMessage(msg);
}
}
<?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"
>
<ImageView
android:id="@+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</ImageView>
<TextView
android:id="@+id/TextView01"
android:layout_below="@id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
</LinearLayout>
上例实现了在屏幕上出现一个图片,并且通过线程的调用(start)使其Alpha值逐渐减小至0(不可见)
其实跟那个ProgressBar的例子是一样的。
那个例子给按钮设置一个监听其,用来启动Handler从而调动线程(有点死循环的意思,不过最后做了判断)
这个例子是自动在程序中启动线程隔一段时间就调用一次相关方法从而在Handler内部类当中实现相应操作。
本质上模式是一致的。
不过本例的原本程序并不是这样的
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class Activity01 extends Activity
{
//声明ImageView对象
ImageView imageview;
TextView textview;
//ImageView的alpha值,
int image_alpha = 255;
Handler mHandler = new Handler();
//控件线程
boolean isrung = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isrung = true;
//获得ImageView的对象
imageview = (ImageView) this.findViewById(R.id.ImageView01);
textview = (TextView) this.findViewById(R.id.TextView01);
//设置imageview的图片资源。同样可以再xml布局中像下面这样写
//android:src="@drawable/logo"
imageview.setImageResource(R.drawable.logo);
//设置imageview的Alpha值
imageview.setAlpha(image_alpha);
//开启一个线程来让Alpha值递减
new Thread(new Runnable() {
public void run()
{
while (isrung)
{
try
{
Thread.sleep(200);
//更新Alpha值
updateAlpha();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
//接受消息之后更新imageview视图
mHandler = new Handler() {
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
imageview.setAlpha(image_alpha);
textview.setText("现在alpha值是:"+Integer.toString(image_alpha));
//更新
imageview.invalidate();
}
};
}
public void updateAlpha()
{
if (image_alpha - 7 >= 0)
{
image_alpha -= 7;
}
else
{
image_alpha = 0;
isrung = false;
}
//发送需要更新imageview视图的消息
mHandler.sendMessage(mHandler.obtainMessage());
}
}
这个程序没有直接利用Handler来传递数据,不知道是怎样自动实现的,另外ImageView类的invalidate方法和setAlpha方法有重复,前者不需要