Android 控件ImageView的使用

 

ImageView 将一张图片显示在屏幕上,需要创建一个显示图片的对象,Android中这个对象就是ImageView.下面例中设计ImageView图片然后

再通过线程对其Alpha进行更改。

imageview_handler.xml

<?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/imageView_handler"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

<TextView

android:id= "@+id/textView_handler"

android:layout_below="@id/imageView_handler"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</LinearLayout>

public class ImageView_Handler extends Activity{

public TextView textView_handler;

public ImageView imageView_handler;

public boolean flag= false;//线程是否执行标志位

Handler handler = new Handler();----------------------------------------------Handler的运用

public int image_alpha = 255;//ImageView的透明度

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.imageview_handler);

flag = true;//初始化线程标志位

imageView_handler = (ImageView)findViewById(R.id.imageView_handler);

textView_handler = (TextView)findViewById(R.id.textView_handler);

imageView_handler.setImageResource(R.drawable.ee);//设置imageView的图片资源也可在xml文件中如下书写

//android:src= "@drawable/ee"

imageView_handler.setAlpha(image_alpha);//设置图片的透明度

//创建一个线程来更新Alpha值

handler = new Handler(){//接受更新后的消息

public void handleMessage(Message msg){

super.handleMessage(msg);

imageView_handler.setAlpha(image_alpha);

textView_handler.setText("当前Alpha值为:"+Integer.toString(image_alpha));

imageView_handler.invalidate();//更新 此行代码是用来更新View的 。在这个例子中我不用效果是一样的为什么在这我要加上他呢?不懂

}

};

new Thread(new Runnable(){

@Override

public void run() {

while(flag){

try{

Thread.sleep(200);

updateAlpha();//更新Alpha值

}catch(InterruptedException e){

e.printStackTrace();

}

}

}

}).start();

}

protected void updateAlpha() {

if(image_alpha>0){

image_alpha-=10;

}else {

image_alpha =0;

flag = false;//线程停止更新

}

handler.sendMessage(handler.obtainMessage());//发送Alpha更新的消息

}

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值