Android special effect View: flashing View in the third bullet, androidview

Source: Internet
Author: User

Android special effect View: flashing View in the third bullet, androidview
Flashing View in the third bullet of Android special effect View
I 've only been doing this for half a day, but I still have to give it one. The rest depends on imagination.

<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); // This is TRANSPARENT, = no text} 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); // The parameters are delay (after which), duration (Execution interval )}}

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);    }}

I have provided three implementation schemes (actually two) to achieve the View flashing effect. Here is a brief description of the two solutions. The first is to change the View status through Timer. The second is to redraw the View at intervals through postInvalidateDelayed.
Then we can change the View in two ways: 1. setColor 2. setAlpha. Of course, we can also use an animation (anim. But the principle is the same.
The recommended method here is postInvalidateDelayed + setAlpha. However, in ImageVIew, when you call setAlpha, the system automatically calls Invalidate (onDraw ), at this time, your image will remain flashing. Here we can only use Timer and animation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.