Android-ImageView圖片視圖Demo

來源:互聯網
上載者:User

標籤:

代碼

package com.lxt008;import com.lxt008.R;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());    }}

 

布局檔案

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

 

Android-ImageView圖片視圖Demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.