发布于 2017-09-24 02:06:47 | 146 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


本文通过两种方法给大家讲解了android实现图片闪烁动画效果,实用性非常高,对这两种方法感兴趣的朋友一起通过本文学习吧

大家在使用APP的时候,有的APP在点击语音搜索界面后,会出现一个小话筒,小话筒会类似雷达似得在闪烁,表示正在倾听你说话的内容(这个大家可以参照微软的必应APP),那么问题来了,这种动画效果是如何实现的呢?其实实现这种动画效果有很多种方法,最常见的是两种:第一种就是插入n张图片进行切换已达到如此目的,第二种就是通过改变一张图片的透明度来达到闪烁的效果。下面就分别讲一下通过这两种方法如何实现。

第一种:通过n张图片之间切换实现动画效果

  这种方法的原理很简单,利用handler的延时机制在子线程中完成图片切换,再在主线程展示。

  1、首先我们要先写一个线程池,在使用的时候方便调用。


package com.jereh.musicapplication.threadpool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
/**
* Created by zhangdi on 2016/9/1.
* 这是一个线程池的工具类,在用到线程的时候可以直接类名加方法名使用
*/
public class ThreadPoolManager {
/** 线程执行器 **/
private static ExecutorService executorService = null;
/** 固定5个线程 **/
private static int nThreads = 5;
/** 单例 **/
private static ThreadPoolManager taskExecutorPool = null;
/** 初始化线程池 **/
static {
taskExecutorPool = new ThreadPoolManager(nThreads * getNumCores());
}
/** 构造函数 **/
private ThreadPoolManager(int threads) {
//executorService = Executors.newFixedThreadPool(threads);
executorService = Executors.newScheduledThreadPool(threads);
}
/**
* 取得单例
*
* @return
*/
public static ThreadPoolManager getInstance() {
return taskExecutorPool;
}
/**
* 取得线程执行器
*
* @return
*/
public ExecutorService getExecutorService() {
return executorService;
}
/**
* 取得周期性线程执行器
* @return
*/
public ScheduledExecutorService getScheduledExcutorService(){
return (ScheduledExecutorService)executorService;
}
/**
* 获得手机cup个数
* @return
*/
public static int getNumCores() {
int threadCount = Runtime.getRuntime().availableProcessors();
return threadCount;
}
}

2、下一步就是在xml文件中插入一个布局


<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fl"/>

3、然后就是在java代码中编辑切换图片了:


package com.jereh.musicapplication;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import com.jereh.musicapplication.threadpool.ThreadPoolManager;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class FrameActivity extends AppCompatActivity {
private Timer timer;
FrameLayout frameLayout;
Drawable drawable;
android.os.Handler handler = new android.os.Handler(){
int i = 0;
@Override
public void handleMessage(Message msg) {
if (msg.what==1){
i++;
move(i%4);
}
super.handleMessage(msg);
}
};
void move(int i){
drawable = getResources().getDrawable(R.mipmap.ic_launcher,null);
Drawable drawable1 = getResources().getDrawable(R.mipmap.dd1,null);
Drawable drawable2 = getResources().getDrawable(R.mipmap.dd2,null);
Drawable drawable3 = getResources().getDrawable(R.mipmap.dd3,null);
switch (i){
case 0:
frameLayout.setForeground(drawable);
break;
case 1:
frameLayout.setForeground(drawable1);
break;
case 2:
frameLayout.setForeground(drawable2);
break;
case 3:
frameLayout.setForeground(drawable3);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame);
frameLayout = (FrameLayout)findViewById(R.id.fl);
timer = new Timer();
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// handler.sendEmptyMessage(1);
// }
// },0,500);//第二个参数是隔多少秒之后开始显示,第三个是隔多久显示下一个
ThreadPoolManager
.getInstance()
.getScheduledExcutorService()
.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
},0,500, TimeUnit.MILLISECONDS);//第二个参数是隔多少秒之后开始显示,第三个是隔多久显示下一个
}
@Override
protected void onDestroy() {
timer.cancel();
super.onDestroy();
}
}

这里我写了两种方式,第一种是用Timer类来实现,后来发现使用自定义的线程池更好,大家如果不想在定义一个线程池的话,可以直接使用Timer类来实现同样的效果,至此使用第一种级n张图片切换实现动画效果的代码就完成了。这种方式有一个弊端就是得需要n张图片,那么要是只有单张图片又该怎么办呢,那么就可以使用下面这种方法了。

第二种:通过改变图片透明度实现动画效果

  1、首先我们先封装两个动画方法,第一个是从不透明到完全透明,第二个是完全透明到不透明


/**
* 透明效果
* @return
*/
public Animation getAlphaAnimationIn() {
//实例化 AlphaAnimation 主要是改变透明度
//透明度 从 1-不透明 0-完全透明
Animation animation = new AlphaAnimation(1.0f, 0);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(2000);
return animation;
}
public Animation getAlphaAnimationOut() {
//实例化 AlphaAnimation 主要是改变透明度
//透明度 从 1-不透明 0-完全透明
Animation animation = new AlphaAnimation(0, 1.0f);
//设置动画插值器 被用来修饰动画效果,定义动画的变化率
animation.setInterpolator(new DecelerateInterpolator());
//设置动画执行时间
animation.setDuration(2000);
return animation;
}

2、分别给这两个方法设置监听,即第一个动画完成立刻执行第二个动画,第二个动画完成在立刻执行第一个动画以实现动画循环播放的效果


voiceState1.setAnimation(animationIn);
voiceState1.setAnimation(animationOut);
/**
* 监听动画实现动画间的切换
*/
animationOut.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
voiceState1.startAnimation(animationIn);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
animationIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
voiceState1.startAnimation(animationOut);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});

至此使用一张图片通过改变其透明度实现闪烁效果就完成了。

以上所述是小编给大家介绍的android实现图片闪烁动画效果的两种实现方式(实用性高),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对PHPERZ网站的支持!



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务