前言
在項目開發過程中,我們有時會遇到需要控件閃爍和停止的問題,這個用xml是可以實現的,但是為了在使用時控制方便,這里我寫了一個工具類,方便在代碼中控制view的閃爍與停止。
本文將涉及以下類容
- 閃爍實現原理
- 在MainActivity中的使用
- 效果圖和項目結構圖
實現過程
下面作以講解
一.閃爍實現原理
閃爍是通過android中的動畫類Animation實現的,
我在實現view閃爍動畫的時候,涉及到Animation的 以下方法:
1. alphaAnimation.setInterpolator(new LinearInterpolator());
這是給動畫設置插值器,默認線性插值器
要實現閃爍的話,就是一個重復的過程,於是Animation需要設置重復次數,因為是點擊開始在結束之前不停止的,所以會一直重復,則設置重復次數如下:
alphaAnimation.setRepeatCount(Animation.INFINITE);
最后在設置完動畫的model后啟動動畫:
alphaAnimation.setRepeatMode(Animation.REVERSE); view.startAnimation(alphaAnimation);
這樣一個不斷閃爍的動畫便完成了,接下來是停止閃爍動畫,Animation 有一個類似clear的方法,可以清除加在view上的而動畫效果,停止閃爍動畫的話,則需要這樣:
view.clearAnimation();
當然,以上便是Animation實現動畫實現view閃爍與停止的原理,這些只要理解就好,你不需要為怎么調用煩惱,因為我將view動畫閃爍及停止的方法封裝到demo中的FlashHelper中
FlashHelper類有兩個公開方法,開始閃爍與停止閃爍。如果你想讓你的view閃爍起來,你可以這樣:
//開啟閃爍,其中mTvText為view對象
FlashHelper.getInstance().startFlick(mTvText);
若想要閃爍的view停止下來,你可以這樣:
//停止閃爍,其中mTvText為view對象
FlashHelper.getInstance().stopFlick(mTvText);
下面看看FlashHelper在MainActivity中是如何調用的吧.
二.在MainActivity中的使用
FlashHelper在MainActivity中使用樣例如下:
package com.android.testdemo;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.base.BaseActivity;
import butterknife.BindView;
public class MainActivity extends BaseActivity {
@BindView(R.id.button1)
Button mBtn1;
@BindView(R.id.button2)
Button mBtn2;
@BindView(R.id.tv_text)
TextView mTvText;
@Override
protected int getContentViewId() {
return R.layout.activity_main;
}
@Override
protected void initView() {
}
@Override
protected void initData() {
}
@Override
protected void setListener() {
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.button1:
FlashHelper.getInstance().startFlick(mTvText);
break;
case R.id.button2:
FlashHelper.getInstance().stopFlick(mTvText);
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
MainActivity的xml文件很簡單,就是一個按鈕點擊開啟閃爍,然后一個按鈕點擊停止閃爍,閃爍效果在一個TextView上呈現,下面也簡單的給出activity_main.xml的代碼吧:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.android.testdemo.MainActivity"
tools:layout_editor_absoluteY="81dp"
android:background="@color/white">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="開啓閃爍"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="40dp"
android:text="停止閃爍"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/tv_text"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="112dp"
android:text="控件閃爍測試"
android:gravity="center"
android:background="@color/green"
android:textColor="@color/black"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
</android.support.constraint.ConstraintLayout>
ok,使用方法介紹完了,現在來看看效果圖和項目結構圖吧
運行效果圖
項目效果圖
項目結構圖
項目結構圖
Android代碼實現控件閃爍效果
注:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權