ImagView

1.TestImageViewActivity:

 1 import android.app.Activity;
2 import android.os.Bundle;
3 import android.os.Handler;
4 import android.os.Message;
5 import android.widget.ImageView;
6 import android.widget.TextView;
7
8 public class TestImageViewActivity extends Activity
9 {
10 private ImageView imageView;
11 private TextView textView;
12 Handler handler = new Handler();
13 int imag_alpha = 255;
14 // 控件线程
15 boolean isrung = false;
16
17 @Override
18 public void onCreate(Bundle savedInstanceState)
19 {
20 super.onCreate(savedInstanceState);
21 setContentView(R.layout.main);
22 isrung = true;
23 imageView = (ImageView) findViewById(R.id.ImagView01);
24 imageView.setImageResource(R.drawable.kk);
25 // 设置Alpha
26 imageView.setAlpha(imag_alpha);
27 textView = (TextView) findViewById(R.id.TextView01);
28 new Thread(new Runnable()
29 {
30 @Override
31 public void run()
32 {
33 while (isrung)
34 {
35 try
36 {
37 Thread.sleep(2000);
38 update_alpha();
39 }
40 catch (InterruptedException e)
41 {
42 e.printStackTrace();
43 }
44 }
45 }
46 }).start();
47 handler = new Handler()
48 {
49 @Override
50 public void handleMessage(Message msg)
51 {
52 super.handleMessage(msg);
53 imageView.setAlpha(imag_alpha);
54 textView.setText("imag_alpha的值为:" + imag_alpha);
55 // 更新
56 imageView.invalidate();
57 }
58 };
59 }
60
61 private void update_alpha()
62 {
63 if (imag_alpha >= 7)
64 {
65 imag_alpha -= 7;
66 }
67 else
68 {
69 isrung = false;
70 }
71 // 发送需要更新imagView视图的消息
72 handler.sendMessage(handler.obtainMessage());
73 }
74 }

 

2. /TestImageView/res/layout/main.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical" android:layout_width="fill_parent"
4 android:layout_height="fill_parent">
5 <ImageView android:id="@+id/ImagView01" android:layout_width="fill_parent"
6 android:layout_height="wrap_content" />
7 <TextView android:id="@+id/TextView01" android:layout_below="@id/ImagView01"
8 android:layout_width="fill_parent" android:layout_height="wrap_content" />
9 </LinearLayout>

 

3. 效果图:





posted @ 2011-10-30 13:22  程序学习笔记  阅读(277)  评论(0编辑  收藏  举报