Android 特效View第三彈之閃爍View,androidview

來源:互聯網
上載者:User

Android 特效View第三彈之閃爍View,androidview
Android  特效View第三彈之閃爍View
動態我只做了半天還是失敗了,給一個,剩下的全靠想象了

<FrameLayout        android:layout_width="match_parent"        android:layout_height="match_parent" >        <com.example.empty.FlickerTextView            android:id="@+id/flicker"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="top|left"            android:text="Happy"            android:textColor="#00FF00"            android:textSize="24dp" />        <com.example.empty.FlickerText            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="bottom|right"            android:alpha="1"            android:text="Today"            android:textColor="#0000FF"            android:textSize="24dp" />        <com.example.empty.FlikerImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:src="@drawable/ic_launcher"/>    </FrameLayout>


package com.example.empty;import java.util.Timer;import java.util.TimerTask;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Color;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.widget.TextView;public class FlickerTextView extends TextView{    boolean change = false;    private Handler handler = null;     public FlickerTextView(Context context, AttributeSet attrs) {        super(context, attrs);                // TODO Auto-generated constructor stub        startFlicker();    }                  @SuppressLint("HandlerLeak")    public void startFlicker(){        handler = new Handler(){            @Override            public void dispatchMessage(Message msg) {                if(change){                    change = false;                    setTextColor(Color.TRANSPARENT); //這個是透明,=看不到文字                }else{                    change = true;                    setTextColor(Color.RED);                }            }        };                 Timer timer = new Timer();        TimerTask task = new TimerTask() {            @Override            public void run() {                Message msg = new Message();                handler.sendMessage(msg);            }        };        timer.schedule(task,1,300);  //參數分別是delay(多長時間後執行),duration(執行間隔)    }     }

package com.example.empty;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.widget.TextView;public class FlickerText extends TextView{    public FlickerText(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub    }        interface STATE{        static final int VISIBLE = 1;        static final int INVISIBLE = 0;    }    @Override     protected void onDraw(Canvas canvas) {         super.onDraw(canvas);        switch(getAlphastate()){        case STATE.VISIBLE:{            setAlpha(STATE.INVISIBLE);            break;        }        case STATE.INVISIBLE:{            setAlpha(STATE.VISIBLE);            break;        }        }                        postInvalidateDelayed(300);    }        public int getAlphastate(){        return (int)getAlpha()== STATE.INVISIBLE ? STATE.INVISIBLE:STATE.VISIBLE;            }}

package com.example.empty;import java.util.Timer;import java.util.TimerTask;import android.content.Context;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.widget.ImageView;public class FlikerImageView extends ImageView {    boolean change = false;    public FlikerImageView(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        startFlicker();    }    interface STATE {        static final int VISIBLE = 255;        static final int INVISIBLE = 0;    }    private Handler handler = new Handler() {        @Override        public void dispatchMessage(Message msg) {            if (change) {                change = false;                setImageAlpha(STATE.INVISIBLE);            } else {                change = true;                setImageAlpha(STATE.VISIBLE);            }        }    };    public void startFlicker() {        Timer timer = new Timer();        TimerTask task = new TimerTask() {            @Override            public void run() {                Message msg = new Message();                handler.sendMessage(msg);            }        };        timer.schedule(task, 1, 300);    }}

我這裡給出了3種實現方案(其實是兩種)來實現達到View閃爍的效果。這裡簡單說明一下2種方案的思路第一種我們通過Timer定時改變View的狀態第二種通過postInvalidateDelayed來每隔一段時間進行一次重繪。
然後我們改變View也是通過2種方式一、setColor 二、setAlpha當然,我們還可以通過一段動畫(anim)來實現。但是原理是相同的
這裡我推薦的方式是postInvalidateDelayed + setAlpha來實現但是在ImageVIew種當你調用setAlpha時系統會自動調用Invalidate(onDraw),這時你的圖片就會一直處於閃爍狀態這裡我們就只能採用Timer和動畫來實現了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.