zoukankan      html  css  js  c++  java
  • ImageView的使用(android)

    在 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>

    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());
        }
    }
  • 相关阅读:
    Java 练习(获取两个字符串中最大相同子串)
    STM32F103 实现 简易闹钟小程序
    STM32F103 实现 LCD显示年月日时分秒星期 并可逐值修改的日期 小程序
    Docker报错之“Failed to get D-Bus connection: Operation not permitted”
    数据结构解析
    每天一条DB2命令-004
    每天一条DB2命令-003
    每天一条DB2命令-002
    ElasticSearch系列
    模块三 GO语言实战与应用-BYTES包与字节串操作(下)
  • 原文地址:https://www.cnblogs.com/zziss/p/2304130.html
Copyright © 2011-2022 走看看